18 Aug


Back in May, the rather awesome imgui creator @ocornut asked the following;

And it got me thinking – why isn’t there a simple JSON reader/writer lib in the same vein as the stb_* libraries that performs a single call to malloc to encode the state? I couldn’t find one, so I decided to write my own.

I’m introducing json.h – my one header/one source json library that will parse a JSON source into a single allocation buffer, and also has functions to write out the minified version of the JSON, and a pretty print function (for human readable JSON).

Lets go through a worked example, lets take the following trivial JSON;

{“a” : [123, null, true, false, “alphabet”]}

The above example covers all the core concepts inherent within JSON, so serves as a good coverage tool for our parsing. The above will be parsed (using json_parse) into a single-malloc’ed buffer, with the start of that buffer being a json_value_s* – a pointer to the root value. The Document Object Model (DOM) for this JSON is;


And the single allocation in-memory view of the above is;


In terms of speed of the library I’ve used these JSON files for reference;

Which produces the following chart;

chartCurrently, parsing is averaging around 55 MB/s, pretty writing around 300 MB/s and minified writing around 500 MB/s on my Intel Core i7-2700k 3.5GHz.

My next step will be to look into my parsing approach and see if anything can be done to speed up parsing of JSON!

I hope this library is useful, and I’m happy to have any comments/critiques on my approach.


One thought on “json.h

  1. Pingback: Allocators in json.h! | Duskborn

Leave a Reply

Your email address will not be published. Required fields are marked *