WDL ~ Comparing options for parsers for JSON files - uchicago-cs/chiventure GitHub Wiki
As we are making the switch to using JSON objects rather than YAML, this document will discuss the options available to use for parsing JSON objects.
Currently, we have a YAML parser implemented in parser.c in the parse_game() function. This YAML parser SHOULD work with JSON objects, as JSON is a subset of YAML. Multiple sources confirm this: https://stackoverflow.com/questions/24608600/is-it-safe-to-parse-json-with-yaml-load https://www.json2yaml.com/yaml-vs-json
However, there are parsers that are specific to Json, cJSON and Json-c, that seem to be strong options to use instead of the YAML parser we currently have. To see whether using a Json specific parser would work better than using the YAML parser we currently have and to see which Json specific parser would be better if we decide to use one, I looked at various sources to compare the parsers:
From http://www.discoversdk.com/compare/cjson-vs-json_c:
“cJson:
- It's a single file of C, and a single header file.
- cJSON doesn't make any assumptions about what order you create things in. You can attach the objects, as above, and later add children to each of those objects.
- Open Source & Easy to use.
Json-c
- parse JSON strings into the C representation of JSON objects.
- Open Source & Easy to use.”
I mention this because cJson isn’t too complex as it contains its functions in a single file as shown in: https://github.com/DaveGamble/cJSON/blob/master/cJSON.h, so the implementation of this parser in parse_game() won’t be hard enough to be a reason to stop us from using it. Json-c, https://github.com/json-c/json-c, is also mentioned as easy to use so changing the YAML parser to Json-c shouldn’t be too difficult.
From https://stackoverflow.com/questions/1726802/what-is-the-difference-between-yaml-and-json Mentions that there is a speed difference in using json over yaml. However, this doesn’t mention if this difference is due to the YAML file format or the difference is due between using a JSON specific parser and the YAML parser. I have also found many answers like the answer from within this link that YAML is better than JSON because of serialization. However, this is irrelevant because: We are using the JSON files for parsing and not serialization as we don’t plan on converting objects that are in the doc obj type in the Chiventure repository back into the JSON format. The serialization answer is based on YAML/JSON objects instead of the parser itself.
However, https://stackoverflow.com/questions/2451732/how-is-it-that-json-serialization-is-so-much-faster-than-yaml-serialization-in-p shows that there is a speed difference between YAML and Json-c in serialization. However, the top answer in this post mentions that “The YAML parsers are comparatively complex, leading to increased overheads.” compared to Json-c. So this suggests that YAML parsers are slower than Json specific parsers by magnitudes (as shown in the speed of each parser in the question), which could be important if we plan on expanding chiventure to be a MUCH longer game than it is right now.
So in conclusion, using a Json specific parser is better than using the current YAML parser we currently have. Between which parser to use, Json-c seems to be the better choice, as cJSON doesn't have any significant advantages that will outweigh Json-c's ability to parse JSON strings into C's representation of JSON objects as we will only use Json-c for parsing. This will allow us to parse the string easier as it allows us to get information from JSON objects to use in our obj type in libobj.