ArduinoJson: F.A.Q

How to prevent string escaping?

String escaping is the feature that allows to have special characters in your encoded string.

For example:

DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["hello"] = "[1,2,3]";
root.printTo(Serial);

would print:

{"hello":"[1,2,3]"}

If you want to disable string escaping, you need to wrap the char* with a RawJson, like this:

DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["hello"] = RawJson("[1,2,3]");
root.printTo(Serial);

which would print:

{"hello":[1,2,3]}

CAUTION: There is one caveat! Unlike const char*, std::string and String, the RawJson string is not duplicated, so you need to make sure it’s still in memory when you call printTo(). You can still duplicate manually by calling JsonBuffer::strdup(), like this:

DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["hello"] = RawJson("[1,2,3]");
{
    String s = "[1,2,3]";
    root["hello"] = RawJson(jsonBuffer.strdup(s));
    // s is destructed here, but that's ok because we made a copy
}
root.printTo(Serial);
Fork me on GitHub