StaticJsonDocument.cpp 5.9 KB

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