JsonVariant::add()
Description
When the JsonVariant
points to an array, JsonVariant::add()
appends a value to the array. Otherwise, this function does nothing.
In other words, JsonVariant::add()
reproduces JsonArray::add()
.
Signatures
bool add(bool value) const;
bool add(float value) const;
bool add(double value) const;
bool add(signed char value) const;
bool add(signed long value) const;
bool add(signed int value) const;
bool add(signed short value) const;
bool add(unsigned char value) const;
bool add(unsigned long value) const;
bool add(unsigned int value) const;
bool add(unsigned short value) const;
bool add(char *value) const;
bool add(const char *value) const; // ⚠️ stores by pointer
bool add(const __FlashStringHelper *value) const;
bool add(const String &value) const;
bool add(const std::string &value) const;
bool add(const Printable& value);
bool add(std::string_view value);
bool add(JsonArray array) const;
bool add(JsonObject object) const;
bool add(JsonVariant variant) const;
bool add(const JsonDocument& doc) const;
bool add(TEnum value) const; // alias of add(int)
bool add(T value) const; // calls user-defined function
JsonArray add<JsonArray>() const; // 🆕 adds a new empty array
JsonObject add<JsonObject>() const; // 🆕 adds a new empty object
JsonVariant add<JsonVariant>() const; // 🆕 adds a new null variant
Arguments
value
: the value to append to the array.
If you pass a JsonArray
, a JsonObject
, or a JsonVariant
, JsonVariant::add()
makes a complete clone of the argument. In other words, the value is stored by copy, not by reference.
As usual, ArduinoJson makes a copy of a string in the JsonDocument
, except if it’s a const char*
.
Return value
JsonVariant::add()
returns a bool
that tells whether the operation was successful or not:
true
if the value was successfully added.false
if there was not enough memory in theJsonDocument
.
String duplication
ArduinoJson makes a copy of the string when you call this function with one of the following types:
char*
String
(orstd::string
)const __FlashStringHelper*
(i.e. Flash string)
User-defined types
JsonVariant::add()
supports user-defined types by calling convertToJson()
.
For example, to support tm
, you must define:
bool convertToJson(const tm& src, JsonVariant dst) {
char buf[32];
strftime(buf, sizeof(buf), "%FT%TZ", &src);
return dst.set(buf);
}