Description

Parses a JSON document and populates a JsonDocument with the content of the document

This function behaves differently depending on the input type:

  • For a read-only input, it duplicates the input string, this consume more space in the JsonDocument.
  • For a writeable input, it alter the string to insert null-terminators and replace escaped chars. This is the zero-copy mode

Prefer the first mode when input comes from a Stream; prefer the second when the input document is in a buffer.

Signatures

// writable input => zero-copy
DeserializationError deserializeJson(JsonDocument& doc, char* input);
DeserializationError deserializeJson(JsonDocument& doc, char* input, size_t inputSize);

// read-only input => duplication
DeserializationError deserializeJson(JsonDocument& doc, const char* input);
DeserializationError deserializeJson(JsonDocument& doc, const char* input, size_t inputSize);
DeserializationError deserializeJson(JsonDocument& doc, const __FlashStringHelper* input);
DeserializationError deserializeJson(JsonDocument& doc, const __FlashStringHelper* input, size_t inputSize);
DeserializationError deserializeJson(JsonDocument& doc, const String& input);
DeserializationError deserializeJson(JsonDocument& doc, const std::string& input);
DeserializationError deserializeJson(JsonDocument& doc, Stream& input);
DeserializationError deserializeJson(JsonDocument& doc, std::istream& input);

Arguments

doc: the JsonDocument that will store the memory representation of the JSON document.

input: the JSON document to parse.

inputSize: the maximum number of bytes to read from input

Return value

Returns a DeserializationError.

Example

char json[] = "{\"hello\":\"world\"}";
StaticJsonDocument<200> doc;
deserializeJson(doc, json);
JsonObject object = doc.as<JsonObject>();
const char* world = object["hello"];

See also