Description

Tells why deserializeJson() or deserializeMsgPack() failed.

Values

A DeserializationError is an enumerated type that can contain one of the following values:

DeserializationError::Ok
The deserialization succeeded. Cool!
DeserializationError::IncompleteInput
The end of the input is missing. Possible reasons:
  • the input buffer was too small
  • a timeout occurred
DeserializationError::InvalidInput
The input is not recognized. Possible reasons:
  • the input is simply invalid
  • the input is valid but is preceded by sometjing else
DeserializationError::NoMemory
The JsonDocument is too small; you need to increase its capacity.
DeserializationError::NotSupported
The document included features not supported by the parser. Possible reasons:
  • in JSON, a unicode surrogate pair like \u0032
  • in MessagePack, a binary value
  • in MessagePack, a object key that is not a string
DeserializationError::TooDeep
JsonDocument::nestingLimit was reached; you need to increase its value

Methods

// return a string representation of the error
const char* c_str() const;

// returns the enum value
Code code() const;

Example

Get the error message

DynamicJsonDocument doc;
DeserializationError err = deserializeJson(doc, "!!NOT JSON!!");
if (err) {
    Serial.print(F("deserializeJson() failed: "))
    Serial.println(err.c_str())
}

the program above prints:

deserializeJson() failed: InvalidInput

Switch/case

DynamicJsonDocument doc;
DeserializationError err = deserializeJson(doc, "!!NOT JSON!!");
switch (err.code()) {
    case DeserializationError::Ok:
        Serial.print(F("Deserialization succeeded"));
        break;
    case DeserializationError::InvalidInput:
        Serial.print(F("Invalid input!"));
        break;
    case DeserializationError::NoMemory:
        Serial.print(F("Not enough memory"));
        break;
    default:
        Serial.print(F("Deserialization failed"));
        break;
}

the program above prints:

Invalid input!

See also