DynamicJsonDocument.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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() == addPadding(doc1.memoryUsage()));
  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() == addPadding(doc1.memoryUsage()));
  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 preserves the buffer when capacity is sufficient") {
  108. DynamicJsonDocument doc1(1234);
  109. deserializeJson(doc1, "{\"hello\":\"world\"}");
  110. DynamicJsonDocument doc2(doc1.capacity());
  111. doc2 = doc1;
  112. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  113. REQUIRE(doc2.capacity() == doc1.capacity());
  114. }
  115. SECTION("Copy assignment realloc the buffer when capacity is insufficient") {
  116. DynamicJsonDocument doc1(1234);
  117. deserializeJson(doc1, "{\"hello\":\"world\"}");
  118. DynamicJsonDocument doc2(8);
  119. REQUIRE(doc2.capacity() < doc1.memoryUsage());
  120. doc2 = doc1;
  121. REQUIRE(doc2.capacity() >= doc1.memoryUsage());
  122. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  123. }
  124. SECTION("Assign from StaticJsonDocument") {
  125. StaticJsonDocument<200> doc1;
  126. deserializeJson(doc1, "{\"hello\":\"world\"}");
  127. DynamicJsonDocument doc2(4096);
  128. doc2.to<JsonVariant>().set(666);
  129. doc2 = doc1;
  130. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  131. }
  132. SECTION("Assign from JsonObject") {
  133. StaticJsonDocument<200> doc1;
  134. JsonObject obj = doc1.to<JsonObject>();
  135. obj["hello"] = "world";
  136. DynamicJsonDocument doc2(4096);
  137. doc2 = obj;
  138. REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
  139. REQUIRE(doc2.capacity() == 4096);
  140. }
  141. SECTION("Assign from JsonArray") {
  142. StaticJsonDocument<200> doc1;
  143. JsonArray arr = doc1.to<JsonArray>();
  144. arr.add("hello");
  145. DynamicJsonDocument doc2(4096);
  146. doc2 = arr;
  147. REQUIRE_JSON(doc2, "[\"hello\"]");
  148. REQUIRE(doc2.capacity() == 4096);
  149. }
  150. SECTION("Assign from JsonVariant") {
  151. StaticJsonDocument<200> doc1;
  152. deserializeJson(doc1, "42");
  153. DynamicJsonDocument doc2(4096);
  154. doc2 = doc1.as<JsonVariant>();
  155. REQUIRE_JSON(doc2, "42");
  156. REQUIRE(doc2.capacity() == 4096);
  157. }
  158. }