DynamicJsonDocument.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. using ARDUINOJSON_NAMESPACE::addPadding;
  7. static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
  8. std::string json;
  9. serializeJson(doc, json);
  10. REQUIRE(json == expected);
  11. }
  12. TEST_CASE("DynamicJsonDocument") {
  13. DynamicJsonDocument doc(4096);
  14. SECTION("serializeJson()") {
  15. JsonObject obj = doc.to<JsonObject>();
  16. obj["hello"] = "world";
  17. std::string json;
  18. serializeJson(doc, json);
  19. REQUIRE(json == "{\"hello\":\"world\"}");
  20. }
  21. SECTION("memoryUsage()") {
  22. SECTION("starts at zero") {
  23. REQUIRE(doc.memoryUsage() == 0);
  24. }
  25. SECTION("JSON_ARRAY_SIZE(0)") {
  26. doc.to<JsonArray>();
  27. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
  28. }
  29. SECTION("JSON_ARRAY_SIZE(1)") {
  30. doc.to<JsonArray>().add(42);
  31. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
  32. }
  33. SECTION("JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0)") {
  34. doc.to<JsonArray>().createNestedArray();
  35. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0));
  36. }
  37. }
  38. SECTION("capacity()") {
  39. SECTION("matches constructor argument") {
  40. DynamicJsonDocument doc2(256);
  41. REQUIRE(doc2.capacity() == 256);
  42. }
  43. SECTION("rounds up constructor argument") {
  44. DynamicJsonDocument doc2(253);
  45. REQUIRE(doc2.capacity() == 256);
  46. }
  47. }
  48. SECTION("memoryUsage()") {
  49. SECTION("Increases after adding value to array") {
  50. JsonArray arr = doc.to<JsonArray>();
  51. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
  52. arr.add(42);
  53. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
  54. arr.add(43);
  55. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
  56. }
  57. SECTION("Increases after adding value to object") {
  58. JsonObject obj = doc.to<JsonObject>();
  59. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  60. obj["a"] = 1;
  61. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1));
  62. obj["b"] = 2;
  63. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2));
  64. }
  65. }
  66. }
  67. TEST_CASE("DynamicJsonDocument constructor") {
  68. SECTION("Copy constructor") {
  69. DynamicJsonDocument doc1(1234);
  70. deserializeJson(doc1, "{\"hello\":\"world\"}");
  71. DynamicJsonDocument doc2 = doc1;
  72. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  73. REQUIRE(doc2.capacity() == doc1.capacity());
  74. }
  75. SECTION("Construct from StaticJsonDocument") {
  76. StaticJsonDocument<200> doc1;
  77. deserializeJson(doc1, "{\"hello\":\"world\"}");
  78. DynamicJsonDocument doc2 = doc1;
  79. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  80. REQUIRE(doc2.capacity() == doc1.capacity());
  81. }
  82. SECTION("Construct from JsonObject") {
  83. StaticJsonDocument<200> doc1;
  84. JsonObject obj = doc1.to<JsonObject>();
  85. obj["hello"] = "world";
  86. DynamicJsonDocument doc2 = obj;
  87. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  88. REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
  89. }
  90. SECTION("Construct from JsonArray") {
  91. StaticJsonDocument<200> doc1;
  92. JsonArray arr = doc1.to<JsonArray>();
  93. arr.add("hello");
  94. DynamicJsonDocument doc2 = arr;
  95. REQUIRE_JSON(doc2, "[\"hello\"]");
  96. REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
  97. }
  98. SECTION("Construct from JsonVariant") {
  99. StaticJsonDocument<200> doc1;
  100. deserializeJson(doc1, "42");
  101. DynamicJsonDocument doc2 = doc1.as<JsonVariant>();
  102. REQUIRE_JSON(doc2, "42");
  103. REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
  104. }
  105. }
  106. TEST_CASE("DynamicJsonDocument assignment") {
  107. SECTION("Copy assignment reallocates when capacity is smaller") {
  108. DynamicJsonDocument doc1(1234);
  109. deserializeJson(doc1, "{\"hello\":\"world\"}");
  110. DynamicJsonDocument doc2(8);
  111. doc2 = doc1;
  112. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  113. REQUIRE(doc2.capacity() == doc1.capacity());
  114. }
  115. SECTION("Copy assignment reallocates when capacity is larger") {
  116. DynamicJsonDocument doc1(100);
  117. deserializeJson(doc1, "{\"hello\":\"world\"}");
  118. DynamicJsonDocument doc2(1234);
  119. doc2 = doc1;
  120. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  121. REQUIRE(doc2.capacity() == doc1.capacity());
  122. }
  123. SECTION("Assign from StaticJsonDocument") {
  124. StaticJsonDocument<200> doc1;
  125. deserializeJson(doc1, "{\"hello\":\"world\"}");
  126. DynamicJsonDocument doc2(4096);
  127. doc2.to<JsonVariant>().set(666);
  128. doc2 = doc1;
  129. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  130. }
  131. SECTION("Assign from JsonObject") {
  132. StaticJsonDocument<200> doc1;
  133. JsonObject obj = doc1.to<JsonObject>();
  134. obj["hello"] = "world";
  135. DynamicJsonDocument doc2(4096);
  136. doc2 = obj;
  137. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  138. REQUIRE(doc2.capacity() == 4096);
  139. }
  140. SECTION("Assign from JsonArray") {
  141. StaticJsonDocument<200> doc1;
  142. JsonArray arr = doc1.to<JsonArray>();
  143. arr.add("hello");
  144. DynamicJsonDocument doc2(4096);
  145. doc2 = arr;
  146. REQUIRE_JSON(doc2, "[\"hello\"]");
  147. REQUIRE(doc2.capacity() == 4096);
  148. }
  149. SECTION("Assign from JsonVariant") {
  150. StaticJsonDocument<200> doc1;
  151. deserializeJson(doc1, "42");
  152. DynamicJsonDocument doc2(4096);
  153. doc2 = doc1.as<JsonVariant>();
  154. REQUIRE_JSON(doc2, "42");
  155. REQUIRE(doc2.capacity() == 4096);
  156. }
  157. SECTION("Assign from MemberProxy") {
  158. StaticJsonDocument<200> doc1;
  159. doc1["value"] = 42;
  160. DynamicJsonDocument doc2(4096);
  161. doc2 = doc1["value"];
  162. REQUIRE_JSON(doc2, "42");
  163. REQUIRE(doc2.capacity() == 4096);
  164. }
  165. SECTION("Assign from ElementProxy") {
  166. StaticJsonDocument<200> doc1;
  167. doc1[0] = 42;
  168. DynamicJsonDocument doc2(4096);
  169. doc2 = doc1[0];
  170. REQUIRE_JSON(doc2, "42");
  171. REQUIRE(doc2.capacity() == 4096);
  172. }
  173. }