(Partial) Simplified JSON support in json.h
With the help of the rather awesome @ocornut - I’ve managed to get a variant of simplified JSON support into json.h!
So first off - what is simplified JSON? Taken straight from the Bitsquid blog;
- Assume an object definition at the root level (no need to surround entire file with { })
- Commas are optional
- Quotes around object keys are optional if the keys are valid identifiers
- Replace : with =
Of the four points above, I’m going to argue that only 1. and 3. are actually useful.
1. Assume an object definition at the root level (no need to surround entire file with { })⌗
If you are always going to start a JSON file with an object (which is the recommended behaviour when using JSON), it can be quite tedious to surround the parent object with { }’s. Imagine we have the following;
Whereas with 1. this could read;
As we can see, it reads that little bit nicer overall. It has a nice benefit of also meaning you don’t have to indent the parent key/value elements of the main object - if you like me are rather over the top about indentation this is a nice space saver. The one downside for the JSON security purists is that someone could easily append onto the object with new elements - something that I know is a problem in certain domains.
2. Commas are optional⌗
I really dislike this idea. Essentially the idea is that;
Would be entirely valid with simplified JSON. I dislike this because you can end up with some really disgusting code. The code above looks like the newlines are basically replacing the commas to denote new elements, but the above would be functionally equivalent to;
Which looks utterly hideous. This also adds some pretty funky parsing variants for json.h which I just wasn’t keen to add.
3. Quotes around object keys are optional if the keys are valid identifiers⌗
It can be a real chore and also quite expensive in terms of file size to surround all the keys of objects with ” “‘s too! For the example;
Given that “a”, “b” and “c” don’t contain funky characters or whitespace, why not allow them to be specified like;
It looks pretty nice, and saves some space, to be able to specify them without the ” “‘s.
4. Replace : with =⌗
This rule I dislike simply because it is a stylistic choice. Even reading the Bitsquid blog that specified simplified JSON - it was done to make the code read more like Lua. This is something we could add, but I don’t see the point as its not a functional change, its a stylistic change.
Solving the problem with 2.⌗
So as I said in my comments on 2., I don’t like the ‘commas are optional’ rule. To my rescue came @ocornut - who happened to suggest that allowing trailing commas on elements would be a useful helper for some work he was doing with my json.h library, and it got me thinking - this seems to solve at least part of the problem with 2.! Making it such that commas are a little easier to use makes them seem that little bit more innocuous for developers to use.
So I’ve settled on a happy medium of the features from simplified JSON that I think are useful. In the next post I’ll explain how I changed the API to allow both pure/original/unadulterated JSON to survive alongside my partial simplified JSON support!