Remove createNestedArray() and createNestedObject()

This commit is contained in:
Benoit Blanchon
2023-08-09 11:31:29 +02:00
parent cdc1262127
commit 7f459adc4b
37 changed files with 99 additions and 547 deletions

View File

@ -28,3 +28,4 @@ HEAD
* Call `shrinkToFit()` in `deserializeJson()` and `deserializeMsgPack()` * Call `shrinkToFit()` in `deserializeJson()` and `deserializeMsgPack()`
* `serializeJson()` and `serializeMsgPack()` replace the content of `std::string` and `String` instead of appending to it * `serializeJson()` and `serializeMsgPack()` replace the content of `std::string` and `String` instead of appending to it
* Replace `add()` with `add<T>()` (`add(T)` is still supported) * Replace `add()` with `add<T>()` (`add(T)` is still supported)
* Remove `createNestedArray()` and `createNestedObject()` (use `to<JsonArray>()` and `to<JsonObject>()` instead)

View File

@ -22,7 +22,7 @@ void setup() {
doc["time"] = 1351824120; doc["time"] = 1351824120;
// Add an array. // Add an array.
JsonArray data = doc.createNestedArray("data"); JsonArray data = doc["data"].to<JsonArray>();
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);

View File

@ -60,7 +60,7 @@ void loop() {
JsonDocument doc; JsonDocument doc;
// Create the "analog" array // Create the "analog" array
JsonArray analogValues = doc.createNestedArray("analog"); JsonArray analogValues = doc["analog"].to<JsonArray>();
for (int pin = 0; pin < 6; pin++) { for (int pin = 0; pin < 6; pin++) {
// Read the analog input // Read the analog input
int value = analogRead(pin); int value = analogRead(pin);
@ -70,7 +70,7 @@ void loop() {
} }
// Create the "digital" array // Create the "digital" array
JsonArray digitalValues = doc.createNestedArray("digital"); JsonArray digitalValues = doc["digital"].to<JsonArray>();
for (int pin = 0; pin < 14; pin++) { for (int pin = 0; pin < 14; pin++) {
// Read the digital input // Read the digital input
int value = digitalRead(pin); int value = digitalRead(pin);

View File

@ -50,7 +50,7 @@ void loop() {
JsonDocument doc; JsonDocument doc;
// Create the "analog" array // Create the "analog" array
JsonArray analogValues = doc.createNestedArray("analog"); JsonArray analogValues = doc["analog"].to<JsonArray>();
for (int pin = 0; pin < 6; pin++) { for (int pin = 0; pin < 6; pin++) {
// Read the analog input // Read the analog input
int value = analogRead(pin); int value = analogRead(pin);
@ -60,7 +60,7 @@ void loop() {
} }
// Create the "digital" array // Create the "digital" array
JsonArray digitalValues = doc.createNestedArray("digital"); JsonArray digitalValues = doc["digital"].to<JsonArray>();
for (int pin = 0; pin < 14; pin++) { for (int pin = 0; pin < 14; pin++) {
// Read the digital input // Read the digital input
int value = digitalRead(pin); int value = digitalRead(pin);

View File

@ -22,7 +22,7 @@ int main() {
// Add an array. // Add an array.
// //
JsonArray data = doc.createNestedArray("data"); JsonArray data = doc["data"].to<JsonArray>();
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);

View File

@ -7,7 +7,6 @@ add_executable(JsonArrayTests
clear.cpp clear.cpp
compare.cpp compare.cpp
copyArray.cpp copyArray.cpp
createNested.cpp
equals.cpp equals.cpp
isNull.cpp isNull.cpp
iterator.cpp iterator.cpp

View File

@ -43,15 +43,15 @@ TEST_CASE("Compare JsonArray with JsonArray") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello"); array1.add("hello");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello"); array2.add("hello");
array2.createNestedObject(); array2.add<JsonObject>();
CHECK(array1 == array2); CHECK(array1 == array2);
CHECK(array1 <= array2); CHECK(array1 <= array2);
@ -62,15 +62,15 @@ TEST_CASE("Compare JsonArray with JsonArray") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello1"); array1.add("hello1");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello2"); array2.add("hello2");
array2.createNestedObject(); array2.add<JsonObject>();
CHECK(array1 != array2); CHECK(array1 != array2);
CHECK_FALSE(array1 == array2); CHECK_FALSE(array1 == array2);
@ -107,15 +107,15 @@ TEST_CASE("Compare JsonArray with JsonVariant") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array = doc.createNestedArray(); JsonArray array = doc.add<JsonArray>();
array.add(1); array.add(1);
array.add("hello"); array.add("hello");
array.createNestedObject(); array.add<JsonObject>();
JsonVariant variant = doc.createNestedArray(); JsonVariant variant = doc.add<JsonArray>();
variant.add(1); variant.add(1);
variant.add("hello"); variant.add("hello");
variant.createNestedObject(); variant.add<JsonObject>();
CHECK(array == variant); CHECK(array == variant);
CHECK(array <= variant); CHECK(array <= variant);
@ -133,15 +133,15 @@ TEST_CASE("Compare JsonArray with JsonVariant") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array = doc.createNestedArray(); JsonArray array = doc.add<JsonArray>();
array.add(1); array.add(1);
array.add("hello1"); array.add("hello1");
array.createNestedObject(); array.add<JsonObject>();
JsonVariant variant = doc.createNestedArray(); JsonVariant variant = doc.add<JsonArray>();
variant.add(1); variant.add(1);
variant.add("hello2"); variant.add("hello2");
variant.createNestedObject(); variant.add<JsonObject>();
CHECK(array != variant); CHECK(array != variant);
CHECK_FALSE(array == variant); CHECK_FALSE(array == variant);
@ -199,15 +199,15 @@ TEST_CASE("Compare JsonArray with JsonVariantConst") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array = doc.createNestedArray(); JsonArray array = doc.add<JsonArray>();
array.add(1); array.add(1);
array.add("hello"); array.add("hello");
array.createNestedObject(); array.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello"); array2.add("hello");
array2.createNestedObject(); array2.add<JsonObject>();
JsonVariantConst variant = array2; JsonVariantConst variant = array2;
CHECK(array == variant); CHECK(array == variant);
@ -226,15 +226,15 @@ TEST_CASE("Compare JsonArray with JsonVariantConst") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array = doc.createNestedArray(); JsonArray array = doc.add<JsonArray>();
array.add(1); array.add(1);
array.add("hello1"); array.add("hello1");
array.createNestedObject(); array.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello2"); array2.add("hello2");
array2.createNestedObject(); array2.add<JsonObject>();
JsonVariantConst variant = array2; JsonVariantConst variant = array2;
CHECK(array != variant); CHECK(array != variant);
@ -292,15 +292,15 @@ TEST_CASE("Compare JsonArray with JsonArrayConst") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello"); array1.add("hello");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello"); array2.add("hello");
array2.createNestedObject(); array2.add<JsonObject>();
JsonArrayConst carray2 = array2; JsonArrayConst carray2 = array2;
CHECK(array1 == carray2); CHECK(array1 == carray2);
@ -319,15 +319,15 @@ TEST_CASE("Compare JsonArray with JsonArrayConst") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello1"); array1.add("hello1");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello2"); array2.add("hello2");
array2.createNestedObject(); array2.add<JsonObject>();
JsonArrayConst carray2 = array2; JsonArrayConst carray2 = array2;
CHECK(array1 != carray2); CHECK(array1 != carray2);
@ -387,16 +387,16 @@ TEST_CASE("Compare JsonArrayConst with JsonArrayConst") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello"); array1.add("hello");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArrayConst carray1 = array1; JsonArrayConst carray1 = array1;
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello"); array2.add("hello");
array2.createNestedObject(); array2.add<JsonObject>();
JsonArrayConst carray2 = array2; JsonArrayConst carray2 = array2;
CHECK(carray1 == carray2); CHECK(carray1 == carray2);
@ -408,16 +408,16 @@ TEST_CASE("Compare JsonArrayConst with JsonArrayConst") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello1"); array1.add("hello1");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArrayConst carray1 = array1; JsonArrayConst carray1 = array1;
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello2"); array2.add("hello2");
array2.createNestedObject(); array2.add<JsonObject>();
JsonArrayConst carray2 = array2; JsonArrayConst carray2 = array2;
CHECK(carray1 != carray2); CHECK(carray1 != carray2);
@ -455,16 +455,16 @@ TEST_CASE("Compare JsonArrayConst with JsonVariant") {
} }
SECTION("Compare with identical array") { SECTION("Compare with identical array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello"); array1.add("hello");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArrayConst carray1 = array1; JsonArrayConst carray1 = array1;
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello"); array2.add("hello");
array2.createNestedObject(); array2.add<JsonObject>();
JsonVariant variant2 = array2; JsonVariant variant2 = array2;
CHECK(carray1 == variant2); CHECK(carray1 == variant2);
@ -483,16 +483,16 @@ TEST_CASE("Compare JsonArrayConst with JsonVariant") {
} }
SECTION("Compare with different array") { SECTION("Compare with different array") {
JsonArray array1 = doc.createNestedArray(); JsonArray array1 = doc.add<JsonArray>();
array1.add(1); array1.add(1);
array1.add("hello1"); array1.add("hello1");
array1.createNestedObject(); array1.add<JsonObject>();
JsonArrayConst carray1 = array1; JsonArrayConst carray1 = array1;
JsonArray array2 = doc.createNestedArray(); JsonArray array2 = doc.add<JsonArray>();
array2.add(1); array2.add(1);
array2.add("hello2"); array2.add("hello2");
array2.createNestedObject(); array2.add<JsonObject>();
JsonVariant variant2 = array2; JsonVariant variant2 = array2;
CHECK(carray1 != variant2); CHECK(carray1 != variant2);

View File

@ -1,21 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonArray basics") {
JsonDocument doc;
JsonArray array = doc.to<JsonArray>();
SECTION("CreateNestedArray") {
JsonArray arr = array.createNestedArray();
REQUIRE(arr == array[0].as<JsonArray>());
}
SECTION("CreateNestedObject") {
JsonObject obj = array.createNestedObject();
REQUIRE(obj == array[0].as<JsonObject>());
}
}

View File

@ -24,12 +24,12 @@ TEST_CASE("JsonArray::nesting()") {
} }
SECTION("returns 2 with nested array") { SECTION("returns 2 with nested array") {
arr.createNestedArray(); arr.add<JsonArray>();
REQUIRE(arr.nesting() == 2); REQUIRE(arr.nesting() == 2);
} }
SECTION("returns 2 with nested object") { SECTION("returns 2 with nested object") {
arr.createNestedObject(); arr.add<JsonObject>();
REQUIRE(arr.nesting() == 2); REQUIRE(arr.nesting() == 2);
} }
} }

View File

@ -19,14 +19,6 @@ TEST_CASE("Unbound JsonArray") {
REQUIRE(0 == array.size()); REQUIRE(0 == array.size());
} }
SECTION("CreateNestedArrayFails") {
REQUIRE(array.createNestedArray().isNull());
}
SECTION("CreateNestedObjectFails") {
REQUIRE(array.createNestedObject().isNull());
}
SECTION("PrintToWritesBrackets") { SECTION("PrintToWritesBrackets") {
char buffer[32]; char buffer[32];
serializeJson(array, buffer, sizeof(buffer)); serializeJson(array, buffer, sizeof(buffer));

View File

@ -9,7 +9,6 @@ add_executable(JsonDocumentTests
compare.cpp compare.cpp
constructor.cpp constructor.cpp
containsKey.cpp containsKey.cpp
createNested.cpp
ElementProxy.cpp ElementProxy.cpp
isNull.cpp isNull.cpp
issue1120.cpp issue1120.cpp

View File

@ -281,38 +281,6 @@ TEST_CASE("MemberProxy cast to JsonVariant") {
CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}"); CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}");
} }
TEST_CASE("MemberProxy::createNestedArray()") {
JsonDocument doc;
JsonArray arr = doc["items"].createNestedArray();
arr.add(42);
CHECK(doc["items"][0][0] == 42);
}
TEST_CASE("MemberProxy::createNestedArray(key)") {
JsonDocument doc;
JsonArray arr = doc["weather"].createNestedArray("temp");
arr.add(42);
CHECK(doc["weather"]["temp"][0] == 42);
}
TEST_CASE("MemberProxy::createNestedObject()") {
JsonDocument doc;
JsonObject obj = doc["items"].createNestedObject();
obj["value"] = 42;
CHECK(doc["items"][0]["value"] == 42);
}
TEST_CASE("MemberProxy::createNestedObject(key)") {
JsonDocument doc;
JsonObject obj = doc["status"].createNestedObject("weather");
obj["temp"] = 42;
CHECK(doc["status"]["weather"]["temp"] == 42);
}
TEST_CASE("Deduplicate keys") { TEST_CASE("Deduplicate keys") {
SpyingAllocator spy; SpyingAllocator spy;
JsonDocument doc(&spy); JsonDocument doc(&spy);

View File

@ -1,66 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::createNestedArray()") {
JsonDocument doc;
SECTION("promotes to array") {
doc.createNestedArray();
REQUIRE(doc.is<JsonArray>());
}
}
TEST_CASE("JsonDocument::createNestedArray(key)") {
JsonDocument doc;
SECTION("key is const char*") {
SECTION("promotes to object") {
doc.createNestedArray("hello");
REQUIRE(doc.is<JsonObject>());
}
}
SECTION("key is std::string") {
SECTION("promotes to object") {
doc.createNestedArray(std::string("hello"));
REQUIRE(doc.is<JsonObject>());
}
}
}
TEST_CASE("JsonDocument::createNestedObject()") {
JsonDocument doc;
SECTION("promotes to array") {
doc.createNestedObject();
REQUIRE(doc.is<JsonArray>());
}
}
TEST_CASE("JsonDocument::createNestedObject(key)") {
JsonDocument doc;
SECTION("key is const char*") {
SECTION("promotes to object") {
doc.createNestedObject("hello");
REQUIRE(doc.is<JsonObject>());
}
}
SECTION("key is std::string") {
SECTION("promotes to object") {
doc.createNestedObject(std::string("hello"));
REQUIRE(doc.is<JsonObject>());
}
}
}

View File

@ -7,8 +7,6 @@ add_executable(JsonObjectTests
compare.cpp compare.cpp
containsKey.cpp containsKey.cpp
copy.cpp copy.cpp
createNestedArray.cpp
createNestedObject.cpp
equals.cpp equals.cpp
invalid.cpp invalid.cpp
isNull.cpp isNull.cpp

View File

@ -43,12 +43,12 @@ TEST_CASE("Compare JsonObject with JsonObject") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello"; object1["b"] = "hello";
object1["c"][0] = false; object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello"; object2["b"] = "hello";
object2["c"][0] = false; object2["c"][0] = false;
@ -62,12 +62,12 @@ TEST_CASE("Compare JsonObject with JsonObject") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello1"; object1["b"] = "hello1";
object1["c"][0] = false; object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello2"; object2["b"] = "hello2";
object2["c"][0] = false; object2["c"][0] = false;
@ -107,12 +107,12 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object = doc.createNestedObject(); JsonObject object = doc.add<JsonObject>();
object["a"] = 1; object["a"] = 1;
object["b"] = "hello"; object["b"] = "hello";
object["c"][0] = false; object["c"][0] = false;
JsonVariant variant = doc.createNestedObject(); JsonVariant variant = doc.add<JsonObject>();
variant["a"] = 1; variant["a"] = 1;
variant["b"] = "hello"; variant["b"] = "hello";
variant["c"][0] = false; variant["c"][0] = false;
@ -133,12 +133,12 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object = doc.createNestedObject(); JsonObject object = doc.add<JsonObject>();
object["a"] = 1; object["a"] = 1;
object["b"] = "hello1"; object["b"] = "hello1";
object["c"][0] = false; object["c"][0] = false;
JsonVariant variant = doc.createNestedObject(); JsonVariant variant = doc.add<JsonObject>();
variant["a"] = 1; variant["a"] = 1;
variant["b"] = "hello2"; variant["b"] = "hello2";
variant["c"][0] = false; variant["c"][0] = false;
@ -199,12 +199,12 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object = doc.createNestedObject(); JsonObject object = doc.add<JsonObject>();
object["a"] = 1; object["a"] = 1;
object["b"] = "hello"; object["b"] = "hello";
object["c"][0] = false; object["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello"; object2["b"] = "hello";
object2["c"][0] = false; object2["c"][0] = false;
@ -226,12 +226,12 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object = doc.createNestedObject(); JsonObject object = doc.add<JsonObject>();
object["a"] = 1; object["a"] = 1;
object["b"] = "hello1"; object["b"] = "hello1";
object["c"][0] = false; object["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello2"; object2["b"] = "hello2";
object2["c"][0] = false; object2["c"][0] = false;
@ -292,12 +292,12 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello"; object1["b"] = "hello";
object1["c"][0] = false; object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello"; object2["b"] = "hello";
object2["c"][0] = false; object2["c"][0] = false;
@ -319,12 +319,12 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello1"; object1["b"] = "hello1";
object1["c"][0] = false; object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello2"; object2["b"] = "hello2";
object2["c"][0] = false; object2["c"][0] = false;
@ -387,13 +387,13 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello"; object1["b"] = "hello";
object1["c"][0] = false; object1["c"][0] = false;
JsonObjectConst carray1 = object1; JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello"; object2["b"] = "hello";
object2["c"][0] = false; object2["c"][0] = false;
@ -408,13 +408,13 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello1"; object1["b"] = "hello1";
object1["c"][0] = false; object1["c"][0] = false;
JsonObjectConst carray1 = object1; JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello2"; object2["b"] = "hello2";
object2["c"][0] = false; object2["c"][0] = false;
@ -455,13 +455,13 @@ TEST_CASE("Compare JsonObjectConst with JsonVariant") {
} }
SECTION("Compare with identical object") { SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello"; object1["b"] = "hello";
object1["c"][0] = false; object1["c"][0] = false;
JsonObjectConst carray1 = object1; JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello"; object2["b"] = "hello";
object2["c"][0] = false; object2["c"][0] = false;
@ -483,13 +483,13 @@ TEST_CASE("Compare JsonObjectConst with JsonVariant") {
} }
SECTION("Compare with different object") { SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject(); JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1; object1["a"] = 1;
object1["b"] = "hello1"; object1["b"] = "hello1";
object1["c"][0] = false; object1["c"][0] = false;
JsonObjectConst carray1 = object1; JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject(); JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1; object2["a"] = 1;
object2["b"] = "hello2"; object2["b"] = "hello2";
object2["c"][0] = false; object2["c"][0] = false;

View File

@ -1,27 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedArray()") {
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
JsonArray arr = obj.createNestedArray("hello");
REQUIRE(arr.isNull() == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
JsonArray arr = obj.createNestedArray(vla);
REQUIRE(arr.isNull() == false);
}
#endif
}

View File

@ -1,25 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedObject()") {
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
obj.createNestedObject("hello");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
obj.createNestedObject(vla);
}
#endif
}

View File

@ -19,14 +19,6 @@ TEST_CASE("JsonObject::invalid()") {
REQUIRE(0 == obj.size()); REQUIRE(0 == obj.size());
} }
SECTION("CreateNestedArrayFails") {
REQUIRE(obj.createNestedArray("hello").isNull());
}
SECTION("CreateNestedObjectFails") {
REQUIRE(obj.createNestedObject("world").isNull());
}
SECTION("serialize to 'null'") { SECTION("serialize to 'null'") {
char buffer[32]; char buffer[32];
serializeJson(obj, buffer, sizeof(buffer)); serializeJson(obj, buffer, sizeof(buffer));

View File

@ -24,12 +24,12 @@ TEST_CASE("JsonObject::nesting()") {
} }
SECTION("returns 2 with nested array") { SECTION("returns 2 with nested array") {
obj.createNestedArray("nested"); obj["nested"].to<JsonArray>();
REQUIRE(obj.nesting() == 2); REQUIRE(obj.nesting() == 2);
} }
SECTION("returns 2 with nested object") { SECTION("returns 2 with nested object") {
obj.createNestedObject("nested"); obj["nested"].to<JsonObject>();
REQUIRE(obj.nesting() == 2); REQUIRE(obj.nesting() == 2);
} }
} }

View File

@ -32,26 +32,6 @@ TEST_CASE("std::string") {
REQUIRE(std::string("value") == obj[std::string("key")]); REQUIRE(std::string("value") == obj[std::string("key")]);
} }
SECTION("createNestedObject()") {
JsonObject obj = doc.to<JsonObject>();
std::string key = "key";
char json[64];
obj.createNestedObject(key);
eraseString(key);
serializeJson(doc, json, sizeof(json));
REQUIRE(std::string("{\"key\":{}}") == json);
}
SECTION("createNestedArray()") {
JsonObject obj = doc.to<JsonObject>();
std::string key = "key";
char json[64];
obj.createNestedArray(key);
eraseString(key);
serializeJson(doc, json, sizeof(json));
REQUIRE(std::string("{\"key\":[]}") == json);
}
SECTION("containsKey()") { SECTION("containsKey()") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);

View File

@ -245,7 +245,7 @@ TEST_CASE("JsonObject::operator[]") {
#endif #endif
SECTION("chain") { SECTION("chain") {
obj.createNestedObject("hello")["world"] = 123; obj["hello"]["world"] = 123;
REQUIRE(123 == obj["hello"]["world"].as<int>()); REQUIRE(123 == obj["hello"]["world"].as<int>());
REQUIRE(true == obj["hello"]["world"].is<int>()); REQUIRE(true == obj["hello"]["world"].is<int>());

View File

@ -92,13 +92,13 @@ TEST_CASE("serializeJson(JsonArray)") {
} }
SECTION("OneEmptyNestedArray") { SECTION("OneEmptyNestedArray") {
array.createNestedArray(); array.add<JsonArray>();
check(array, "[[]]"); check(array, "[[]]");
} }
SECTION("OneEmptyNestedHash") { SECTION("OneEmptyNestedHash") {
array.createNestedObject(); array.add<JsonObject>();
check(array, "[{}]"); check(array, "[{}]");
} }

View File

@ -43,8 +43,8 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
} }
SECTION("EmptyNestedArrays") { SECTION("EmptyNestedArrays") {
array.createNestedArray(); array.add<JsonArray>();
array.createNestedArray(); array.add<JsonArray>();
checkArray(array, checkArray(array,
"[\r\n" "[\r\n"
@ -54,11 +54,11 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
} }
SECTION("NestedArrays") { SECTION("NestedArrays") {
JsonArray nested1 = array.createNestedArray(); JsonArray nested1 = array.add<JsonArray>();
nested1.add(1); nested1.add(1);
nested1.add(2); nested1.add(2);
JsonObject nested2 = array.createNestedObject(); JsonObject nested2 = array.add<JsonObject>();
nested2["key"] = 3; nested2["key"] = 3;
checkArray(array, checkArray(array,

View File

@ -99,7 +99,7 @@ TEST_CASE("serializeJson(JsonObject)") {
JsonDocument b; JsonDocument b;
JsonDocument c; JsonDocument c;
obj.createNestedArray("a"); obj["a"].to<JsonArray>();
obj["b"] = b.to<JsonArray>(); obj["b"] = b.to<JsonArray>();
obj["c"] = c.to<JsonArray>(); obj["c"] = c.to<JsonArray>();
@ -110,7 +110,7 @@ TEST_CASE("serializeJson(JsonObject)") {
JsonDocument b; JsonDocument b;
JsonDocument c; JsonDocument c;
obj.createNestedObject("a"); obj["a"].to<JsonObject>();
obj["b"] = b.to<JsonObject>(); obj["b"] = b.to<JsonObject>();
obj["c"] = c.to<JsonObject>(); obj["c"] = c.to<JsonObject>();

View File

@ -47,8 +47,8 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
} }
SECTION("EmptyNestedContainers") { SECTION("EmptyNestedContainers") {
obj.createNestedObject("key1"); obj["key1"].to<JsonObject>();
obj.createNestedArray("key2"); obj["key2"].to<JsonArray>();
checkObjectPretty(obj, checkObjectPretty(obj,
"{\r\n" "{\r\n"
@ -58,10 +58,10 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
} }
SECTION("NestedContainers") { SECTION("NestedContainers") {
JsonObject nested1 = obj.createNestedObject("key1"); JsonObject nested1 = obj["key1"].to<JsonObject>();
nested1["a"] = 1; nested1["a"] = 1;
JsonArray nested2 = obj.createNestedArray("key2"); JsonArray nested2 = obj["key2"].to<JsonArray>();
nested2.add(2); nested2.add(2);
checkObjectPretty(obj, checkObjectPretty(obj,

View File

@ -10,7 +10,6 @@ add_executable(JsonVariantTests
containsKey.cpp containsKey.cpp
converters.cpp converters.cpp
copy.cpp copy.cpp
createNested.cpp
is.cpp is.cpp
isnull.cpp isnull.cpp
misc.cpp misc.cpp

View File

@ -16,7 +16,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("stores JsonArray by copy") { SECTION("stores JsonArray by copy") {
JsonArray arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
JsonObject obj = arr.createNestedObject(); JsonObject obj = arr.add<JsonObject>();
obj["hello"] = "world"; obj["hello"] = "world";
var1.set(arr); var1.set(arr);
@ -27,7 +27,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("stores JsonObject by copy") { SECTION("stores JsonObject by copy") {
JsonObject obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
JsonArray arr = obj.createNestedArray("value"); JsonArray arr = obj["value"].to<JsonArray>();
arr.add(42); arr.add(42);
var1.set(obj); var1.set(obj);

View File

@ -1,58 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
TEST_CASE("JsonVariant::createNestedObject()") {
JsonDocument doc;
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to array") {
JsonObject obj = variant.createNestedObject();
obj["value"] = 42;
REQUIRE(variant.is<JsonArray>() == true);
REQUIRE(variant[0]["value"] == 42);
REQUIRE(obj.isNull() == false);
}
}
TEST_CASE("JsonVariant::createNestedArray()") {
JsonDocument doc;
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to array") {
JsonArray arr = variant.createNestedArray();
REQUIRE(variant.is<JsonArray>() == true);
REQUIRE(arr.isNull() == false);
}
}
TEST_CASE("JsonVariant::createNestedObject(key)") {
JsonDocument doc;
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to object") {
JsonObject obj = variant.createNestedObject("weather");
obj["temp"] = 42;
REQUIRE(variant.is<JsonObject>() == true);
REQUIRE(variant["weather"]["temp"] == 42);
}
}
TEST_CASE("JsonVariant::createNestedArray(key)") {
JsonDocument doc;
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to object") {
JsonArray arr = variant.createNestedArray("items");
REQUIRE(variant.is<JsonObject>() == true);
REQUIRE(arr.isNull() == false);
}
}

View File

@ -50,7 +50,7 @@ TEST_CASE("JsonVariant::operator[]") {
} }
SECTION("set value in a nested object") { SECTION("set value in a nested object") {
array.createNestedObject(); array.add<JsonObject>();
var[0]["hello"] = "world"; var[0]["hello"] = "world";

View File

@ -193,22 +193,6 @@ TEST_CASE("unsigned char[]") {
REQUIRE(0 == obj.size()); REQUIRE(0 == obj.size());
} }
SECTION("createNestedArray()") {
unsigned char key[] = "hello";
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj.createNestedArray(key);
}
SECTION("createNestedObject()") {
unsigned char key[] = "hello";
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj.createNestedObject(key);
}
} }
SECTION("MemberProxy") { SECTION("MemberProxy") {

View File

@ -12,8 +12,6 @@ measureMsgPack KEYWORD2
# Methods # Methods
add KEYWORD2 add KEYWORD2
as KEYWORD2 as KEYWORD2
createNestedArray KEYWORD2
createNestedObject KEYWORD2
get KEYWORD2 get KEYWORD2
set KEYWORD2 set KEYWORD2
to KEYWORD2 to KEYWORD2

View File

@ -130,16 +130,6 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
return {*this, index}; return {*this, index};
} }
// Creates an object and appends it to the array.
// https://arduinojson.org/v6/api/jsonarray/createnestedobject/
FORCE_INLINE JsonObject createNestedObject() const;
// Creates an array and appends it to the array.
// https://arduinojson.org/v6/api/jsonarray/createnestedarray/
FORCE_INLINE JsonArray createNestedArray() const {
return add<JsonArray>();
}
operator JsonVariantConst() const { operator JsonVariantConst() const {
return JsonVariantConst(collectionToVariant(data_), resources_); return JsonVariantConst(collectionToVariant(data_), resources_);
} }

View File

@ -7,26 +7,8 @@
#include <ArduinoJson/Array/JsonArray.hpp> #include <ArduinoJson/Array/JsonArray.hpp>
#include <ArduinoJson/Object/JsonObject.hpp> #include <ArduinoJson/Object/JsonObject.hpp>
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
inline JsonObject JsonArray::createNestedObject() const {
return add<JsonObject>();
}
ARDUINOJSON_END_PUBLIC_NAMESPACE
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
template <typename TDerived>
inline JsonArray VariantRefBase<TDerived>::createNestedArray() const {
return add<JsonArray>();
}
template <typename TDerived>
inline JsonObject VariantRefBase<TDerived>::createNestedObject() const {
return add<JsonObject>();
}
template <typename TDerived> template <typename TDerived>
inline ElementProxy<TDerived> VariantRefBase<TDerived>::operator[]( inline ElementProxy<TDerived> VariantRefBase<TDerived>::operator[](
size_t index) const { size_t index) const {

View File

@ -152,46 +152,6 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
return getVariant().template to<T>(); return getVariant().template to<T>();
} }
// Creates an array and appends it to the root array.
// https://arduinojson.org/v6/api/jsondocument/createnestedarray/
JsonArray createNestedArray() {
return add<JsonArray>();
}
// Creates an array and adds it to the root object.
// https://arduinojson.org/v6/api/jsondocument/createnestedarray/
template <typename TChar>
JsonArray createNestedArray(TChar* key) {
return operator[](key).template to<JsonArray>();
}
// Creates an array and adds it to the root object.
// https://arduinojson.org/v6/api/jsondocument/createnestedarray/
template <typename TString>
JsonArray createNestedArray(const TString& key) {
return operator[](key).template to<JsonArray>();
}
// Creates an object and appends it to the root array.
// https://arduinojson.org/v6/api/jsondocument/createnestedobject/
JsonObject createNestedObject() {
return add<JsonObject>();
}
// Creates an object and adds it to the root object.
// https://arduinojson.org/v6/api/jsondocument/createnestedobject/
template <typename TChar>
JsonObject createNestedObject(TChar* key) {
return operator[](key).template to<JsonObject>();
}
// Creates an object and adds it to the root object.
// https://arduinojson.org/v6/api/jsondocument/createnestedobject/
template <typename TString>
JsonObject createNestedObject(const TString& key) {
return operator[](key).template to<JsonObject>();
}
// Returns true if the root object contains the specified key. // Returns true if the root object contains the specified key.
// https://arduinojson.org/v6/api/jsondocument/containskey/ // https://arduinojson.org/v6/api/jsondocument/containskey/
template <typename TChar> template <typename TChar>

View File

@ -166,30 +166,6 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
resources_) != 0; resources_) != 0;
} }
// Creates an array and adds it to the object.
// https://arduinojson.org/v6/api/jsonobject/createnestedarray/
template <typename TString>
FORCE_INLINE JsonArray createNestedArray(const TString& key) const;
// Creates an array and adds it to the object.
// https://arduinojson.org/v6/api/jsonobject/createnestedarray/
template <typename TChar>
FORCE_INLINE JsonArray createNestedArray(TChar* key) const;
// Creates an object and adds it to the object.
// https://arduinojson.org/v6/api/jsonobject/createnestedobject/
template <typename TString>
JsonObject createNestedObject(const TString& key) const {
return operator[](key).template to<JsonObject>();
}
// Creates an object and adds it to the object.
// https://arduinojson.org/v6/api/jsonobject/createnestedobject/
template <typename TChar>
JsonObject createNestedObject(TChar* key) const {
return operator[](key).template to<JsonObject>();
}
private: private:
detail::ResourceManager* getResourceManager() const { detail::ResourceManager* getResourceManager() const {
return resources_; return resources_;

View File

@ -7,49 +7,8 @@
#include <ArduinoJson/Array/JsonArray.hpp> #include <ArduinoJson/Array/JsonArray.hpp>
#include <ArduinoJson/Object/JsonObject.hpp> #include <ArduinoJson/Object/JsonObject.hpp>
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
template <typename TString>
inline JsonArray JsonObject::createNestedArray(const TString& key) const {
return operator[](key).template to<JsonArray>();
}
template <typename TChar>
inline JsonArray JsonObject::createNestedArray(TChar* key) const {
return operator[](key).template to<JsonArray>();
}
ARDUINOJSON_END_PUBLIC_NAMESPACE
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
template <typename TDerived>
template <typename TString>
inline JsonArray VariantRefBase<TDerived>::createNestedArray(
const TString& key) const {
return operator[](key).template to<JsonArray>();
}
template <typename TDerived>
template <typename TChar>
inline JsonArray VariantRefBase<TDerived>::createNestedArray(TChar* key) const {
return operator[](key).template to<JsonArray>();
}
template <typename TDerived>
template <typename TString>
inline JsonObject VariantRefBase<TDerived>::createNestedObject(
const TString& key) const {
return operator[](key).template to<JsonObject>();
}
template <typename TDerived>
template <typename TChar>
inline JsonObject VariantRefBase<TDerived>::createNestedObject(
TChar* key) const {
return operator[](key).template to<JsonObject>();
}
template <typename TDerived> template <typename TDerived>
template <typename TString> template <typename TString>
inline typename enable_if<IsString<TString>::value, bool>::type inline typename enable_if<IsString<TString>::value, bool>::type

View File

@ -191,14 +191,6 @@ class VariantRefBase : public VariantTag {
getResourceManager()); getResourceManager());
} }
// Creates an array and appends it to the array.
// https://arduinojson.org/v6/api/jsonvariant/createnestedarray/
FORCE_INLINE JsonArray createNestedArray() const;
// Creates an object and appends it to the array.
// https://arduinojson.org/v6/api/jsonvariant/createnestedobject/
FORCE_INLINE JsonObject createNestedObject() const;
// Gets or sets an array element. // Gets or sets an array element.
// https://arduinojson.org/v6/api/jsonvariant/subscript/ // https://arduinojson.org/v6/api/jsonvariant/subscript/
FORCE_INLINE ElementProxy<TDerived> operator[](size_t index) const; FORCE_INLINE ElementProxy<TDerived> operator[](size_t index) const;
@ -229,26 +221,6 @@ class VariantRefBase : public VariantTag {
MemberProxy<TDerived, TChar*>>::type MemberProxy<TDerived, TChar*>>::type
operator[](TChar* key) const; operator[](TChar* key) const;
// Creates an array and adds it to the object.
// https://arduinojson.org/v6/api/jsonvariant/createnestedarray/
template <typename TString>
FORCE_INLINE JsonArray createNestedArray(const TString& key) const;
// Creates an array and adds it to the object.
// https://arduinojson.org/v6/api/jsonvariant/createnestedarray/
template <typename TChar>
FORCE_INLINE JsonArray createNestedArray(TChar* key) const;
// Creates an object and adds it to the object.
// https://arduinojson.org/v6/api/jsonvariant/createnestedobject/
template <typename TString>
JsonObject createNestedObject(const TString& key) const;
// Creates an object and adds it to the object.
// https://arduinojson.org/v6/api/jsonvariant/createnestedobject/
template <typename TChar>
JsonObject createNestedObject(TChar* key) const;
private: private:
TDerived& derived() { TDerived& derived() {
return static_cast<TDerived&>(*this); return static_cast<TDerived&>(*this);