createNested.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. static const char* null = 0;
  8. TEST_CASE("JsonVariant::createNestedObject()") {
  9. DynamicJsonDocument doc(4096);
  10. JsonVariant variant = doc.to<JsonVariant>();
  11. SECTION("promotes to array") {
  12. JsonObject obj = variant.createNestedObject();
  13. obj["value"] = 42;
  14. REQUIRE(variant.is<JsonArray>() == true);
  15. REQUIRE(variant[0]["value"] == 42);
  16. REQUIRE(obj.isNull() == false);
  17. }
  18. SECTION("works on MemberProxy") {
  19. JsonObject obj = variant["items"].createNestedObject();
  20. obj["value"] = 42;
  21. REQUIRE(variant["items"][0]["value"] == 42);
  22. }
  23. }
  24. TEST_CASE("JsonVariant::createNestedArray()") {
  25. DynamicJsonDocument doc(4096);
  26. JsonVariant variant = doc.to<JsonVariant>();
  27. SECTION("promotes to array") {
  28. JsonArray arr = variant.createNestedArray();
  29. REQUIRE(variant.is<JsonArray>() == true);
  30. REQUIRE(arr.isNull() == false);
  31. }
  32. SECTION("works on MemberProxy") {
  33. JsonArray arr = variant["items"].createNestedArray();
  34. arr.add(42);
  35. REQUIRE(variant["items"][0][0] == 42);
  36. }
  37. }
  38. TEST_CASE("JsonVariant::createNestedObject(key)") {
  39. DynamicJsonDocument doc(4096);
  40. JsonVariant variant = doc.to<JsonVariant>();
  41. SECTION("promotes to object") {
  42. JsonObject obj = variant.createNestedObject("weather");
  43. obj["temp"] = 42;
  44. REQUIRE(variant.is<JsonObject>() == true);
  45. REQUIRE(variant["weather"]["temp"] == 42);
  46. }
  47. SECTION("works on MemberProxy") {
  48. JsonObject obj = variant["status"].createNestedObject("weather");
  49. obj["temp"] = 42;
  50. REQUIRE(variant["status"]["weather"]["temp"] == 42);
  51. }
  52. }
  53. TEST_CASE("JsonVariant::createNestedArray(key)") {
  54. DynamicJsonDocument doc(4096);
  55. JsonVariant variant = doc.to<JsonVariant>();
  56. SECTION("promotes to object") {
  57. JsonArray arr = variant.createNestedArray("items");
  58. REQUIRE(variant.is<JsonObject>() == true);
  59. REQUIRE(arr.isNull() == false);
  60. }
  61. SECTION("works on MemberProxy") {
  62. JsonArray arr = variant["weather"].createNestedArray("temp");
  63. arr.add(42);
  64. REQUIRE(variant["weather"]["temp"][0] == 42);
  65. }
  66. }