array.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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<char*>() == 0);
  81. REQUIRE(arr[1].as<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("Block comments") {
  136. SECTION("Before opening bracket") {
  137. DeserializationError err =
  138. deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
  139. JsonArray arr = doc.as<JsonArray>();
  140. REQUIRE(err == DeserializationError::Ok);
  141. REQUIRE(1 == arr.size());
  142. REQUIRE(arr[0] == "hello");
  143. }
  144. SECTION("After opening bracket") {
  145. DeserializationError err =
  146. deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
  147. JsonArray arr = doc.as<JsonArray>();
  148. REQUIRE(err == DeserializationError::Ok);
  149. REQUIRE(1 == arr.size());
  150. REQUIRE(arr[0] == "hello");
  151. }
  152. SECTION("Before closing bracket") {
  153. DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
  154. JsonArray arr = doc.as<JsonArray>();
  155. REQUIRE(err == DeserializationError::Ok);
  156. REQUIRE(1 == arr.size());
  157. REQUIRE(arr[0] == "hello");
  158. }
  159. SECTION("After closing bracket") {
  160. DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
  161. JsonArray arr = doc.as<JsonArray>();
  162. REQUIRE(err == DeserializationError::Ok);
  163. REQUIRE(1 == arr.size());
  164. REQUIRE(arr[0] == "hello");
  165. }
  166. SECTION("Before comma") {
  167. DeserializationError err =
  168. deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
  169. JsonArray arr = doc.as<JsonArray>();
  170. REQUIRE(err == DeserializationError::Ok);
  171. REQUIRE(2 == arr.size());
  172. REQUIRE(arr[0] == "hello");
  173. REQUIRE(arr[1] == "world");
  174. }
  175. SECTION("After comma") {
  176. DeserializationError err =
  177. deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
  178. JsonArray arr = doc.as<JsonArray>();
  179. REQUIRE(err == DeserializationError::Ok);
  180. REQUIRE(2 == arr.size());
  181. REQUIRE(arr[0] == "hello");
  182. REQUIRE(arr[1] == "world");
  183. }
  184. SECTION("/*/") {
  185. DeserializationError err = deserializeJson(doc, "[/*/\n]");
  186. REQUIRE(err == DeserializationError::IncompleteInput);
  187. }
  188. SECTION("Unfinished comment") {
  189. DeserializationError err = deserializeJson(doc, "[/*COMMENT]");
  190. REQUIRE(err == DeserializationError::IncompleteInput);
  191. }
  192. SECTION("Final slash missing") {
  193. DeserializationError err = deserializeJson(doc, "[/*COMMENT*]");
  194. REQUIRE(err == DeserializationError::IncompleteInput);
  195. }
  196. }
  197. SECTION("Trailing comments") {
  198. SECTION("Before opening bracket") {
  199. DeserializationError err =
  200. deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
  201. JsonArray arr = doc.as<JsonArray>();
  202. REQUIRE(err == DeserializationError::Ok);
  203. REQUIRE(1 == arr.size());
  204. REQUIRE(arr[0] == "hello");
  205. }
  206. SECTION("After opening bracket") {
  207. DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
  208. JsonArray arr = doc.as<JsonArray>();
  209. REQUIRE(err == DeserializationError::Ok);
  210. REQUIRE(1 == arr.size());
  211. REQUIRE(arr[0] == "hello");
  212. }
  213. SECTION("Before closing bracket") {
  214. DeserializationError err =
  215. deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
  216. JsonArray arr = doc.as<JsonArray>();
  217. REQUIRE(err == DeserializationError::Ok);
  218. REQUIRE(1 == arr.size());
  219. REQUIRE(arr[0] == "hello");
  220. }
  221. SECTION("After closing bracket") {
  222. DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
  223. JsonArray arr = doc.as<JsonArray>();
  224. REQUIRE(err == DeserializationError::Ok);
  225. REQUIRE(1 == arr.size());
  226. REQUIRE(arr[0] == "hello");
  227. }
  228. SECTION("Before comma") {
  229. DeserializationError err =
  230. deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
  231. JsonArray arr = doc.as<JsonArray>();
  232. REQUIRE(err == DeserializationError::Ok);
  233. REQUIRE(2 == arr.size());
  234. REQUIRE(arr[0] == "hello");
  235. REQUIRE(arr[1] == "world");
  236. }
  237. SECTION("After comma") {
  238. DeserializationError err =
  239. deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
  240. JsonArray arr = doc.as<JsonArray>();
  241. REQUIRE(err == DeserializationError::Ok);
  242. REQUIRE(2 == arr.size());
  243. REQUIRE(arr[0] == "hello");
  244. REQUIRE(arr[1] == "world");
  245. }
  246. SECTION("Invalid comment") {
  247. DeserializationError err = deserializeJson(doc, "[/COMMENT\n]");
  248. REQUIRE(err == DeserializationError::InvalidInput);
  249. }
  250. SECTION("End document with comment") {
  251. DeserializationError err = deserializeJson(doc, "[//COMMENT");
  252. REQUIRE(err == DeserializationError::IncompleteInput);
  253. }
  254. }
  255. SECTION("Premature null-terminator") {
  256. SECTION("After opening bracket") {
  257. DeserializationError err = deserializeJson(doc, "[");
  258. REQUIRE(err == DeserializationError::IncompleteInput);
  259. }
  260. SECTION("After value") {
  261. DeserializationError err = deserializeJson(doc, "[1");
  262. REQUIRE(err == DeserializationError::IncompleteInput);
  263. }
  264. SECTION("After comma") {
  265. DeserializationError err = deserializeJson(doc, "[1,");
  266. REQUIRE(err == DeserializationError::IncompleteInput);
  267. }
  268. }
  269. SECTION("Premature end of input") {
  270. const char* input = "[1,2]";
  271. SECTION("After opening bracket") {
  272. DeserializationError err = deserializeJson(doc, input, 1);
  273. REQUIRE(err == DeserializationError::IncompleteInput);
  274. }
  275. SECTION("After value") {
  276. DeserializationError err = deserializeJson(doc, input, 2);
  277. REQUIRE(err == DeserializationError::IncompleteInput);
  278. }
  279. SECTION("After comma") {
  280. DeserializationError err = deserializeJson(doc, input, 3);
  281. REQUIRE(err == DeserializationError::IncompleteInput);
  282. }
  283. }
  284. SECTION("Misc") {
  285. SECTION("Nested objects") {
  286. char jsonString[] =
  287. " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
  288. DeserializationError err = deserializeJson(doc, jsonString);
  289. JsonArray arr = doc.as<JsonArray>();
  290. JsonObject object1 = arr[0];
  291. const JsonObject object2 = arr[1];
  292. JsonObject object3 = arr[2];
  293. REQUIRE(err == DeserializationError::Ok);
  294. REQUIRE(object1.isNull() == false);
  295. REQUIRE(object2.isNull() == false);
  296. REQUIRE(object3.isNull() == true);
  297. REQUIRE(2 == object1.size());
  298. REQUIRE(2 == object2.size());
  299. REQUIRE(0 == object3.size());
  300. REQUIRE(1 == object1["a"].as<int>());
  301. REQUIRE(2 == object1["b"].as<int>());
  302. REQUIRE(3 == object2["c"].as<int>());
  303. REQUIRE(4 == object2["d"].as<int>());
  304. REQUIRE(0 == object3["e"].as<int>());
  305. }
  306. }
  307. SECTION("Should clear the JsonArray") {
  308. deserializeJson(doc, "[1,2,3,4]");
  309. deserializeJson(doc, "[]");
  310. JsonArray arr = doc.as<JsonArray>();
  311. REQUIRE(arr.size() == 0);
  312. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
  313. }
  314. }