Wednesday, September 19, 2018

JSON Parsing

Just because it's the current thing for exchanging information over the interwebz, and because I've needed a method to serialize custom properties for SAFMQ, I've developed a couple of JSON parsers.

I know, I know...  There are literally hundreds of JSON parsers (well tens for C++), but a bunch had requirements like C++11 (which is kinda old, but pretty new as far as SAFMQ goes).

So I write one in C++, it uses std::vector<T>, std::map<K,T>, and std::string to manage it's contents.  I also features custom validators to check numeric formats, and works on a byte-by-byte method of parsing, so it's pretty fast.  Only slowed down by the time to place items in the vector/map for arrays and object members.  Since it's C++ I overloaded the operator[](int) and operator[](const std::string&) to access items and members, and added methods to make it seem like a vector or map when you access contents.  There's  a set of cast operators to retrieve integers, doubles, and strings.  I snached up some CppUnit code to throw a bunch of tests at it.  CppUnit doesn't have code coverage built in, so my goal was to manually ensure I covered every branch.  I feel pretty good about it.

Then yesterday I decided to do the same in Java.  I'm new to IntelliJ that we use at work so it was an opportunity to get more familiar with the IDE as well as check code coverage with jUnit what not.  Like the C++ version it does a byte-by-byte parse, and leverages ArrayList<T> and HashMap<T> for items and members.  Java doesn't have custom operators, so in come getters an setters.  Overall, I'm pretty pleased, although I had to do some weird steps to ensure 100% code coverage (if/throw on a single line, remove an early check and put it deeper in the method, which lead to sub-optimal code).  Most of it was because the overall structure of the program, double checking conditions in subsequent code, etc.  Built this one with 100% coverage in a day, so I feel pretty good about it.