JsonObject::isNull()
Description
JsonObject::isNull()
tests whether the JsonObject
points to an object or not.
You can use this function to:
- check if the object was successfully parsed, or
- check if the object was successfully allocated.
As an alternative, you can use the conversion to bool
; for example, if(object)
instead of if(!object.isNull())
Signature
bool isNull() const;
Return value
JsonObject::isNull()
returns a bool
that tells if the JsonObject
points to something:
true
if theJsonObject
is null,false
if theJsonObject
is valid and points to an object.
Examples
Example 1: parsing success:
JsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject object = doc.as<JsonObject>();
Serial.println(object.isNull()); // false
Example 2: parsing failure:
JsonDocument doc;
deserializeJson(doc, "[\"hello\",\"world\"]");
JsonObject object = doc.as<JsonObject>();
Serial.println(object.isNull()); // true
Example 3: allocation success:
JsonDocument doc;
JsonObject object = doc.to<JsonObject>();
Serial.println(object.isNull()); // false
Example 4: allocation failure:
JsonDocument doc;
JsonObject object = doc.to<JsonObject>();
Serial.println(object.isNull()); // true