by crdoconnor · 11y ago · 0 comments
If you have a file containing this:
["Name", "Session", "Score", "Completed"]
["Gilbert", "2013", 24, true]
["Alexa", "2013", 29, true]
["May", "2012B", 14, false]
["Deloise", "2012A", 19, true]
And it gets chopped in half (a problem that is not that uncommon), you will get this: ["Name", "Session", "Score", "Completed"]
["Gilbert", "2013", 24, true]
["Alexa", "2013", 29, true]
Which is still valid. This will cause missing data rather than a clear error message. May and Deloise may end up not getting their scores and there's a good chance nobody will notice.By contrast when the file is represented as regular JSON:
[
["Name", "Session", "Score", "Completed"],
["Gilbert", "2013", 24, true],
["Alexa", "2013", 29, true],
["May", "2012B", 14, false],
["Deloise", "2012A", 19, true]
]
(not a completely ideal representation, but you get the picture)Chopping this in half will give you a clear error, forcing the developer to recover from it before continuing.
Same principle as strong vs. weak typing.
Individual JSON snippets in lines are a good replacement for plain text-only logs of indeterminate length, however.
Most JSON parsers simply parse the JSON string and represent it as an object in memory. You don't want to do that for very big files. Stream based parsers avoid this problem, but are more complicated to work with.