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