Json Library?

I'm looking for a good, lightweight library that can parse JSON files for configuration data in my game (along with a tutorial).

I want something simple and lightweight (that doesn't use anything except for the standard libraries).
JSON is a very easy format to parse, so you generally don't need a library for it (that is one of its main benefits) - you can just make a quick one yourself.

If you want a library anyway, I don't know of any that fullfil your requirements exactly. If you are using the boost library anyway, the Boost.Property_Tree library provides a JSON parser, as well as a very usable interface to retrieve data from it. Its also a header-only template library - what you don't use doesn't add to your executable.

However, if you don't want a dependency of boost, the official JSON website ( http://www.json.org/ )has about 20 conforming JSON parsers for C++ and another 20 or so for C (see near the bottom of the page). You can probably find one you like from those ones.
Last edited on
I've decided to use Jansson. Thanks.
closed account (10X9216C)
The problem with boost is that it uses strings for all it's types. So if you use boost to generate a json file that is going to be used in javascript or something similar. You'll need to manually convert the values to their appropriate types.
spectral wrote:
You'll need to manually convert the values to their appropriate types.
I only really needed it as an intermediate format for my game engine; just something that didn't stop my work flow, easy to add onto, and quick to parse. I had no intent to use javascript (or anything remotely similar), and I plan on compiling it to a different format upon release, so this really didn't effect me much (Jansson handles the conversions anyway).

Jansson works great and I'd recommend it to anyone; it was trivial to set it up with cmake and compile it's library (quick as well), and I got a working solution in a matter of minutes with everything set up.
Last edited on
closed account (10X9216C)
Well it is handy information, I ended up using boost without realizing it and when my plans changed and i ended up using the json files with javascript it was a bit too late to change api. I did look at some other libraries before using boost, some of them didn't allow the user to specify the order or just put them in a random/alphabetical order, which kind of mattered with some of the content.

Jansson works great and I'd recommend it to anyone; it was trivial to set it up with cmake and compile it's library (quick as well), and I got a working solution in a matter of minutes with everything set up.

That is just about almost every library out there that uses cmake. That is what cmake is for after all.
Last edited on
That is just about almost every library out there that uses cmake. That is what cmake is for after all.
Well, my only experience with cmake was when I previously built SFML 2.1 a while ago (I've been using SDL for years and I wanted to see what SFML was about), and there were tons of non-trivial errors with the generation of the project file. I guess I just wasn't aware that it was reliable (which I don't think anymore).
Last edited on
I use cJSON - very lightweight and no setup required, just add cjson.c and cjson.h to your project and you are done.
http://sourceforge.net/projects/cjson/
I like json-parser because of the granularity in the type system - it is much more fine-grained than JavaScript's type system. I actually needed to port a project to HTML5 and ran into a snag when I found out that JavaScript did not have disparate floating point and integer types. I think it also allows for duplicate keys in objects, which is useful for parsing user-made JSON.

If you don't care about the types of data (e.g. only care if it is a number or not) then the only real benefit of json-parser is the speed and low memory usage (maybe you're parsing a huge amount of data).

If you don't need the speed and don't care about excess memory usage, then use a high level C++ json-parsing library.
Last edited on
I use jsoncpp because it is in my package management system.

I tried boost, but it wasn't accepting valid json. I had something like this:
1
2
3
4
5
{
  [
    { "name" : "john" }
  ]
}


Boost wanted:
1
2
3
4
5
6
{
  "people":
  [
    "person": { "name": "john" }
  ]
}
Last edited on
@LowestOne: Your first example was not valid JSON. Objects cannot contain nameless members. Your second example is also not valid JSON - array elements cannot have names.

http://jsonlint.com/
Last edited on
Then it was something else, that is why I said "something like"

Edit: going back into the project, I had this:
1
2
3
4
5
6
{
  "people":
  [
    { "name", "john" }
  ]
}


If I recall correctly, Boost didn't like the unnamed object inside the array.
Last edited on
That's still not valid JSON, you used a comma instead of a colon inside that object.
lol
Topic archived. No new replies allowed.