Go Down

Topic: ArduinoJson 5.13.0 is out! (Read 896 times) previous topic - next topic


Jan 19, 2018, 04:06 pm Last Edit: Jan 20, 2018, 10:25 am by BenoitB
I just released a new version of ArduinoJson, a library to serialize and deserialize JSON documents.

This new version brings a few improvements concerning the duplication of strings.

New rules for string duplications

When a program adds a string, ArduinoJson stores it differently depending on the type of the string.

In previous versions, ArduinoJson stored a pointer when the string was a plain old C string (char*, const char*, char[]), but stored a copy when the string had any other type (String, Flash strings...).

ArduinoJson 5.13 changes the rules: now, only const char* are stored with a pointer, all other string types are duplicated, which greatly simplifies our programs.


Since older versions stored char[] strings with a pointer, we used to see curious behavior when adding strings in a loop. To prevent ArduinoJson for storing the same pointer (an thus the same string) several times, we had to call JsonBuffer::strdup():

Code: [Select]

JsonArray& array = jsonBuffer.createArray();
for (int i=0; i<3; i++) {
    char buffer[16];
    sprintf(buffer, "iteration %d", 0);
    array.add(jsonBuffer.strdup(buffer)); // <- need to duplicate :-(

Thanks to the implicit duplication of char*, we can simplify this program to:

Code: [Select]

JsonArray& array = jsonBuffer.createArray();
for (int i=0; i<3; i++) {
    char buffer[16];
    sprintf(buffer, "iteration %d", 0);
    array.add(buffer); // <- implicit duplication :-)

Improvement to RawJson()

RawJson() is a special function to insert raw JSON fragments in a JsonArray or a JsonObject.

In previous versions, RawJson() only accepted const char* as an argument; now, in ArduinoJson 5.13, it also accepts String objects and Flash strings too.

But there is more: strings marked with RawJson() obey to the same duplication rules as other strings.

Here is an example:

Code: [Select]

JsonObject& root = jsonBuffer.createObject();
root["coord"] = RawJson(F("{\"lat\":48.74801,\"lon\":2.293491}"));

Bye bye strdup()

The new rules for string duplication make JsonBuffer::strdup() useless.

In ArduinoJson 5.13, JsonBuffer::strdup() is flagged as deprecated, so you'll have a compilation warning if your program uses it.

I'll probably remove the function in a future release, so I encourage you to update your programs.

How to get ArduinoJson 5.13.0?

You can download this new version from the Arduino Library Manager.

If you don't use the Arduino IDE, you can download the single header version.

As usual, you can try the library on wandbox.org:

* JsonParserExample
* JsonGeneratorExample


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up