[Arduino] JSON library 3.0

Only two weeks after version 2.0, I'm releasing a new major revision of Arduino JSON library.

In case you don't know it already, it's a free JSON library for embedded systems with the following features:

  • Elegant API, very easy to use
  • Fixed memory allocation (no malloc)
  • Small footprint

EDIT: you should directly jump to version 4.0

What's new in 3.0?

  • New API for the JSON parser (see below)
  • Renamed JsonHashTable into JsonObject
  • Added iterators for JsonArray and JsonObject

A new API for the parser

This is the major change in this revision: an extremely intuitive API for the JSON parser.

Let's see an example. Here is a snippet written for version 2.0:

JsonHashTable root = parser.parseHashTable(json);

char*  sensor    = root.getString("sensor");
long   time      = root.getLong("time");
double latitude  = root.getArray("data").getDouble(0);
double longitude = root.getArray("data").getDouble(1);

And now, here is the same code written for version 3.0:

JsonObject root = parser.parse(json);

char*  sensor    = root["sensor"];
long   time      = root["time"];
double latitude  = root["data"][0];
double longitude = root["data"][1];

As you can see, you can extract the JSON data in a much more intuitive way.

How does it works?

The principles of this new API are:

  • Almost every function returns a JsonValue
  • operator[] is directly available in JsonValue
  • You can cast a JsonValue to
    • a double
    • a long
    • a boolean
    • a char*
    • a JsonArray
    • a JsonObject
  • JsonArray and JsonObject provide advanced features, like iterators

Can I still use the old API?

I don't see any reason to stick with the old API.
However, I'll maintain the compatibility within all the 3.X revisions.
In other words, I plan to remove the old API in version 4.0.
That way, you should be able to update your code progressively.

When you use the old API, you get a few compilation warnings saying that some feature is deprecated.

I don't recommend this, but if you want to disable those warnings, just add the following line above the #include: