#include "JFile.h" #include "ZString.h" #include "Exception.h" namespace coreutils { // MFile and JString share a single virtual MString base (the diamond: // both "MFile : virtual public MString" and "JString : virtual public // MString"), and a virtual base is initialized only by the *most // derived* class's constructor - so JString's own "{}" default // initializer never runs here. Left alone, a JFile backed by a new or // empty file starts as a raw empty string instead of a valid empty JSON // object, and every subsequent locate()/set() call silently misbehaves // (no leading "{" for ifNext("{") to match, so writes end up inserted // at offset 0 with no braces or separators at all). Detect that case // here and seed valid JSON explicitly. JFile::JFile(ZString filename) : MFile(filename) { if(getLength() == 0) *this << "{}"; } }