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
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user