// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #include #include #include TEST_CASE("JsonDocument::createNestedObject()") { JsonDocument doc; SECTION("createNestedObject()") { JsonObject object = doc.createNestedObject(); object["hello"] = "world"; REQUIRE(doc.as() == "[{\"hello\":\"world\"}]"); } SECTION("createNestedObject(const char*)") { JsonObject object = doc.createNestedObject("key"); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } SECTION("createNestedObject(std::string)") { JsonObject object = doc.createNestedObject(std::string("key")); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("createNestedObject(VLA)") { size_t i = 16; char vla[i]; strcpy(vla, "key"); JsonObject object = doc.createNestedObject(vla); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #endif } TEST_CASE("JsonArray::createNestedObject()") { JsonDocument doc; JsonArray array = doc.to(); JsonObject object = array.createNestedObject(); object["hello"] = "world"; REQUIRE(doc.as() == "[{\"hello\":\"world\"}]"); } TEST_CASE("JsonObject::createNestedObject()") { JsonDocument doc; JsonObject object = doc.to(); SECTION("createNestedObject(const char*)") { JsonObject nestedObject = object.createNestedObject("key"); nestedObject["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } SECTION("createNestedObject(std::string)") { JsonObject nestedObject = object.createNestedObject(std::string("key")); nestedObject["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("createNestedObject(VLA)") { size_t i = 16; char vla[i]; strcpy(vla, "key"); JsonObject nestedObject = object.createNestedObject(vla); nestedObject["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #endif } TEST_CASE("JsonVariant::createNestedObject()") { JsonDocument doc; JsonVariant variant = doc.to(); SECTION("createNestedObject()") { JsonObject object = variant.createNestedObject(); object["hello"] = "world"; REQUIRE(doc.as() == "[{\"hello\":\"world\"}]"); } SECTION("createNestedObject(const char*)") { JsonObject object = variant.createNestedObject("key"); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } SECTION("createNestedObject(std::string)") { JsonObject object = variant.createNestedObject(std::string("key")); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("createNestedObject(VLA)") { size_t i = 16; char vla[i]; strcpy(vla, "key"); JsonObject object = variant.createNestedObject(vla); object["hello"] = "world"; REQUIRE(doc.as() == "{\"key\":{\"hello\":\"world\"}}"); } #endif }