array.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using ArduinoJson::detail::sizeofArray;
  8. using ArduinoJson::detail::sizeofString;
  9. TEST_CASE("deserialize JSON array") {
  10. SpyingAllocator allocator;
  11. JsonDocument doc(&allocator);
  12. SECTION("An empty array") {
  13. DeserializationError err = deserializeJson(doc, "[]");
  14. JsonArray arr = doc.as<JsonArray>();
  15. REQUIRE(err == DeserializationError::Ok);
  16. REQUIRE(0 == arr.size());
  17. }
  18. SECTION("Spaces") {
  19. SECTION("Before the opening bracket") {
  20. DeserializationError err = deserializeJson(doc, " []");
  21. JsonArray arr = doc.as<JsonArray>();
  22. REQUIRE(err == DeserializationError::Ok);
  23. REQUIRE(0 == arr.size());
  24. }
  25. SECTION("Before first value") {
  26. DeserializationError err = deserializeJson(doc, "[ \t\r\n42]");
  27. JsonArray arr = doc.as<JsonArray>();
  28. REQUIRE(err == DeserializationError::Ok);
  29. REQUIRE(1 == arr.size());
  30. REQUIRE(arr[0] == 42);
  31. }
  32. SECTION("After first value") {
  33. DeserializationError err = deserializeJson(doc, "[42 \t\r\n]");
  34. JsonArray arr = doc.as<JsonArray>();
  35. REQUIRE(err == DeserializationError::Ok);
  36. REQUIRE(1 == arr.size());
  37. REQUIRE(arr[0] == 42);
  38. }
  39. }
  40. SECTION("Values types") {
  41. SECTION("On integer") {
  42. DeserializationError err = deserializeJson(doc, "[42]");
  43. JsonArray arr = doc.as<JsonArray>();
  44. REQUIRE(err == DeserializationError::Ok);
  45. REQUIRE(1 == arr.size());
  46. REQUIRE(arr[0] == 42);
  47. }
  48. SECTION("Two integers") {
  49. DeserializationError err = deserializeJson(doc, "[42,84]");
  50. JsonArray arr = doc.as<JsonArray>();
  51. REQUIRE(err == DeserializationError::Ok);
  52. REQUIRE(2 == arr.size());
  53. REQUIRE(arr[0] == 42);
  54. REQUIRE(arr[1] == 84);
  55. }
  56. SECTION("Double") {
  57. DeserializationError err = deserializeJson(doc, "[4.2,1e2]");
  58. JsonArray arr = doc.as<JsonArray>();
  59. REQUIRE(err == DeserializationError::Ok);
  60. REQUIRE(2 == arr.size());
  61. REQUIRE(arr[0] == 4.2);
  62. REQUIRE(arr[1] == 1e2);
  63. }
  64. SECTION("Unsigned long") {
  65. DeserializationError err = deserializeJson(doc, "[4294967295]");
  66. JsonArray arr = doc.as<JsonArray>();
  67. REQUIRE(err == DeserializationError::Ok);
  68. REQUIRE(1 == arr.size());
  69. REQUIRE(arr[0] == 4294967295UL);
  70. }
  71. SECTION("Boolean") {
  72. DeserializationError err = deserializeJson(doc, "[true,false]");
  73. JsonArray arr = doc.as<JsonArray>();
  74. REQUIRE(err == DeserializationError::Ok);
  75. REQUIRE(2 == arr.size());
  76. REQUIRE(arr[0] == true);
  77. REQUIRE(arr[1] == false);
  78. }
  79. SECTION("Null") {
  80. DeserializationError err = deserializeJson(doc, "[null,null]");
  81. JsonArray arr = doc.as<JsonArray>();
  82. REQUIRE(err == DeserializationError::Ok);
  83. REQUIRE(2 == arr.size());
  84. REQUIRE(arr[0].as<const char*>() == 0);
  85. REQUIRE(arr[1].as<const char*>() == 0);
  86. }
  87. }
  88. SECTION("Quotes") {
  89. SECTION("Double quotes") {
  90. DeserializationError err =
  91. deserializeJson(doc, "[ \"hello\" , \"world\" ]");
  92. JsonArray arr = doc.as<JsonArray>();
  93. REQUIRE(err == DeserializationError::Ok);
  94. REQUIRE(2 == arr.size());
  95. REQUIRE(arr[0] == "hello");
  96. REQUIRE(arr[1] == "world");
  97. }
  98. SECTION("Single quotes") {
  99. DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]");
  100. JsonArray arr = doc.as<JsonArray>();
  101. REQUIRE(err == DeserializationError::Ok);
  102. REQUIRE(2 == arr.size());
  103. REQUIRE(arr[0] == "hello");
  104. REQUIRE(arr[1] == "world");
  105. }
  106. SECTION("No quotes") {
  107. DeserializationError err = deserializeJson(doc, "[ hello , world ]");
  108. REQUIRE(err == DeserializationError::InvalidInput);
  109. }
  110. SECTION("Double quotes (empty strings)") {
  111. DeserializationError err = deserializeJson(doc, "[\"\",\"\"]");
  112. JsonArray arr = doc.as<JsonArray>();
  113. REQUIRE(err == DeserializationError::Ok);
  114. REQUIRE(2 == arr.size());
  115. REQUIRE(arr[0] == "");
  116. REQUIRE(arr[1] == "");
  117. }
  118. SECTION("Single quotes (empty strings)") {
  119. DeserializationError err = deserializeJson(doc, "[\'\',\'\']");
  120. JsonArray arr = doc.as<JsonArray>();
  121. REQUIRE(err == DeserializationError::Ok);
  122. REQUIRE(2 == arr.size());
  123. REQUIRE(arr[0] == "");
  124. REQUIRE(arr[1] == "");
  125. }
  126. SECTION("No quotes (empty strings)") {
  127. DeserializationError err = deserializeJson(doc, "[,]");
  128. REQUIRE(err == DeserializationError::InvalidInput);
  129. }
  130. SECTION("Closing single quotes missing") {
  131. DeserializationError err = deserializeJson(doc, "[\"]");
  132. REQUIRE(err == DeserializationError::IncompleteInput);
  133. }
  134. SECTION("Closing double quotes missing") {
  135. DeserializationError err = deserializeJson(doc, "[\']");
  136. REQUIRE(err == DeserializationError::IncompleteInput);
  137. }
  138. }
  139. SECTION("Premature null-terminator") {
  140. SECTION("After opening bracket") {
  141. DeserializationError err = deserializeJson(doc, "[");
  142. REQUIRE(err == DeserializationError::IncompleteInput);
  143. }
  144. SECTION("After value") {
  145. DeserializationError err = deserializeJson(doc, "[1");
  146. REQUIRE(err == DeserializationError::IncompleteInput);
  147. }
  148. SECTION("After comma") {
  149. DeserializationError err = deserializeJson(doc, "[1,");
  150. REQUIRE(err == DeserializationError::IncompleteInput);
  151. }
  152. }
  153. SECTION("Premature end of input") {
  154. const char* input = "[1,2]";
  155. SECTION("After opening bracket") {
  156. DeserializationError err = deserializeJson(doc, input, 1);
  157. REQUIRE(err == DeserializationError::IncompleteInput);
  158. }
  159. SECTION("After value") {
  160. DeserializationError err = deserializeJson(doc, input, 2);
  161. REQUIRE(err == DeserializationError::IncompleteInput);
  162. }
  163. SECTION("After comma") {
  164. DeserializationError err = deserializeJson(doc, input, 3);
  165. REQUIRE(err == DeserializationError::IncompleteInput);
  166. }
  167. }
  168. SECTION("Misc") {
  169. SECTION("Nested objects") {
  170. char jsonString[] =
  171. " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
  172. DeserializationError err = deserializeJson(doc, jsonString);
  173. JsonArray arr = doc.as<JsonArray>();
  174. JsonObject object1 = arr[0];
  175. const JsonObject object2 = arr[1];
  176. JsonObject object3 = arr[2];
  177. REQUIRE(err == DeserializationError::Ok);
  178. REQUIRE(object1.isNull() == false);
  179. REQUIRE(object2.isNull() == false);
  180. REQUIRE(object3.isNull() == true);
  181. REQUIRE(2 == object1.size());
  182. REQUIRE(2 == object2.size());
  183. REQUIRE(0 == object3.size());
  184. REQUIRE(1 == object1["a"].as<int>());
  185. REQUIRE(2 == object1["b"].as<int>());
  186. REQUIRE(3 == object2["c"].as<int>());
  187. REQUIRE(4 == object2["d"].as<int>());
  188. REQUIRE(0 == object3["e"].as<int>());
  189. }
  190. }
  191. SECTION("Should clear the JsonArray") {
  192. deserializeJson(doc, "[1,2,3,4]");
  193. deserializeJson(doc, "[]");
  194. JsonArray arr = doc.as<JsonArray>();
  195. REQUIRE(arr.size() == 0);
  196. REQUIRE(allocator.log() == AllocatorLog()
  197. << AllocatorLog::Allocate(sizeofPool())
  198. << AllocatorLog::Deallocate(sizeofPool()));
  199. }
  200. }
  201. TEST_CASE("deserialize JSON array under memory constraints") {
  202. TimebombAllocator timebomb(100);
  203. SpyingAllocator allocator(&timebomb);
  204. JsonDocument doc(&allocator);
  205. SECTION("empty array requires no allocation") {
  206. timebomb.setCountdown(0);
  207. char input[] = "[]";
  208. DeserializationError err = deserializeJson(doc, input);
  209. REQUIRE(err == DeserializationError::Ok);
  210. }
  211. SECTION("allocation of pool list fails") {
  212. timebomb.setCountdown(0);
  213. char input[] = "[1]";
  214. DeserializationError err = deserializeJson(doc, input);
  215. REQUIRE(err == DeserializationError::NoMemory);
  216. REQUIRE(doc.as<std::string>() == "[]");
  217. }
  218. SECTION("allocation of pool fails") {
  219. timebomb.setCountdown(0);
  220. char input[] = "[1]";
  221. DeserializationError err = deserializeJson(doc, input);
  222. REQUIRE(err == DeserializationError::NoMemory);
  223. REQUIRE(doc.as<std::string>() == "[]");
  224. }
  225. SECTION("allocation of string fails in array") {
  226. timebomb.setCountdown(1);
  227. char input[] = "[0,\"hi!\"]";
  228. DeserializationError err = deserializeJson(doc, input);
  229. REQUIRE(err == DeserializationError::NoMemory);
  230. REQUIRE(doc.as<std::string>() == "[0,null]");
  231. }
  232. SECTION("don't store space characters") {
  233. deserializeJson(doc, " [ \"1234567\" ] ");
  234. REQUIRE(allocator.log() == AllocatorLog()
  235. << AllocatorLog::Allocate(sizeofPool())
  236. << AllocatorLog::Allocate(sizeofString(31))
  237. << AllocatorLog::Reallocate(
  238. sizeofString(31), sizeofString(7)));
  239. }
  240. }