Skip to content

A Nice JSON Parser for C++

July 18, 2011

I want to bring attention to a JSON parser for C++ created by somebody named Mike from the UK. It’s not one of the many listed on the JSON web site, so people may not be aware of it.

That’s really the end of the important section of this post, the rest is me waxing needlessly.

A few years ago I needed a JSON parser in C++ to read Avro schemas. At the time, the choices were more sparse than now (there were only a few listed on JSON.org), and I couldn’t find one I felt comfortable with. They either brought in extra unwanted dependencies, or had other issues such as not compiling out-of-the-box (and me not wanting to find out why and fix it).

I can probably invent other excuses I didn’t reuse an existing parser, but the biggest factor was a desire to roll my own, as I hadn’t yet done much with parsers and wanted to play. And so I did.

I figured to teach myself lex and yacc, the canonical Unix tools for parsing (well technically their derivatives, flex and bison). Little did I know that I would enter a world of hurt dealing with the fact that there are many versions of these tools floating around, none of which are happy to be conversant with the others. If it compiled and worked on one system, only luck will make it work on another, depending on which versions were installed. Need the parser to be thread safe? It’s possible but only after you jump through hoops. Want documentation? There’s a lot out there, but in disjointed form: I’d find a description of a fix for my problem only to discover it didn’t work on the version I used.

I don’t mean to harp on these tools much, they’re actually really great and powerful, and worked well once I understood them. In teaching myself to use them, I learned a bit about parser internals too. It just happens that portability across different versions is not one of the lex/yacc family strengths, and if you’re trying to create a project that builds on a variety of systems it becomes a headache.

Flash forward to the present, and again I found myself in need of a JSON parser. This time I didn’t want to write my own again (not that I had the luxury of time to do so anyway). So a coworker pointed me to this simple one, which worked nicely for my needs, one of which was ease of building in Windows. This was no fuss to get working on Windows or Linux for that matter, unlike some of the others I tried, and the API was straightforward and did what I needed.

If this existed back when I did my own, it would’ve saved me a lot of time. But then I never would’ve learned lex and yacc, so I am thankful for that experience. But I’m also glad I’m not writing the parser this time.

4 Comments leave one →
  1. Andrew Dragon permalink
    October 6, 2011 2:59 AM

    please reply.

  2. February 23, 2013 11:48 PM

    Hi the link to this C++ parser from “mike” doesn’t seem to work – i don’t suppose you have the source by chance!?

    • sbanacho permalink*
      February 26, 2013 7:30 AM

      Hi, here’s a link to the github page for the project: https://github.com/MJPA/SimpleJSON

      • January 2, 2014 12:16 AM

        At my master tsheis I was working on a task to create a new compiler for my tutor’s new theoretic programing language. So, I am student, but I still managed to get started with JavaCC (so the statement, JavaCC is to hard for students is wrong). I had a look into ANTLR as well, documentation is awesome, but ANTLR didnt fit my needs.JavaCC and especially JJTree were what I was looking for. After some research I was able to find some good documentation for JavaCC and JJTree. Of course, I had good experience due to my study, only the syntax of JavaCC grammars were different. The rules were kinda same then i learnt in CoCo-2. Also a good tip for getting started with JavaCC is the book from Tom Copeland. But with some experience the documentation is kinda enough.

Leave a Reply

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