forked from bblanchon/ArduinoJson
@ -5,6 +5,8 @@ ArduinoJson: change log
|
||||
* Add MsgPack bin8/bin16/bin32 support (PR #2078 by @Sanae6)
|
||||
* Make string support even more generic (PR #2084 by @d-a-v)
|
||||
* Optimize `deserializeMsgPack()`
|
||||
* Allow using a `JsonVariant` as a key or index (issue #2080)
|
||||
Note: works only for reading, not for writing
|
||||
|
||||
v7.0.4 (2024-03-12)
|
||||
------
|
||||
|
@ -88,6 +88,15 @@ TEST_CASE("JsonArray::remove()") {
|
||||
JsonArray unboundArray;
|
||||
unboundArray.remove(unboundArray.begin());
|
||||
}
|
||||
|
||||
SECTION("use JsonVariant as index") {
|
||||
array.remove(array[3]); // no effect with null variant
|
||||
array.remove(array[0]); // remove element at index 1
|
||||
|
||||
REQUIRE(2 == array.size());
|
||||
REQUIRE(array[0] == 1);
|
||||
REQUIRE(array[1] == 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Removed elements are recycled") {
|
||||
|
@ -164,4 +164,13 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("Use a JsonVariant as index") {
|
||||
array[0] = 1;
|
||||
array[1] = 2;
|
||||
array[2] = 3;
|
||||
|
||||
REQUIRE(array[array[1]] == 3);
|
||||
REQUIRE(array[array[3]] == nullptr);
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,15 @@ TEST_CASE("JsonArrayConst::operator[]") {
|
||||
doc.add(2);
|
||||
doc.add(3);
|
||||
|
||||
REQUIRE(1 == arr[0].as<int>());
|
||||
REQUIRE(2 == arr[1].as<int>());
|
||||
REQUIRE(3 == arr[2].as<int>());
|
||||
REQUIRE(0 == arr[3].as<int>());
|
||||
SECTION("int") {
|
||||
REQUIRE(1 == arr[0].as<int>());
|
||||
REQUIRE(2 == arr[1].as<int>());
|
||||
REQUIRE(3 == arr[2].as<int>());
|
||||
REQUIRE(0 == arr[3].as<int>());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
REQUIRE(2 == arr[arr[0]].as<int>());
|
||||
REQUIRE(0 == arr[arr[3]].as<int>());
|
||||
}
|
||||
}
|
||||
|
@ -41,4 +41,12 @@ TEST_CASE("JsonDocument::containsKey()") {
|
||||
SECTION("returns false on null") {
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("support JsonVariant") {
|
||||
doc["hello"] = "world";
|
||||
doc["key"] = "hello";
|
||||
|
||||
REQUIRE(doc.containsKey(doc["key"]) == true);
|
||||
REQUIRE(doc.containsKey(doc["foo"]) == false);
|
||||
}
|
||||
}
|
||||
|
@ -49,4 +49,25 @@ TEST_CASE("JsonDocument::remove()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("remove(JsonVariant) from object") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
doc["c"] = "b";
|
||||
|
||||
doc.remove(doc["c"]);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1,\"c\":\"b\"}");
|
||||
}
|
||||
|
||||
SECTION("remove(JsonVariant) from array") {
|
||||
doc[0] = 3;
|
||||
doc[1] = 2;
|
||||
doc[2] = 1;
|
||||
|
||||
doc.remove(doc[2]);
|
||||
doc.remove(doc[3]); // noop
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[3,1]");
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,12 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
REQUIRE(cdoc[std::string("hello")] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(doc[doc["key"]] == "world");
|
||||
REQUIRE(cdoc[cdoc["key"]] == "world");
|
||||
}
|
||||
|
||||
SECTION("supports operator|") {
|
||||
REQUIRE((doc["hello"] | "nope") == std::string("world"));
|
||||
REQUIRE((doc["world"] | "nope") == std::string("nope"));
|
||||
@ -31,8 +37,16 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
SECTION("array") {
|
||||
deserializeJson(doc, "[\"hello\",\"world\"]");
|
||||
|
||||
REQUIRE(doc[1] == "world");
|
||||
REQUIRE(cdoc[1] == "world");
|
||||
SECTION("int") {
|
||||
REQUIRE(doc[1] == "world");
|
||||
REQUIRE(cdoc[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
doc[2] = 1;
|
||||
REQUIRE(doc[doc[2]] == "world");
|
||||
REQUIRE(cdoc[doc[2]] == "world");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,4 +30,10 @@ TEST_CASE("JsonObject::containsKey()") {
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("key is a JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(true == obj.containsKey(obj["key"]));
|
||||
REQUIRE(false == obj.containsKey(obj["hello"]));
|
||||
}
|
||||
}
|
||||
|
@ -80,4 +80,10 @@ TEST_CASE("JsonObject::remove()") {
|
||||
JsonObject unboundObject;
|
||||
unboundObject.remove(unboundObject.begin());
|
||||
}
|
||||
|
||||
SECTION("remove(JsonVariant)") {
|
||||
obj["key"] = "b";
|
||||
obj.remove(obj["key"]);
|
||||
REQUIRE("{\"a\":0,\"c\":2,\"key\":\"b\"}" == doc.as<std::string>());
|
||||
}
|
||||
}
|
||||
|
@ -249,4 +249,12 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
REQUIRE(true == obj["hello"]["world"].is<int>());
|
||||
REQUIRE(false == obj["hello"]["world"].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
obj["hello"] = "world";
|
||||
doc["key"] = "hello";
|
||||
|
||||
REQUIRE(obj[obj["key"]] == "world");
|
||||
REQUIRE(obj[obj["foo"]] == nullptr);
|
||||
}
|
||||
}
|
||||
|
@ -29,4 +29,10 @@ TEST_CASE("JsonObjectConst::containsKey()") {
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("supports JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(true == obj.containsKey(obj["key"]));
|
||||
REQUIRE(false == obj.containsKey(obj["hello"]));
|
||||
}
|
||||
}
|
||||
|
@ -30,4 +30,10 @@ TEST_CASE("JsonObjectConst::operator[]") {
|
||||
REQUIRE(std::string("world") == obj[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("supports JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(obj[obj["key"]] == "world");
|
||||
REQUIRE(obj[obj["foo"]] == nullptr);
|
||||
}
|
||||
}
|
||||
|
@ -23,4 +23,12 @@ TEST_CASE("JsonVariant::containsKey()") {
|
||||
REQUIRE(var.containsKey(std::string("hello")) == true);
|
||||
REQUIRE(var.containsKey(std::string("world")) == false);
|
||||
}
|
||||
|
||||
SECTION("containsKey(JsonVariant)") {
|
||||
var["hello"] = "world";
|
||||
var["key"] = "hello";
|
||||
|
||||
REQUIRE(var.containsKey(doc["key"]) == true);
|
||||
REQUIRE(var.containsKey(doc["foo"]) == false);
|
||||
}
|
||||
}
|
||||
|
@ -81,3 +81,30 @@ TEST_CASE("JsonVariant::remove(std::string)") {
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
var["a"] = "a";
|
||||
var["b"] = 2;
|
||||
var["c"] = "b";
|
||||
|
||||
var.remove(var["c"]);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"a\":\"a\",\"c\":\"b\"}");
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::remove(JsonVariant) from array") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
var[0] = 3;
|
||||
var[1] = 2;
|
||||
var[2] = 1;
|
||||
|
||||
var.remove(var[2]);
|
||||
var.remove(var[3]); // noop
|
||||
|
||||
REQUIRE(var.as<std::string>() == "[3,1]");
|
||||
}
|
||||
|
@ -67,6 +67,15 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
REQUIRE(var.is<int>());
|
||||
REQUIRE(var.as<int>() == 123);
|
||||
}
|
||||
|
||||
SECTION("use JsonVariant as index") {
|
||||
array.add("A");
|
||||
array.add("B");
|
||||
array.add(1);
|
||||
|
||||
REQUIRE(var[var[2]] == "B");
|
||||
REQUIRE(var[var[3]].isNull());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("The JsonVariant is a JsonObject") {
|
||||
@ -103,6 +112,15 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
JsonArray arr = var["hello"].to<JsonArray>();
|
||||
REQUIRE(arr.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("use JsonVariant as key") {
|
||||
object["a"] = "a";
|
||||
object["b"] = "b";
|
||||
object["c"] = "b";
|
||||
|
||||
REQUIRE(var[var["c"]] == "b");
|
||||
REQUIRE(var[var["d"]].isNull());
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
|
@ -30,4 +30,10 @@ TEST_CASE("JsonVariantConst::containsKey()") {
|
||||
REQUIRE(true == var.containsKey(vla));
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("support JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(var.containsKey(var["key"]) == true);
|
||||
REQUIRE(var.containsKey(var["foo"]) == false);
|
||||
}
|
||||
}
|
||||
|
@ -27,13 +27,24 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
array.add("A");
|
||||
array.add("B");
|
||||
|
||||
REQUIRE(std::string("A") == var[0]);
|
||||
REQUIRE(std::string("B") == var[1]);
|
||||
REQUIRE(std::string("A") ==
|
||||
var[static_cast<unsigned char>(0)]); // issue #381
|
||||
REQUIRE(var[666].isNull());
|
||||
REQUIRE(var[3].isNull());
|
||||
REQUIRE(var["0"].isNull());
|
||||
SECTION("int") {
|
||||
REQUIRE(std::string("A") == var[0]);
|
||||
REQUIRE(std::string("B") == var[1]);
|
||||
REQUIRE(std::string("A") ==
|
||||
var[static_cast<unsigned char>(0)]); // issue #381
|
||||
REQUIRE(var[666].isNull());
|
||||
REQUIRE(var[3].isNull());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
REQUIRE(var["0"].isNull());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
array.add(1);
|
||||
REQUIRE(var[var[2]] == std::string("B"));
|
||||
REQUIRE(var[var[3]].isNull());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("object") {
|
||||
@ -64,5 +75,11 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
REQUIRE(std::string("A") == var[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("supports JsonVariant") {
|
||||
object["c"] = "b";
|
||||
REQUIRE(var[var["c"]] == "B");
|
||||
REQUIRE(var[var["d"]].isNull());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -114,6 +114,15 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
detail::ArrayData::removeElement(data_, index, resources_);
|
||||
}
|
||||
|
||||
// Removes the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarray/remove/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value>::type remove(
|
||||
TVariant variant) const {
|
||||
if (variant.template is<size_t>())
|
||||
remove(variant.template as<size_t>());
|
||||
}
|
||||
|
||||
// Removes all the elements of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/clear/
|
||||
void clear() const {
|
||||
@ -122,8 +131,23 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
|
||||
// Gets or sets the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarray/subscript/
|
||||
detail::ElementProxy<JsonArray> operator[](size_t index) const {
|
||||
return {*this, index};
|
||||
template <typename T>
|
||||
typename detail::enable_if<detail::is_integral<T>::value,
|
||||
detail::ElementProxy<JsonArray>>::type
|
||||
operator[](T index) const {
|
||||
return {*this, size_t(index)};
|
||||
}
|
||||
|
||||
// Gets or sets the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarray/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
detail::ElementProxy<JsonArray>>::type
|
||||
operator[](const TVariant& variant) const {
|
||||
if (variant.template is<size_t>())
|
||||
return operator[](variant.template as<size_t>());
|
||||
else
|
||||
return {*this, size_t(-1)};
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
|
@ -45,9 +45,25 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
|
||||
// Returns the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
|
||||
JsonVariantConst operator[](size_t index) const {
|
||||
template <typename T>
|
||||
typename detail::enable_if<detail::is_integral<T>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](T index) const {
|
||||
return JsonVariantConst(
|
||||
detail::ArrayData::getElement(data_, index, resources_), resources_);
|
||||
detail::ArrayData::getElement(data_, size_t(index), resources_),
|
||||
resources_);
|
||||
}
|
||||
|
||||
// Returns the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](const TVariant& variant) const {
|
||||
if (variant.template is<size_t>())
|
||||
return operator[](variant.template as<size_t>());
|
||||
else
|
||||
return JsonVariantConst();
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
|
@ -163,10 +163,19 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// Returns true if the root object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsondocument/containskey/
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
||||
containsKey(const TString& key) const {
|
||||
return data_.getMember(detail::adaptString(key), &resources_) != 0;
|
||||
}
|
||||
|
||||
// Returns true if the root object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsondocument/containskey/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value, bool>::type
|
||||
containsKey(const TVariant& key) const {
|
||||
return containsKey(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// Gets or sets a root object's member.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
template <typename TString>
|
||||
@ -207,8 +216,11 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
|
||||
// Gets or sets a root array's element.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
detail::ElementProxy<JsonDocument&> operator[](size_t index) {
|
||||
return {*this, index};
|
||||
template <typename T>
|
||||
typename detail::enable_if<detail::is_integral<T>::value,
|
||||
detail::ElementProxy<JsonDocument&>>::type
|
||||
operator[](T index) {
|
||||
return {*this, size_t(index)};
|
||||
}
|
||||
|
||||
// Gets a root array's member.
|
||||
@ -217,6 +229,19 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
return JsonVariantConst(data_.getElement(index, &resources_), &resources_);
|
||||
}
|
||||
|
||||
// Gets or sets a root object's member.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](const TVariant& key) const {
|
||||
if (key.template is<const char*>())
|
||||
return operator[](key.template as<const char*>());
|
||||
if (key.template is<size_t>())
|
||||
return operator[](key.template as<size_t>());
|
||||
return {};
|
||||
}
|
||||
|
||||
// Appends a new (empty) element to the root array.
|
||||
// Returns a reference to the new element.
|
||||
// https://arduinojson.org/v7/api/jsondocument/add/
|
||||
@ -251,8 +276,11 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
|
||||
// Removes an element of the root array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/remove/
|
||||
void remove(size_t index) {
|
||||
detail::VariantData::removeElement(getData(), index, getResourceManager());
|
||||
template <typename T>
|
||||
typename detail::enable_if<detail::is_integral<T>::value>::type remove(
|
||||
T index) {
|
||||
detail::VariantData::removeElement(getData(), size_t(index),
|
||||
getResourceManager());
|
||||
}
|
||||
|
||||
// Removes a member of the root object.
|
||||
@ -267,13 +295,23 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// Removes a member of the root object.
|
||||
// https://arduinojson.org/v7/api/jsondocument/remove/
|
||||
template <typename TString>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TString>::value>::type remove(
|
||||
const TString& key) {
|
||||
detail::VariantData::removeMember(getData(), detail::adaptString(key),
|
||||
getResourceManager());
|
||||
}
|
||||
|
||||
// Removes a member of the root object or an element of the root array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/remove/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value>::type remove(
|
||||
const TVariant& key) {
|
||||
if (key.template is<const char*>())
|
||||
remove(key.template as<const char*>());
|
||||
if (key.template is<size_t>())
|
||||
remove(key.template as<size_t>());
|
||||
}
|
||||
|
||||
operator JsonVariant() {
|
||||
return getVariant();
|
||||
}
|
||||
|
@ -102,7 +102,6 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Gets or sets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/subscript/
|
||||
template <typename TString>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TString>::value,
|
||||
detail::MemberProxy<JsonObject, TString>>::type
|
||||
operator[](const TString& key) const {
|
||||
@ -112,13 +111,24 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Gets or sets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/subscript/
|
||||
template <typename TChar>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TChar*>::value,
|
||||
detail::MemberProxy<JsonObject, TChar*>>::type
|
||||
operator[](TChar* key) const {
|
||||
return {*this, key};
|
||||
}
|
||||
|
||||
// Gets or sets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
detail::MemberProxy<JsonObject, const char*>>::type
|
||||
operator[](const TVariant& key) const {
|
||||
if (key.template is<const char*>())
|
||||
return {*this, key.template as<const char*>()};
|
||||
else
|
||||
return {*this, nullptr};
|
||||
}
|
||||
|
||||
// Removes the member at the specified iterator.
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
FORCE_INLINE void remove(iterator it) const {
|
||||
@ -128,11 +138,21 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Removes the member with the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
template <typename TString>
|
||||
FORCE_INLINE void remove(const TString& key) const {
|
||||
typename detail::enable_if<detail::IsString<TString>::value>::type remove(
|
||||
const TString& key) const {
|
||||
detail::ObjectData::removeMember(data_, detail::adaptString(key),
|
||||
resources_);
|
||||
}
|
||||
|
||||
// Removes the member with the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value>::type remove(
|
||||
const TVariant& key) const {
|
||||
if (key.template is<const char*>())
|
||||
remove(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// Removes the member with the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
template <typename TChar>
|
||||
@ -144,7 +164,6 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/containskey/
|
||||
template <typename TString>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
||||
containsKey(const TString& key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
@ -154,13 +173,20 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/containskey/
|
||||
template <typename TChar>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TChar*>::value, bool>::type
|
||||
containsKey(TChar* key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
}
|
||||
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/containskey/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value, bool>::type
|
||||
containsKey(const TVariant& key) const {
|
||||
return containsKey(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].to<JsonArray>() instead
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use obj[key].to<JsonArray>() instead")
|
||||
|
@ -71,7 +71,8 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/containskey/
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
||||
containsKey(const TString& key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
}
|
||||
@ -84,6 +85,14 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
resources_) != 0;
|
||||
}
|
||||
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/containskey/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value, bool>::type
|
||||
containsKey(const TVariant& key) const {
|
||||
return containsKey(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/subscript/
|
||||
template <typename TString>
|
||||
@ -106,6 +115,18 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
resources_);
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](const TVariant& key) const {
|
||||
if (key.template is<const char*>())
|
||||
return operator[](key.template as<const char*>());
|
||||
else
|
||||
return JsonVariantConst();
|
||||
}
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -14,7 +14,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
template <typename T>
|
||||
struct is_enum {
|
||||
static const bool value = is_convertible<T, int>::value &&
|
||||
static const bool value = is_convertible<T, long long>::value &&
|
||||
!is_class<T>::value && !is_integral<T>::value &&
|
||||
!is_floating_point<T>::value;
|
||||
};
|
||||
|
@ -86,9 +86,13 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
|
||||
// Gets array's element at specified index.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/subscript/
|
||||
JsonVariantConst operator[](size_t index) const {
|
||||
template <typename T>
|
||||
typename detail::enable_if<detail::is_integral<T>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](T index) const {
|
||||
return JsonVariantConst(
|
||||
detail::VariantData::getElement(data_, index, resources_), resources_);
|
||||
detail::VariantData::getElement(data_, size_t(index), resources_),
|
||||
resources_);
|
||||
}
|
||||
|
||||
// Gets object's member with specified key.
|
||||
@ -113,10 +117,22 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
resources_);
|
||||
}
|
||||
|
||||
// Gets object's member with specified key or the array's element at the
|
||||
// specified index.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/subscript/
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value,
|
||||
JsonVariantConst>::type
|
||||
operator[](const TVariant& key) const {
|
||||
if (key.template is<size_t>())
|
||||
return operator[](key.template as<size_t>());
|
||||
else
|
||||
return operator[](key.template as<const char*>());
|
||||
}
|
||||
|
||||
// Returns true if tge object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/containskey/
|
||||
template <typename TString>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
||||
containsKey(const TString& key) const {
|
||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
||||
@ -126,13 +142,18 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
// Returns true if tge object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/containskey/
|
||||
template <typename TChar>
|
||||
|
||||
typename detail::enable_if<detail::IsString<TChar*>::value, bool>::type
|
||||
containsKey(TChar* key) const {
|
||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
}
|
||||
|
||||
template <typename TVariant>
|
||||
typename detail::enable_if<detail::IsVariant<TVariant>::value, bool>::type
|
||||
containsKey(const TVariant& key) const {
|
||||
return containsKey(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -169,6 +169,17 @@ class VariantRefBase : public VariantTag {
|
||||
getResourceManager());
|
||||
}
|
||||
|
||||
// Removes a member of the object or an element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||
template <typename TVariant>
|
||||
typename enable_if<IsVariant<TVariant>::value>::type remove(
|
||||
const TVariant& key) const {
|
||||
if (key.template is<size_t>())
|
||||
remove(key.template as<size_t>());
|
||||
else
|
||||
remove(key.template as<const char*>());
|
||||
}
|
||||
|
||||
// Gets or sets an array element.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/subscript/
|
||||
ElementProxy<TDerived> operator[](size_t index) const;
|
||||
@ -185,6 +196,12 @@ class VariantRefBase : public VariantTag {
|
||||
typename enable_if<IsString<TChar*>::value, bool>::type containsKey(
|
||||
TChar* key) const;
|
||||
|
||||
// Returns true if the object contains the specified key.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/containskey/
|
||||
template <typename TVariant>
|
||||
typename enable_if<IsVariant<TVariant>::value, bool>::type containsKey(
|
||||
const TVariant& key) const;
|
||||
|
||||
// Gets or sets an object member.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/subscript/
|
||||
template <typename TString>
|
||||
@ -199,6 +216,17 @@ class VariantRefBase : public VariantTag {
|
||||
MemberProxy<TDerived, TChar*>>::type
|
||||
operator[](TChar* key) const;
|
||||
|
||||
// Gets an object member or an array element.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/subscript/
|
||||
template <typename TVariant>
|
||||
typename enable_if<IsVariant<TVariant>::value, JsonVariantConst>::type
|
||||
operator[](const TVariant& key) const {
|
||||
if (key.template is<size_t>())
|
||||
return operator[](key.template as<size_t>());
|
||||
else
|
||||
return operator[](key.template as<const char*>());
|
||||
}
|
||||
|
||||
// DEPRECATED: use add<JsonVariant>() instead
|
||||
ARDUINOJSON_DEPRECATED("use add<JsonVariant>() instead")
|
||||
JsonVariant add() const;
|
||||
|
@ -90,6 +90,13 @@ VariantRefBase<TDerived>::containsKey(TChar* key) const {
|
||||
getResourceManager()) != 0;
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TVariant>
|
||||
inline typename enable_if<IsVariant<TVariant>::value, bool>::type
|
||||
VariantRefBase<TDerived>::containsKey(const TVariant& key) const {
|
||||
return containsKey(key.template as<const char*>());
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline JsonVariant VariantRefBase<TDerived>::getVariant() const {
|
||||
return JsonVariant(getData(), getResourceManager());
|
||||
|
Reference in New Issue
Block a user