23 Dec

Full Simplified JSON support in json.h

In a previous post (Partial) Simplified JSON support in json.h I covered the partially added simplified JSON support I had added to json.h. One of the things I covered was my unwillingness to implement two of the features of simplified JSON, commas being optional and replacing : with =. I argued that both of these were unnecessary and stupid additions to the library.

I was wrong. The immediate feedback I received detailed good reasons why these were useful, but more importantly – why did I half-arse implementing a feature that was requested by a user!

So today, I’ve implemented all of simplified JSON. You can now parse;

foo = "bar"
yada = 2
meow = true

by using the following code;

const char payload[] = ...; // the string above!
json_value_s* json = json_parse_ex(
  payload,
  strlen(payload),
  json_parse_flags_allow_simplified_json,
  0);

A few caveats to remember when writing/using this code though:

  • json_parse_flags_allow_simplified_json is a bitmask enabling many other json_parse_flags that you can enable separately. As such, the behaviour of each of them applies collectively when using this flag
  • commas aren’t banned, they are just not required. You can mix commas/no-commas throughout your simplified JSON. You can also have trailing commas after the last element in an object/array now too
  • unquoted keys aren’t banned, you can mix and match quoted/unquoted keys
  • you always have a global object. Even if your JSON string was ‘{}’, you would have a global object that contained one empty object with simplified JSON enabled
  • colons aren’t banned, you can mix ‘:’ and ‘=’ when separating your key/value pairs within objects

I hope you find this option useful, and I intend to keep working on json.h over my Christmas holidays so stay tuned!