Description

JsonVariant::is<T>() tests if the variant 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<signed long long>() const;     // <- may require ARDUINOJSON_USE_LONG_LONG
bool is<unsigned long long>() const;   // <- may require ARDUINOJSON_USE_LONG_LONG

bool is<char*>() const;
bool is<const char*>() const;

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

Return value

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

Remark about 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>() returns true for integers too.

Integer overflows

This function 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

Example

variant.set(42);
variant.is<int>();        // true
variant.is<double>();     // true
variant.is<char*>();      // false
variant.is<JsonArray>();  // false

See also