Description

JsonDocument::is<T>() tests if the document is currently holding a value of type T.

Signatures

bool is<bool>() const;

bool is<float>() const;
bool is<double>() const;

bool is<signed char>() const;
bool is<unsigned char>() const;
bool is<signed int>() const;
bool is<unsigned int>() const;
bool is<signed short>() const;
bool is<unsigned short>() const;
bool is<signed long>() const;
bool is<unsigned long>() const;
bool is<unsigned long long>() const;   // ⚠️ may require ARDUINOJSON_USE_LONG_LONG
bool is<signed long long>() const;     // ⚠️ may require ARDUINOJSON_USE_LONG_LONG

bool is<char*>() const;  // ⛔ removed in 6.20
bool is<const char*>() const;
bool is<String>() const;
bool is<std::string>() const;

bool is<JsonArray>() const;
bool is<JsonObject>() const;
bool is<JsonVariant>() const;

bool is<JsonArrayConst>() const;
bool is<JsonObjectConst>() const;
bool is<JsonVariantConst>() const;

bool is<TEnum>() const; // alias of as<int>()
bool is<T>() const;     // calls user-defined function

Return value

  • true if the document is currently holding a value of type T,
  • false if not

JSON types vs. C++ types

Different C++ types can store the same JSON value, so is<T>() can return true for several Ts. For example, is<float>() always returns the same value as is<double>() .

The table below gives the correspondence between the JSON type and the C++ types:

JSON type T
Floating point float, double
Integer int, short, long, long long
String const char*, char*
Boolean bool
Array JsonArray
Object JsonObject

Caution: is<float>() and is<double>() return true for integers too.

Integer overflows

JsonDocument::is<T>() is aware of integer overflows and only returns true if the specified type can store the value.

For example if the variant contains 512, is<char>() returns false, but is<int>() return true.

This feature was added in ArduinoJson 6.10.0

User-defined types

JsonDocument::is<T>() supports user-defined types by calling canConvertFromJson(). For example, to support an hypothetical Complex class, we could write:

bool canConvertFromJson(JsonVariantConst src, const Complex&) {
  return src["real"].is<double>() && src["imag"].is<double>();
}

The second parameter of canConvertFromJson() is required to trigger ADL but must not be used by the function.

This feature was added in ArduinoJson 6.18.0, see article for details.

Example

StaticJsonDocument<32> doc;
doc.set(42);
doc.is<int>();         // true
doc.is<double>();      // true
doc.is<const char*>(); // false
doc.is<JsonArray>();   // false

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)