// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include TEST_CASE("JsonObject::operator[]") { DynamicJsonDocument doc; JsonObject obj = doc.to(); SECTION("int") { obj["hello"] = 123; REQUIRE(123 == obj["hello"].as()); REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); } SECTION("volatile int") { // issue #415 volatile int i = 123; obj["hello"] = i; REQUIRE(123 == obj["hello"].as()); REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); } SECTION("double") { obj["hello"] = 123.45; REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); REQUIRE(123.45 == obj["hello"].as()); } SECTION("bool") { obj["hello"] = true; REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); REQUIRE(true == obj["hello"].as()); } SECTION("const char*") { obj["hello"] = "h3110"; REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); REQUIRE(std::string("h3110") == obj["hello"].as()); REQUIRE(std::string("h3110") == obj["hello"].as()); // <- short hand } SECTION("array") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); obj["hello"] = arr; REQUIRE(arr == obj["hello"].as()); REQUIRE(arr == obj["hello"].as()); // <- short hand // REQUIRE(arr == obj["hello"].as()); // REQUIRE(arr == obj["hello"].as()); // <- short hand REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); } SECTION("object") { DynamicJsonDocument doc2; JsonObject obj2 = doc2.to(); obj["hello"] = obj2; REQUIRE(obj2 == obj["hello"].as()); REQUIRE(obj2 == obj["hello"].as()); REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); REQUIRE(false == obj["hello"].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); arr.add(42); obj["a"] = arr[0]; REQUIRE(42 == obj["a"]); } SECTION("object subscript") { DynamicJsonDocument doc2; JsonObject obj2 = doc2.to(); obj2.set("x", 42); obj["a"] = obj2["x"]; REQUIRE(42 == obj["a"]); } SECTION("char key[]") { // issue #423 char key[] = "hello"; obj[key] = 42; REQUIRE(42 == obj[key]); } SECTION("should not duplicate const char*") { obj["hello"] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1); REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* value") { obj["hello"] = const_cast("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* key") { obj[const_cast("hello")] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* key&value") { obj[const_cast("hello")] = const_cast("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= doc.memoryUsage()); } SECTION("should duplicate std::string value") { obj["hello"] = std::string("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string key") { obj[std::string("hello")] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string key&value") { obj[std::string("hello")] = std::string("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= doc.memoryUsage()); } SECTION("should ignore null key") { // object must have a value to make a call to strcmp() obj["dummy"] = 42; const char* null = 0; obj[null] = 666; REQUIRE(obj.size() == 1); REQUIRE(obj[null] == 0); } }