array.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("deserialize JSON array") {
  7. DynamicJsonDocument doc(4096);
  8. SECTION("An empty array") {
  9. DeserializationError err = deserializeJson(doc, "[]");
  10. JsonArray arr = doc.as<JsonArray>();
  11. REQUIRE(err == DeserializationError::Ok);
  12. REQUIRE(0 == arr.size());
  13. }
  14. SECTION("Spaces") {
  15. SECTION("Before the opening bracket") {
  16. DeserializationError err = deserializeJson(doc, " []");
  17. JsonArray arr = doc.as<JsonArray>();
  18. REQUIRE(err == DeserializationError::Ok);
  19. REQUIRE(0 == arr.size());
  20. }
  21. SECTION("Before first value") {
  22. DeserializationError err = deserializeJson(doc, "[ \t\r\n42]");
  23. JsonArray arr = doc.as<JsonArray>();
  24. REQUIRE(err == DeserializationError::Ok);
  25. REQUIRE(1 == arr.size());
  26. REQUIRE(arr[0] == 42);
  27. }
  28. SECTION("After first value") {
  29. DeserializationError err = deserializeJson(doc, "[42 \t\r\n]");
  30. JsonArray arr = doc.as<JsonArray>();
  31. REQUIRE(err == DeserializationError::Ok);
  32. REQUIRE(1 == arr.size());
  33. REQUIRE(arr[0] == 42);
  34. }
  35. }
  36. SECTION("Values types") {
  37. SECTION("On integer") {
  38. DeserializationError err = deserializeJson(doc, "[42]");
  39. JsonArray arr = doc.as<JsonArray>();
  40. REQUIRE(err == DeserializationError::Ok);
  41. REQUIRE(1 == arr.size());
  42. REQUIRE(arr[0] == 42);
  43. }
  44. SECTION("Two integers") {
  45. DeserializationError err = deserializeJson(doc, "[42,84]");
  46. JsonArray arr = doc.as<JsonArray>();
  47. REQUIRE(err == DeserializationError::Ok);
  48. REQUIRE(2 == arr.size());
  49. REQUIRE(arr[0] == 42);
  50. REQUIRE(arr[1] == 84);
  51. }
  52. SECTION("Double") {
  53. DeserializationError err = deserializeJson(doc, "[4.2,1e2]");
  54. JsonArray arr = doc.as<JsonArray>();
  55. REQUIRE(err == DeserializationError::Ok);
  56. REQUIRE(2 == arr.size());
  57. REQUIRE(arr[0] == 4.2);
  58. REQUIRE(arr[1] == 1e2);
  59. }
  60. SECTION("Unsigned long") {
  61. DeserializationError err = deserializeJson(doc, "[4294967295]");
  62. JsonArray arr = doc.as<JsonArray>();
  63. REQUIRE(err == DeserializationError::Ok);
  64. REQUIRE(1 == arr.size());
  65. REQUIRE(arr[0] == 4294967295UL);
  66. }
  67. SECTION("Boolean") {
  68. DeserializationError err = deserializeJson(doc, "[true,false]");
  69. JsonArray arr = doc.as<JsonArray>();
  70. REQUIRE(err == DeserializationError::Ok);
  71. REQUIRE(2 == arr.size());
  72. REQUIRE(arr[0] == true);
  73. REQUIRE(arr[1] == false);
  74. }
  75. SECTION("Null") {
  76. DeserializationError err = deserializeJson(doc, "[null,null]");
  77. JsonArray arr = doc.as<JsonArray>();
  78. REQUIRE(err == DeserializationError::Ok);
  79. REQUIRE(2 == arr.size());
  80. REQUIRE(arr[0].as<const char*>() == 0);
  81. REQUIRE(arr[1].as<const char*>() == 0);
  82. }
  83. }
  84. SECTION("Quotes") {
  85. SECTION("Double quotes") {
  86. DeserializationError err =
  87. deserializeJson(doc, "[ \"hello\" , \"world\" ]");
  88. JsonArray arr = doc.as<JsonArray>();
  89. REQUIRE(err == DeserializationError::Ok);
  90. REQUIRE(2 == arr.size());
  91. REQUIRE(arr[0] == "hello");
  92. REQUIRE(arr[1] == "world");
  93. }
  94. SECTION("Single quotes") {
  95. DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]");
  96. JsonArray arr = doc.as<JsonArray>();
  97. REQUIRE(err == DeserializationError::Ok);
  98. REQUIRE(2 == arr.size());
  99. REQUIRE(arr[0] == "hello");
  100. REQUIRE(arr[1] == "world");
  101. }
  102. SECTION("No quotes") {
  103. DeserializationError err = deserializeJson(doc, "[ hello , world ]");
  104. REQUIRE(err == DeserializationError::InvalidInput);
  105. }
  106. SECTION("Double quotes (empty strings)") {
  107. DeserializationError err = deserializeJson(doc, "[\"\",\"\"]");
  108. JsonArray arr = doc.as<JsonArray>();
  109. REQUIRE(err == DeserializationError::Ok);
  110. REQUIRE(2 == arr.size());
  111. REQUIRE(arr[0] == "");
  112. REQUIRE(arr[1] == "");
  113. }
  114. SECTION("Single quotes (empty strings)") {
  115. DeserializationError err = deserializeJson(doc, "[\'\',\'\']");
  116. JsonArray arr = doc.as<JsonArray>();
  117. REQUIRE(err == DeserializationError::Ok);
  118. REQUIRE(2 == arr.size());
  119. REQUIRE(arr[0] == "");
  120. REQUIRE(arr[1] == "");
  121. }
  122. SECTION("No quotes (empty strings)") {
  123. DeserializationError err = deserializeJson(doc, "[,]");
  124. REQUIRE(err == DeserializationError::InvalidInput);
  125. }
  126. SECTION("Closing single quotes missing") {
  127. DeserializationError err = deserializeJson(doc, "[\"]");
  128. REQUIRE(err == DeserializationError::IncompleteInput);
  129. }
  130. SECTION("Closing double quotes missing") {
  131. DeserializationError err = deserializeJson(doc, "[\']");
  132. REQUIRE(err == DeserializationError::IncompleteInput);
  133. }
  134. }
  135. SECTION("Premature null-terminator") {
  136. SECTION("After opening bracket") {
  137. DeserializationError err = deserializeJson(doc, "[");
  138. REQUIRE(err == DeserializationError::IncompleteInput);
  139. }
  140. SECTION("After value") {
  141. DeserializationError err = deserializeJson(doc, "[1");
  142. REQUIRE(err == DeserializationError::IncompleteInput);
  143. }
  144. SECTION("After comma") {
  145. DeserializationError err = deserializeJson(doc, "[1,");
  146. REQUIRE(err == DeserializationError::IncompleteInput);
  147. }
  148. }
  149. SECTION("Premature end of input") {
  150. const char* input = "[1,2]";
  151. SECTION("After opening bracket") {
  152. DeserializationError err = deserializeJson(doc, input, 1);
  153. REQUIRE(err == DeserializationError::IncompleteInput);
  154. }
  155. SECTION("After value") {
  156. DeserializationError err = deserializeJson(doc, input, 2);
  157. REQUIRE(err == DeserializationError::IncompleteInput);
  158. }
  159. SECTION("After comma") {
  160. DeserializationError err = deserializeJson(doc, input, 3);
  161. REQUIRE(err == DeserializationError::IncompleteInput);
  162. }
  163. }
  164. SECTION("Misc") {
  165. SECTION("Nested objects") {
  166. char jsonString[] =
  167. " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
  168. DeserializationError err = deserializeJson(doc, jsonString);
  169. JsonArray arr = doc.as<JsonArray>();
  170. JsonObject object1 = arr[0];
  171. const JsonObject object2 = arr[1];
  172. JsonObject object3 = arr[2];
  173. REQUIRE(err == DeserializationError::Ok);
  174. REQUIRE(object1.isNull() == false);
  175. REQUIRE(object2.isNull() == false);
  176. REQUIRE(object3.isNull() == true);
  177. REQUIRE(2 == object1.size());
  178. REQUIRE(2 == object2.size());
  179. REQUIRE(0 == object3.size());
  180. REQUIRE(1 == object1["a"].as<int>());
  181. REQUIRE(2 == object1["b"].as<int>());
  182. REQUIRE(3 == object2["c"].as<int>());
  183. REQUIRE(4 == object2["d"].as<int>());
  184. REQUIRE(0 == object3["e"].as<int>());
  185. }
  186. }
  187. SECTION("Should clear the JsonArray") {
  188. deserializeJson(doc, "[1,2,3,4]");
  189. deserializeJson(doc, "[]");
  190. JsonArray arr = doc.as<JsonArray>();
  191. REQUIRE(arr.size() == 0);
  192. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
  193. }
  194. }