diff --git a/JFile.cpp b/JFile.cpp index ed64f43..69c171a 100644 --- a/JFile.cpp +++ b/JFile.cpp @@ -15,8 +15,22 @@ namespace coreutils { // 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) + if(getLength() == 0) { *this << "{}"; + } else { + // MFile's own file-loading reads the raw bytes straight into the + // shared MString buffer - it never goes through JString::operator=, + // which is the only other place this class normally strips + // whitespace before parsing (removeWhitespace()). Without this, a + // JFile backed by a hand-written or pretty-printed JSON file (any + // whitespace between tokens - indentation, newlines, spaces after + // ':'/',') fails every locate()/exists()/get*() call, since + // JString's parser expects compact JSON with no whitespace at all + // between structural tokens. In-memory JSON built via operator= + // never hit this because it already goes through the same + // normalization - loading from disk just missed it. + removeWhitespace(); + } } diff --git a/JString.h b/JString.h index 62847a0..f1ba16c 100644 --- a/JString.h +++ b/JString.h @@ -193,9 +193,22 @@ namespace coreutils { virtual void changed(ZString key, ZString value); - private: + protected: + + /// + /// Strips whitespace between JSON structural tokens (indentation, + /// newlines, spaces after ':'/',') - the parser expects compact JSON + /// internally. operator=(ZString) already calls this for in-memory + /// assignment; JFile's constructor also calls it after loading a + /// file's raw bytes, which otherwise bypasses this entirely (see + /// JFile.cpp). Protected rather than private specifically so JFile + /// can reach it. + /// + void removeWhitespace(); + private: + /// /// Structural helpers used internally by locate()/set() to correctly skip /// past a value of any JSON type without being confused by JSON-structural diff --git a/testing/jfile_test b/testing/jfile_test index 3271a2a..00cfdd2 100755 Binary files a/testing/jfile_test and b/testing/jfile_test differ diff --git a/testing/jstring_test b/testing/jstring_test index 689ee78..2c72bd3 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/mfile_test b/testing/mfile_test index dc7ae5c..dee58c9 100755 Binary files a/testing/mfile_test and b/testing/mfile_test differ