Fix JFile to normalize whitespace when loading JSON from disk
JString::operator=(ZString) already stripped whitespace between JSON structural tokens before parsing, but JFile constructed via MFile file- loading bypassed that path entirely, so any pretty-printed/hand-edited JSON file silently failed every locate()/exists()/get*() call. Widen removeWhitespace() to protected and call it from JFile constructor.
This commit is contained in:
1 parent
6b188fbee5
commit
d7057ebb00
5 files changed
+29
-2
No files matched your search
@@ -15,8 +15,22 @@ namespace coreutils {
|
|||||||
// at offset 0 with no braces or separators at all). Detect that case
|
// at offset 0 with no braces or separators at all). Detect that case
|
||||||
// here and seed valid JSON explicitly.
|
// here and seed valid JSON explicitly.
|
||||||
JFile::JFile(ZString filename) : MFile(filename) {
|
JFile::JFile(ZString filename) : MFile(filename) {
|
||||||
if(getLength() == 0)
|
if(getLength() == 0) {
|
||||||
*this << "{}";
|
*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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -193,9 +193,22 @@ namespace coreutils {
|
|||||||
|
|
||||||
virtual void changed(ZString key, ZString value);
|
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();
|
void removeWhitespace();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Structural helpers used internally by locate()/set() to correctly skip
|
/// Structural helpers used internally by locate()/set() to correctly skip
|
||||||
/// past a value of any JSON type without being confused by JSON-structural
|
/// past a value of any JSON type without being confused by JSON-structural
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user