createNestedObject.cpp 939 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonObject::createNestedObject()") {
  7. DynamicJsonDocument doc;
  8. JsonObject obj = doc.to<JsonObject>();
  9. SECTION("key is a const char*") {
  10. obj.createNestedObject("hello");
  11. }
  12. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  13. SECTION("key is a VLA") {
  14. int i = 16;
  15. char vla[i];
  16. strcpy(vla, "hello");
  17. obj.createNestedObject(vla);
  18. }
  19. #endif
  20. SECTION("releases memory from nested object") {
  21. obj.createNestedObject(std::string("a"))
  22. .createNestedObject(std::string("b"))
  23. .set(std::string("c"))
  24. .set(1);
  25. // {"a":{"b":{"c":1}}}
  26. REQUIRE(doc.memoryUsage() ==
  27. 3 * JSON_OBJECT_SIZE(1) + 3 * JSON_STRING_SIZE(2));
  28. obj.createNestedObject(std::string("a"));
  29. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2));
  30. }
  31. }