My grug programming language that is written in C needs to parse JSON, but since it only needs to be able to parse strings, arrays, and objects, I’ve decided to write a tiny json.c that is roughly 500 lines long. It makes use of the array-based hash table I described in this blog post to detect duplicate object keys.

The JSON spec specifies that the other value types are number, true, false and null, but in my case I am fine with having them be stored as strings. You could easily add support for these types however by adding just a few dozen lines to json.c, so feel free to.

Instead of using malloc(), it makes use of static arrays (which grow dynamically, so essentially act as vectors), which means that parsing a new JSON file overwrites any old parsed data. It also uses longjmp() to keep the clutter of error handling at bay.

Most of the work went into adding tons of tests to ensure it has as close to 100% coverage as possible; tests.c is almost as large as json.c! The tests also act as documentation. I’ve fuzzed the program with libFuzzer and documented it in the README. See the repository’s README for more info. Enjoy! 🙂