-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
This is the detailed documentation of every class and function of the ArduinoJson library. Some parts have been simplified to be easier to understand, so if you look at the source code, you might see some differences.
-
JsonArray
- Constructor
- JsonArray::add()
- JsonArray::createNestedArray()
- JsonArray::createNestedObject()
- JsonArray::get()
- JsonArray::measureLength()
- JsonArray::measurePrettyLength()
- JsonArray::operator[]
- JsonArray::prettyPrintTo()
- JsonArray::printTo()
- JsonArray::removeAt()
- JsonArray::set()
- JsonArray::size()
- JsonArray::success()
- JsonBuffer (DynamicJsonBuffer and StaticJsonBuffer)
-
JsonObject
- Constructor
- JsonObject::begin() / JsonObject::end()
- JsonObject::createNestedArray()
- JsonObject::createNestedObject()
- JsonObject::get()
- JsonObject::measureLength()
- JsonObject::measurePrettyLength()
- JsonObject::operator[]
- JsonObject::prettyPrintTo()
- JsonObject::printTo()
- JsonObject::remove()
- JsonObject::set()
- JsonObject::size()
- JsonObject::success()
- JsonVariant
Represents an array in a JSON object tree.
The constructor is private, you cannot instantiate a JsonArray directly, you have to use a JsonBuffer.
Because the memory of a JsonArray is located a JsonBuffer, you always manipulate it through reference and you cannot copy it.
StaticJsonBuffer<200> jsonBuffer;
// create an empty array
JsonArray& array1 = jsonBuffer.createArray();
// parse a JSON array
char json[] = "[1,2,3]";
JsonArray& array2 = jsonBuffer.parseArray(json);Adds a value to the end of the array.
bool add(bool value);
bool add(float value, uint8_t decimals = 2);
bool add(double value, uint8_t decimals = 2);
bool add(signed char value);
bool add(signed long value);
bool add(signed int value);
bool add(signed short value);
bool add(unsigned char value);
bool add(unsigned long value);
bool add(unsigned int value);
bool add(unsigned short value);
bool add(const char *value);
bool add(const String &value); // see Remarks
bool add(JsonArray &array);
bool add(JsonObject &object);
bool add(const JsonVariant &variant);value: the value to add to the array.
decimals: the number of digits to print after the decimal point.
true if the value was successfully added.
false if there was not enough memory in the JsonBuffer.
When you call JsonArray::add(const String&), a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use JsonArray::add(const char*) instead.
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
array.add(3.14156, 4);
array.printTo(Serial);will write
["hello",3.1416]Adds a new nested array to the end of the array.
JsonArray& createNestedArray();A reference to the new JsonArray.
You can check JsonArray::success() to verify that the allocation succeeded.
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
JsonArray& nested = array.createNestedArray();
nested.add("world");
array.printTo(Serial);will write
["hello",["world"]]Adds a new nested object to the end of the array.
JsonObject& createNestedObject();A reference to the new JsonObject.
You can check JsonObject::success() to verify that the allocation succeeded.
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
JsonObject& nested = array.createNestedObject();
nested["hello"] = "world";
array.printTo(Serial);will write
[{"hello":"world"}]Gets the value at the specified index.
// Non template version
JsonVariant get (size_t index) const;
// Template version
bool get<bool> (size_t index) const;
const char* get<const char*> (size_t index) const;
double get<double> (size_t index) const;
float get<float> (size_t index) const;
signed char get<signed char> (size_t index) const;
signed int get<signed int> (size_t index) const;
signed long get<signed long> (size_t index) const;
signed short get<signed short> (size_t index) const;
String get<String> (size_t index) const;
unsigned char get<unsigned char> (size_t index) const;
unsigned int get<unsigned int> (size_t index) const;
unsigned long get<unsigned long> (size_t index) const;
unsigned short get<unsigned short> (size_t index) const;index: the index of the value in the array.
T: the type of the value
The value at the specified index. This can be a JsonVariant or a value of type T.
The template version of get() returns a value of the specified type.
In the case of an error (index out of range or incompatible type), the default value of the type T is returned.
char json[] = "[1,3.14]";
StaticJsonBuffer<256> jsonBuffer;
JsonArray& array = jsonBuffer.parseArray(json);
int value0 = array.get(0); // implicit cast of the JsonVariant
float value1 = array.get<float>(1); // template version of get()
const char* value2 = array.get(2); // returns NULLGets the length of string produced by JsonArray::printTo().
This can be very handy to fill the Content-Length of an HTTP request or response.
The number of characters in the JSON string that would be generated by JsonArray::printTo().
It doesn't include the zero-terminator.
size_t measureLength() constGets the length of string produced by JsonArray::prettyPrintTo().
size_t measurePrettyLength() constThe number of characters in the JSON string that would be generated by JsonArray::prettyPrintTo().
It doesn't include the zero-terminator.
A shortcut for JsonArray::get() and JsonArray::set(), with an array-like syntax.
JsonVariant& operator[](size_t index);
const JsonVariant& operator[](size_t index) const;index: the zero-based position of the value in the array.
A reference to the JsonVariant in the array.
JsonArray& array = jsonBuffer::createArray();
array.add(42);
int value = array[0];
array[0] = 666;Serialize the array to an indented JSON string.
This will create a "prettified" JSON, if you want a compact JSON without space or line break, use JsonArray::print()
size_t prettyPrintTo(char* buffer, size_t size) const;
size_t prettyPrintTo(Print &) const;
size_t prettyPrintTo(String &) const;The destination of the JSON string.
Can be either:
- a
bufferwith specifiedsize(this includes the zero-terminator) - an implementation of
Print(likeSerial,EthernetClient...) - a
String
The number of bytes written
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
array.add("world");
array.prettyPrintTo(Serial);will write the following string to the serial output:
[
"hello",
"world"
]Serialize the array to a JSON string.
This will create a compact JSON, if you want a pretty JSON with spaces and line breaks, use JsonArray::prettyPrint()
size_t printTo(char* buffer, size_t size) const;
size_t printTo(Print &) const;
size_t printTo(String &) const;The destination of the JSON string.
Can be either:
- a
bufferwith specifiedsize(this includes the zero-terminator) - an implementation of
Print(likeSerial,EthernetClient...) - a
String
The number of bytes written
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
array.add("world");
array.printTo(Serial);will write the following string to the serial output:
["hello","world"]Removes the element at the specified index.
IMPORTANT: This function doesn't free the memory allocated to the element in the JsonBuffer. This behavior has been prefered to keep the JsonBuffer fast and small, which is a foundational principle of the library.
void removeAt(size_t index);index: the zero-based position of the element in the array.
JsonArray& array = jsonBuffer.createArray();
array.add("A");
array.add("B");
array.add("C");
array.removeAt(1);
array.printTo(Serial);will print the following string to the serial output:
["A","C"]Sets the value at specified index.
bool set(size_t index, bool value);
bool set(size_t index, float value, uint8_t decimals = 2);
bool set(size_t index, double value, uint8_t decimals = 2);
bool set(size_t index, signed char value);
bool set(size_t index, signed long value);
bool set(size_t index, signed int value);
bool set(size_t index, signed short value);
bool set(size_t index, unsigned char value);
bool set(size_t index, unsigned long value);
bool set(size_t index, unsigned int value);
bool set(size_t index, unsigned short value);
bool set(size_t index, const char *value);
bool set(size_t index, const String &value); // see Remarks
bool set(size_t index, JsonArray &array);
bool set(size_t index, JsonObject &object);
bool set(size_t index, const JsonVariant &value);index: position to set value in array.
value: the value to set in index of array.
true if allocation succeeded.
false if there was not enough space left in the JsonBuffer, this can only happen when value is a String.
When you call JsonArray::set(size_t, const String&), a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use JsonArray::set(size_t, const char*) instead.
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.set(0,"hello");
array.add(1,3.14156, 4);
array.printTo(Serial); // [3.1416]Returns the number of element in the array.
size_t size() const;An unsigned integer containing the number of elements in the array.
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
array.add("world");
Serial.println(array.size()); // 2Tells if the array is valid, which can be used:
- to check if the array was successfully parsed, or
- to check if the array was successfully allocated.
bool success() const;true if the array was successfully parsed/allocated.
false if the parsing/allocation failed.
Example 1: parsing success:
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.parseArray("[1,2]");
Serial.println(array.success()); // trueExample 2: parsing failure:
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.parseArray("{1,2}");
Serial.println(array.success()); // falseExample 3: allocation success:
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
Serial.println(array.success()); // trueExample 4: allocation failure:
StaticJsonBuffer<1> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
Serial.println(array.success()); // falseJsonBuffer is the entry point for using the library: it handle the memory management and calls the parser.
It implements a speed efficient memory pool and comes in two flavors:
-
DynamicJsonBufferwhich is allocated on the heap and grows automatically -
StaticJsonBufferwhich is (most likely) allocated on the stack and has a fixed size.
JsonBuffer is optimized of fast allocation, but doesn't allow to free the allocated memory block.
To free a JsonBuffer, you must discard the entire object.
This is the source of a lot of question, please read the FAQ in the wiki for clarifications.
Allocates an empty JsonArray.
JsonArray& createArray();Returns a reference to the new JsonArray or JsonArray::invalid() if the allocation fails.
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("hello");
array.add("world");Allocates an empty JsonObject.
JsonObject createObject();Returns a reference to the new JsonObject or JsonObject::invalid() if the allocation fails.
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object["hello"] = "world";Allocates and populate a JsonArray from a JSON string.
By design, the parser needs to alter the string to insert null-terminators and replace escaped chars.
If the JSON string is read-only, it will have to duplicate the input string, this consume more space in the JsonBuffer.
Therefore, it's recommended to have a JSON input in a char[] or a char*.
JsonArray& parseArray(char* json, uint8_t nestingLimit=10); // <- recommended
JsonArray& parseArray(const char* json, uint8_t nestingLimit=10);
JsonArray& parseArray(const String& json, uint8_t nestingLimit=10);json is the input string to be parsed.
nestingLimit specifies the maximum level of nesting allowed in the JSON string.
If set to 0, only a flat array can be parsed.
If set to 1, the object can contain nested arrays or objects but only 1 level deep.
And bigger values will allow more level of nesting.
The purpose of this feature is to prevent stack overflow that could lead to a security risk.
Returns a reference to the new JsonArray or JsonArray::invalid() if the allocation fails.
char json[] = "[\"hello\",\"world\"]";
StaticJsonBuffer<200> jsonBuffer;
JsonArray& array = jsonBuffer.parseArray(json);
const char* hello = array[0];
const char* world = array[1];Allocates and populate a JsonObject from a JSON string.
By design, the parser needs to alter the string to insert null-terminators and replace escaped chars.
If the JSON string is read-only, it will have to duplicate the input string, this consume more space in the JsonBuffer.
Therefore, it's recommended to have a JSON input in a char[] or a char*.
JsonObject& parseObject(char* json, uint8_t nestingLimit=10); // <- recommended
JsonObject& parseObject(const char* json, uint8_t nestingLimit=10);
JsonObject& parseObject(const String& json, uint8_t nestingLimit=10);json is the input string to be parsed.
nestingLimit specifies the maximum level of nesting allowed in the JSON string.
If set to 0, only a flat object can be parsed.
If set to 1, the object can contain nested objects or objects but only 1 level deep.
And bigger values will allow more level of nesting.
The purpose of this feature is to prevent stack overflow that could lead to a security risk.
Returns a reference to the new JsonObject or JsonObject::invalid() if the allocation fails.
char json[] = "{\"hello\":\"world\"}";
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.parseObject(json);
const char* world = object["hello"];Make a copy of the specified string.
char* strdup(const char* str);
char* strdup(const String& str);str, the string to duplicate.
A newly allocate string, filled with a copy of str.
StaticJsonBuffer<200> jsonBuffer;
char orig[16] = "hello";
char* dupl = jsonBuffer.strdup(orig);
strcpy(orig, "world");
Serial.println(dupl); // world
Serial.println(orig); // helloRepresents an object (ie an unordered set of name/value pairs) in a JSON object tree.
The constructor is private, you cannot instanciate a JsonObject directly, you have to use a JsonBuffer.
Because the memory of a JsonObject is located a JsonBuffer, you always manipulate it through reference and you cannot copy it.
StaticJsonBuffer<200> jsonBuffer;
// create an empty object
JsonObject& object1 = jsonBuffer.createObject();
// parse a JSON object
char json[] = "[1,2,3]";
JsonObject& object2 = jsonBuffer.parseObject(json);Returns an iterator that can be use to get all key-value pairs in the object.
JsonObject::iterator begin();
JsonObject::iterator end();
JsonObject::const_iterator begin() const;
JsonObject::const_iterator end() const;A forward iterator pointing to a JsonPair, which itself contains two members key and value.
char json[] = "{\"first\":\"hello\",\"second\":\"world\"}";
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
// using C++98 syntax:
for (JsonObject::iterator it=root.begin(); it!=root.end(); ++it) {
Serial.println(it->key);
Serial.println(it->value);
}
// using C++11 syntax:
for (auto kv : root) {
Serial.println(kv.key);
Serial.println(kv.value);
}The code above would print:
first
hello
second
world
Creates a JsonArray as a child of the current object.
JsonArray& createNestedArray(const char* key) const;
JsonArray& createNestedArray(const String& key) const; // <- duplicates keykey: the key of the array in the object, can be a const char* or a const String&
When you add a value using a String for key, a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use a const char* key instead.
A reference to the new JsonArray.
You can check JsonArray::success() to verify that the allocatio succeeded.
StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["status"] = "on";
JsonArray& levels = root.createNestedArray("levels");
levels.add(10);
levels.add(30);
root.prettyPrintTo(Serial);will print
{
"status": "on",
"levels": [
10,
20
]
}Creates a JsonObject as a child of the current object.
JsonObject& createNestedObject(const char* key) const;
JsonObject& createNestedObject(const String& key) const; // <- duplicates keykey: the key of the object in the object, can be a const char* or a const String&
When you add a value using a String for key, a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use a const char* key instead.
A reference to the new JsonObject.
You can check JsonObject::success() to verify that the allocatio succeeded.
StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["city"] = "Paris";
JsonObject& weather = root.createNestedObject("weather");
weather["temp"] = 14.2;
weather["cond"] = "cloudy";
root.prettyPrintTo(Serial);will print
{
"city": "Paris",
"weather": {
"temp": 14.20,
"cond": "cloudy"
}
}Gets the value at the specified index.
// Non template version
JsonVariant get (JsonObjectKey key) const;
// Template version
bool get<bool> (JsonObjectKey key) const;
const char* get<const char*> (JsonObjectKey key) const;
double get<double> (JsonObjectKey key) const;
float get<float> (JsonObjectKey key) const;
signed char get<signed char> (JsonObjectKey key) const;
signed int get<signed int> (JsonObjectKey key) const;
signed long get<signed long> (JsonObjectKey key) const;
signed short get<signed short> (JsonObjectKey key) const;
String get<String> (JsonObjectKey key) const;
unsigned char get<unsigned char> (JsonObjectKey key) const;
unsigned int get<unsigned int> (JsonObjectKey key) const;
unsigned long get<unsigned long> (JsonObjectKey key) const;
unsigned short get<unsigned short> (JsonObjectKey key) const;key: the key of the value in the object, can be a const char* or a const String&
T: the type of the value
The value at the specified key. This can be a JsonVariant or a value of type T.
The template version of get() returns a value of the specified type.
In case of an error (key out of range or incompatible type), the default value of the type T is returned.
char json[] = "{\"pi\":3.14}";
StaticJsonBuffer<256> jsonBuffer;
JsonObject& object = jsonBuffer.parseObject(json);
float pi = object.get<float>("pi"); // template version of get()
const char* value2 = object.get<const char*>("toto"); // returns NULLGets the length of string produced by JsonObject::printTo().
This can be very handy to fill the Content-Length of an HTTP request or response.
The number of characters in the JSON string that would be generated by JsonObject::printTo().
It doesn't include the zero-terminator.
size_t measureLength() constGets the length of string produced by JsonObject::prettyPrintTo().
The number of characters in the JSON string that would be generated by JsonObject::prettyPrintTo().
It doesn't include the zero-terminator.
size_t measurePrettyLength() constA shortcut for JsonObject::get() and JsonObject::set(), with a map-like syntax.
JsonVariant& operator[](const char* key);
JsonVariant& operator[](const String& key);
const JsonVariant& operator[](const cahr* key) const;
const JsonVariant& operator[](const String& key) const;key: the key that the value will be associated to.
A reference to the JsonVariant in the object.
When you add a value using a String for key, a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use a const char* key instead.
JsonObject& object = jsonBuffer::createObject();
object["hello"] = "world";
const char* world = object["hello"];Serialize the object to an indented JSON string.
This will create a "prettified" JSON, if you want a compact JSON without space or line break, use JsonObject::print()
size_t prettyPrintTo(char* buffer, size_t size) const;
size_t prettyPrintTo(Print &) const;
size_t prettyPrintTo(String &) const;The destination of the JSON string.
Can be either:
- a
bufferwith specifiedsize(this includes the zero-terminator) - an implementation of
Print(likeSerial,EthernetClient...) - a
String
The number of bytes written
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object["hello"] = "world";
object.prettyPrintTo(Serial);will write the following string to the serial output:
{
"hello": "world"
}Serialize the object to a JSON string.
This will create a compact JSON, if you want a pretty JSON with spaces and line breaks, use JsonObject::prettyPrint()
size_t printTo(char* buffer, size_t size) const;
size_t printTo(Print &) const;
size_t printTo(String &) const;The destination of the JSON string.
Can be either:
- a
bufferwith specifiedsize(this includes the zero-terminator) - an implementation of
Print(likeSerial,EthernetClient...) - a
String
The number of bytes written
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object["hello"] = "world";
object.printTo(Serial);will write the following string to the serial output:
{"hello":"world"}Removes the element at the specified key.
IMPORTANT: This function doesn't free the memory allocated to the element in the JsonBuffer. This behavior has been prefered to keep the JsonBuffer fast and small, which is a foundational principle of the library.
void remove(const char* key);
void remove(const String& key);key: the key to remove from the object.
JsonObject& object = jsonBuffer.createObject();
object["A"] = 1;
object["B"] = 2;
object["C"] = 3;
object.remove("B");
object.printTo(Serial);will print the following string to the serial output:
{"A":1,"C":3}Sets the value at specified key.
bool set(const char* key, bool value);
bool set(const char* key, float value, uint8_t decimals = 2);
bool set(const char* key, double value, uint8_t decimals = 2);
bool set(const char* key, signed char value);
bool set(const char* key, signed long value);
bool set(const char* key, signed int value);
bool set(const char* key, signed short value);
bool set(const char* key, unsigned char value);
bool set(const char* key, unsigned long value);
bool set(const char* key, unsigned int value);
bool set(const char* key, unsigned short value);
bool set(const char* key, const char *value);
bool set(const char* key, const String &value); // see Remarks
bool set(const char* key, JsonArray &array);
bool set(const char* key, JsonObject &object);
bool set(const char* key, const JsonVariant &value);
bool set(const String& key, bool value); // see Remarks
bool set(const String& key, float value, uint8_t decimals = 2); // see Remarks
bool set(const String& key, double value, uint8_t decimals = 2); // see Remarks
bool set(const String& key, signed char value); // see Remarks
bool set(const String& key, signed long value); // see Remarks
bool set(const String& key, signed int value); // see Remarks
bool set(const String& key, signed short value); // see Remarks
bool set(const String& key, unsigned char value); // see Remarks
bool set(const String& key, unsigned long value); // see Remarks
bool set(const String& key, unsigned int value); // see Remarks
bool set(const String& key, unsigned short value); // see Remarks
bool set(const String& key, const char *value); // see Remarks
bool set(const String& key, const String &value); // see Remarks twice
bool set(const String& key, JsonArray &array); // see Remarks
bool set(const String& key, JsonObject &object); // see Remarks
bool set(const String& key, const JsonVariant &value); // see Remarkskey: the key to attach the value to.
value: the value to attach to the key.
true if allocation succeeded.
false if there was not enough space left in the JsonBuffer.
When you use a String, a copy of the string is made, causing the JsonBuffer to grow.
The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.
To avoid this behavior, use const char* keys and values instead.
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object.set("hello","world");
object.printTo(Serial);will print the following string to the serial output:
{"hello":"world"}Returns the number of key/value pairs in the object.
size_t size() const;An unsigned integer containing the number of key/value pairs in the object.
JsonObject& object = jsonBuffer.createObject();
object["hello"] = "world";
Serial.println(object.size()); // 1Tells if the object is valid, which can be used:
- to check if the object was successfully parsed, or
- to check if the object was successfully allocated.
bool success() const;true if the object was successfully parsed/allocated.
false if the parsing/allocation failed.
Example 1: parsing success:
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.parseObject("{\"hello\":\"world\"}");
Serial.println(object.success()); // trueExample 2: parsing failure:
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.parseObject("[\"hello\",\"world\"]");
Serial.println(object.success()); // falseExample 3: allocation success:
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
Serial.println(object.success()); // trueExample 4: allocation failure:
StaticJsonBuffer<1> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
Serial.println(object.success()); // falseA variable that can hold different type of values:
- a boolean
- a char, short, int or a long (signed or unsigned)
- a string (const char*)
- a reference to a JsonArray or JsonObject
A JsonVariant can be any of theses types at a time, but can only hold one value.
Its type can change at run time.
Get the value of the specified type.
bool as<bool>() const;
float as<float>() const;
double as<double>() const;
signed char as<signed char>() const;
unsigned char as<unsigned char>() const;
signed int as<signed int>() const;
unsigned int as<unsigned int>() const;
signed short as<signed short>() const;
unsigned short as<unsigned short>() const;
signed long as<signed long>() const;
unsigned long as<unsigned long>() const;
unsigned long long as<unsigned long long>() const; // <- may require ARDUINOJSON_USE_LONG_LONG
signed long long as<signed long long>() const; // <- may require ARDUINOJSON_USE_LONG_LONG
signed __int64 as<signed __int64>() const; // <- may require ARDUINOJSON_USE_INT64
unsigned __int64 as<unsigned __int64>() const; // <- may require ARDUINOJSON_USE_INT64
const char* as<char*>() const;
const char* as<const char*>() const;
String as<String>() const; // <- causes duplication of the string
JsonArray& as<JsonArray>() const;
JsonArray& as<JsonArray&>() const;
JsonArray& as<const JsonArray&>() const;
JsonObject& as<JsonObject>() const;
JsonObject& as<JsonObject&>() const;
JsonObject& as<const JsonObject&>() const;The value of the specified type or a default value if the JsonVariant is not compatible with the specified type.
The default value is:
-
0for numerical types -
NULLforconst char* -
JsonArray::invalid()forJsonArray& -
JsonObject::invalid()forJsonObject&
JsonVariant variant = 42;
int i = variant.as<int>(); // <- i == 42
double d = variant.as<double>(); // <- d == 42.0
const char* s = variant.as<char*>(); // <- s == NULLTest if the variant is currently holding a value of the specified type.
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<signed __int64>() const; // <- may require ARDUINOJSON_USE_INT64
bool is<unsigned __int64>() const; // <- may require ARDUINOJSON_USE_INT64
bool is<char*>() const;
bool is<const char*>() const;
bool is<JsonArray>() const;
bool is<JsonArray&>() const;
bool is<const JsonArray&>() const;
bool is<JsonObject>() const;
bool is<JsonObject&>() const;
bool is<const JsonObject&>() const;-
trueif the variant is currently holding a value of the specified type, -
falseif not
JsonVariant variant = 42;
bool i = variant.is<int>(); // <- i == true
bool d = variant.is<double>(); // <- d == false
bool s = variant.is<char*>(); // <- s == false