Snake - Story mode design, and Json parser woes

Published September 02, 2019
Advertisement

I started up a wiki over on my GitHub account describing the story mode design for The Garden of Eating.  I'm looking forward to implementing all of that, but the first step in getting there is dealing with the file system and JSON parsing.

I was originally thinking I'd just have a "campaigns" folder, where each sub-folder would represent a campaign that the player would pick somewhere on the menu.  I quickly realized that C++ is still rough around the edges in supporting certain file system operations -- like enumerating the list of files or folders at some given path:

  • I was hesitant to use the new std::filesystem.  So far, my project is C++11 compliant; this namespace was added in C++17.
  • I was hesitant to use this Boost library I'm reading so much about.  It's another thing to learn, and to try to build or integrate into my project.  Maybe next time...
  • I don't want to use operating-specific calls.  I still have the thought that it would be nice to be able to take all my code and build it for a Mac or Linux machine.

So instead of enumerating the sub-folders, I'm just going to require that there be a "campaigns-list.json" file found in the "campaigns" folder.  This file must be a JSON array of strings, listing the folder names.  It seemed like a small price to pay, in order to keep my dependencies to a minimum, and to keep my build environment flexible.

And then I started trying out some of the various free / open source C++ Json libraries out there...

Nlohmann JSON for Modern C++

I first tried the latest version of Nlohmann JSON for Modern C++.  At a glance, it seemed like the simplest library to get going.  It has a single header file you can include in your project, and away you go.  Unfortunately, I struggled to get it to parse a JSON file, and felt the need to try a different library.  This stackoverflow question didn't solve my issue either.  The library's approach for parsing JSON from a file has been deprecated and changed multiple times over the library's short lifespan, and trying to use this static "parse" method with the 3.7 version in VS 2019 was just not compiling.  Maybe I'm just a dumb-dumb, but if I can't get something like this to work within a half-hour, it's time to move on.

The main readme for the library still shows parsing from a file using the 1.X version approach.  If this could be updated with whatever the 3.7 version is using, I presumably wouldn't have had an issue, and my JSON library evaluation would have ended there.  Unless the 3.7 version is indeed using the static "parse" function, in which case I'd have to be of the belief that not a single person is successfully using this library because I'm always right when I code things. ?


// read a JSON file
std::ifstream i("file.json");
json j;
i >> j; // My note:  this was deprecated a long time ago!

RapidJSON

Next I tried RapidJSON.  I was able to get this to work, but there were a few red flags that pushed me away from it:

  • As of today, the Build Status on their front page is failing for both Linux and Windows.  This didn't instill me with confidence.
  • The licence file mentions the MIT, BSD, and JSON licences.  On further reading, this wouldn't have been an issue, as I wouldn't be including the portion that is under the JSON licence.  But I like to keep things simple.
  • It's a header-only library as well, but there are LOTS of header files.  I wanted to see if I could find something with fewer files.

JsonCpp

The JsonCpp library is where I've ended up.  To get this integrated into my project, I did have to install Python and run the "amalgamate.py" script.  This produces a distribution folder with two header files, and a single *.cpp source file.  The library's API is just fine for my purposes, and I was able to parse and navigate some sample Json files without any trouble.

So, JsonCpp it is.

0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement