JsonVariant::getOrCreate()
⚠️ Subject to change
Version 6 is currently in beta, so the APIUnless you need the new features of version 6 (e.g. MessagePack), please use version 5.13.4.
Version 6 should stay in beta stage until march 2019.
Description
JsonVariant::getOrCreate()
reproduces JsonObject::getOrCreate()
; it only works if the JsonVariant
points an object.
-
If the
JsonVariant
points to an object,JsonVariant::getOrCreate()
gets the value associated with the specified key. If the key is not present in the object, this function adds a new key-value pair to the object. -
If the
JsonVariant
doesn’t point to an object, thisJsonVariant::getOrCreate()
does nothing.
Instead of this function, you can use JsonVariant::operator[]
which offers a more intuitive syntax.
Signatures
JsonVariant getOrCreate(const char* key) const;
JsonVariant getOrCreate(String key) const;
JsonVariant getOrCreate(std::string key) const;
JsonVariant getOrCreate(const __FlashStringHelper* key) const;
Arguments
key
: the key of the value in the object.
As usual, ArduinoJson makes a copy of the key in the JsonDocument
, except if it’s a const char*
.
Return value
If the key is already present in the object, this function returns a JsonVariant
that points to the associated value.
If the key is not present in the object, this function adds a new key-value pair to the object and returns a JsonVariant
that points to the value. The returned JsonVariant
could be null if there were not enough room in the JsonDocument
.