2014-11-07 22:12:40 +01:00
|
|
|
Migrating code written for Arduino JSON v3 to v4
|
|
|
|
================================================
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v4 was a major rewrite of the library, and the API changed significantly.
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
## Includes
|
|
|
|
|
|
|
|
Arduino JSON v3 had two include files:
|
|
|
|
|
|
|
|
#include <JsonParser.h>
|
|
|
|
#include <JsonGenerator.h>
|
|
|
|
|
|
|
|
Arduino JSON v4 only has one:
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
## Namespaces
|
|
|
|
|
|
|
|
Arduino JSON v3 had two namespaces:
|
|
|
|
|
|
|
|
using namespace ArduinoJson::Parser;
|
|
|
|
using namespace ArduinoJson::Generator;
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v4 doesn't require the `using namespace` statement.
|
|
|
|
It has a namespace but the `using namespace` is done in the header file.
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
## StaticJsonBuffer
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v3 had different memory allocation models for the parser:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
JsonParser<16> parser; // 16 being the capacity in "tokens"
|
|
|
|
|
|
|
|
and for the generator:
|
|
|
|
|
|
|
|
JsonArray<4> array; // 4 being the number of element
|
|
|
|
JsonObject<4> object;
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v4 only has one memory allocation model:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
StaticJsonBuffer<128> buffer; // 128 being the capacity in bytes
|
|
|
|
|
|
|
|
## Return values for the parser
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v3 returned value types:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
JsonArray array = parser.parseArray(json);
|
|
|
|
JsonObject object = parser.parseObject(json);
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v4 returns references types:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
JsonArray& array = buffer.parseArray(json);
|
|
|
|
JsonObject& object = buffer.parseObject(json);
|
|
|
|
|
|
|
|
Everything else is compatible
|
|
|
|
|
|
|
|
## Creating arrays and objects
|
|
|
|
|
|
|
|
Arduino JSON v3 allowed to create `JsonArray` and `JsonObject` directly:
|
|
|
|
|
|
|
|
JsonArray<4> array;
|
|
|
|
JsonObject<4> object;
|
|
|
|
|
|
|
|
Arduino JSON v4 requires that you use a `StaticJsonBuffer` for that:
|
|
|
|
|
|
|
|
JsonArray& array = buffer.createArray();
|
|
|
|
JsonObject& object = buffer.createObject();
|
|
|
|
|
|
|
|
Note: you don't have to specify the capacity anymore.
|
|
|
|
|
|
|
|
## Printable interface
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
Arduino JSON v3 used to implement the Printable interface, which allowed statements like:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
Serial.print(array);
|
|
|
|
|
2014-11-29 10:36:15 +01:00
|
|
|
But Arduino JSON v4 doesn't, instead you need to write this:
|
2014-11-07 22:12:40 +01:00
|
|
|
|
|
|
|
array.printTo(Serial);
|
|
|
|
|
2014-11-29 13:53:18 +01:00
|
|
|
Note: there was a good reason for removing that feature, and it's reducing the size of `JsonArray` and `JsonObject`.
|