I was recently made aware of the awesome JSON parser/writer benchmark suite nativejson-benchmark  (thanks to the awesome @chadaustin).

The suite takes over 40 different JSON libraries and compares how fast the can parse/write JSON, but also has a set of test cases that cover some really obtuse corner cases that a lot of parsers missed. One of the parsers that failed some of these tests was my own one json.h, so in pull request #42 I’ve merged in the fixes for the failures uncovered by the test suite.

The main difference when users of json.h migrate to the latest code is the deprecation of the json_parse_flags_allow_string_simplification option. My reading of the JSON specifications was wrong in that I thought I had to preserve control characters like ‘\n’ into the end result. The json_parse_flags_allow_string_simplification option was my ‘solution’ to what I thought would be non-conforming behaviour. Now, with the tip code, effectively json_parse_flags_allow_string_simplification is always on by default, with no option to turn it off.

Another change is that any malformed characters that occurred after a valid JSON sequence in source would have previously been ignored. Say you had provided the following JSON:

{“a” : true, “b” : false} heyo this really shouldn’t be here

Previously, the parser would just parse the object and terminate when the object closed - EG. when the closing ‘}’ was detected. This was dangerous behaviour that nativejson-benchmark correctly failed my library on, so I’ve changed it such that it will fail if:

  • Any non-whitespace character is found after the close of the parent value
  • Or, if json_parse_flags_allow_c_style_comments is enabled, any non-whitespace character occurs outwith any comments that occur after the close of the parent value

json.h should now pass 100% of the tests provided in the nativejson-benchmark test suite, and be more useful to users going forward. Happy hunting folks!