object.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 object") {
  7. DynamicJsonDocument doc(4096);
  8. SECTION("An empty object") {
  9. DeserializationError err = deserializeJson(doc, "{}");
  10. JsonObject obj = doc.as<JsonObject>();
  11. REQUIRE(err == DeserializationError::Ok);
  12. REQUIRE(doc.is<JsonObject>());
  13. REQUIRE(obj.size() == 0);
  14. }
  15. SECTION("Quotes") {
  16. SECTION("Double quotes") {
  17. DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}");
  18. JsonObject obj = doc.as<JsonObject>();
  19. REQUIRE(err == DeserializationError::Ok);
  20. REQUIRE(doc.is<JsonObject>());
  21. REQUIRE(obj.size() == 1);
  22. REQUIRE(obj["key"] == "value");
  23. }
  24. SECTION("Single quotes") {
  25. DeserializationError err = deserializeJson(doc, "{'key':'value'}");
  26. JsonObject obj = doc.as<JsonObject>();
  27. REQUIRE(err == DeserializationError::Ok);
  28. REQUIRE(doc.is<JsonObject>());
  29. REQUIRE(obj.size() == 1);
  30. REQUIRE(obj["key"] == "value");
  31. }
  32. SECTION("No quotes") {
  33. DeserializationError err = deserializeJson(doc, "{key:'value'}");
  34. JsonObject obj = doc.as<JsonObject>();
  35. REQUIRE(err == DeserializationError::Ok);
  36. REQUIRE(doc.is<JsonObject>());
  37. REQUIRE(obj.size() == 1);
  38. REQUIRE(obj["key"] == "value");
  39. }
  40. SECTION("No quotes, allow underscore in key") {
  41. DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}");
  42. JsonObject obj = doc.as<JsonObject>();
  43. REQUIRE(err == DeserializationError::Ok);
  44. REQUIRE(doc.is<JsonObject>());
  45. REQUIRE(obj.size() == 1);
  46. REQUIRE(obj["_k_e_y_"] == 42);
  47. }
  48. }
  49. SECTION("Spaces") {
  50. SECTION("Before the key") {
  51. DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}");
  52. JsonObject obj = doc.as<JsonObject>();
  53. REQUIRE(err == DeserializationError::Ok);
  54. REQUIRE(doc.is<JsonObject>());
  55. REQUIRE(obj.size() == 1);
  56. REQUIRE(obj["key"] == "value");
  57. }
  58. SECTION("After the key") {
  59. DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}");
  60. JsonObject obj = doc.as<JsonObject>();
  61. REQUIRE(err == DeserializationError::Ok);
  62. REQUIRE(doc.is<JsonObject>());
  63. REQUIRE(obj.size() == 1);
  64. REQUIRE(obj["key"] == "value");
  65. }
  66. SECTION("Before the value") {
  67. DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}");
  68. JsonObject obj = doc.as<JsonObject>();
  69. REQUIRE(err == DeserializationError::Ok);
  70. REQUIRE(doc.is<JsonObject>());
  71. REQUIRE(obj.size() == 1);
  72. REQUIRE(obj["key"] == "value");
  73. }
  74. SECTION("After the value") {
  75. DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }");
  76. JsonObject obj = doc.as<JsonObject>();
  77. REQUIRE(err == DeserializationError::Ok);
  78. REQUIRE(doc.is<JsonObject>());
  79. REQUIRE(obj.size() == 1);
  80. REQUIRE(obj["key"] == "value");
  81. }
  82. SECTION("Before the colon") {
  83. DeserializationError err =
  84. deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
  85. JsonObject obj = doc.as<JsonObject>();
  86. REQUIRE(err == DeserializationError::Ok);
  87. REQUIRE(doc.is<JsonObject>());
  88. REQUIRE(obj.size() == 2);
  89. REQUIRE(obj["key1"] == "value1");
  90. REQUIRE(obj["key2"] == "value2");
  91. }
  92. SECTION("After the colon") {
  93. DeserializationError err =
  94. deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
  95. JsonObject obj = doc.as<JsonObject>();
  96. REQUIRE(err == DeserializationError::Ok);
  97. REQUIRE(doc.is<JsonObject>());
  98. REQUIRE(obj.size() == 2);
  99. REQUIRE(obj["key1"] == "value1");
  100. REQUIRE(obj["key2"] == "value2");
  101. }
  102. }
  103. SECTION("Values types") {
  104. SECTION("String") {
  105. DeserializationError err =
  106. deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
  107. JsonObject obj = doc.as<JsonObject>();
  108. REQUIRE(err == DeserializationError::Ok);
  109. REQUIRE(doc.is<JsonObject>());
  110. REQUIRE(obj.size() == 2);
  111. REQUIRE(obj["key1"] == "value1");
  112. REQUIRE(obj["key2"] == "value2");
  113. }
  114. SECTION("Integer") {
  115. DeserializationError err =
  116. deserializeJson(doc, "{\"key1\":42,\"key2\":-42}");
  117. JsonObject obj = doc.as<JsonObject>();
  118. REQUIRE(err == DeserializationError::Ok);
  119. REQUIRE(doc.is<JsonObject>());
  120. REQUIRE(obj.size() == 2);
  121. REQUIRE(obj["key1"] == 42);
  122. REQUIRE(obj["key2"] == -42);
  123. }
  124. SECTION("Double") {
  125. DeserializationError err =
  126. deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
  127. JsonObject obj = doc.as<JsonObject>();
  128. REQUIRE(err == DeserializationError::Ok);
  129. REQUIRE(doc.is<JsonObject>());
  130. REQUIRE(obj.size() == 2);
  131. REQUIRE(obj["key1"] == 12.345);
  132. REQUIRE(obj["key2"] == -7E89);
  133. }
  134. SECTION("Booleans") {
  135. DeserializationError err =
  136. deserializeJson(doc, "{\"key1\":true,\"key2\":false}");
  137. JsonObject obj = doc.as<JsonObject>();
  138. REQUIRE(err == DeserializationError::Ok);
  139. REQUIRE(doc.is<JsonObject>());
  140. REQUIRE(obj.size() == 2);
  141. REQUIRE(obj["key1"] == true);
  142. REQUIRE(obj["key2"] == false);
  143. }
  144. SECTION("Null") {
  145. DeserializationError err =
  146. deserializeJson(doc, "{\"key1\":null,\"key2\":null}");
  147. JsonObject obj = doc.as<JsonObject>();
  148. REQUIRE(err == DeserializationError::Ok);
  149. REQUIRE(doc.is<JsonObject>());
  150. REQUIRE(obj.size() == 2);
  151. REQUIRE(obj["key1"].as<char*>() == 0);
  152. REQUIRE(obj["key2"].as<char*>() == 0);
  153. }
  154. SECTION("Array") {
  155. char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
  156. DeserializationError err = deserializeJson(doc, jsonString);
  157. JsonObject obj = doc.as<JsonObject>();
  158. JsonArray array1 = obj["ab"];
  159. const JsonArray array2 = obj["cd"];
  160. JsonArray array3 = obj["ef"];
  161. REQUIRE(err == DeserializationError::Ok);
  162. REQUIRE(array1.isNull() == false);
  163. REQUIRE(array2.isNull() == false);
  164. REQUIRE(array3.isNull() == true);
  165. REQUIRE(2 == array1.size());
  166. REQUIRE(2 == array2.size());
  167. REQUIRE(0 == array3.size());
  168. REQUIRE(1 == array1[0].as<int>());
  169. REQUIRE(2 == array1[1].as<int>());
  170. REQUIRE(3 == array2[0].as<int>());
  171. REQUIRE(4 == array2[1].as<int>());
  172. REQUIRE(0 == array3[0].as<int>());
  173. }
  174. }
  175. SECTION("Premature null terminator") {
  176. SECTION("After opening brace") {
  177. DeserializationError err = deserializeJson(doc, "{");
  178. REQUIRE(err == DeserializationError::IncompleteInput);
  179. }
  180. SECTION("After key") {
  181. DeserializationError err = deserializeJson(doc, "{\"hello\"");
  182. REQUIRE(err == DeserializationError::IncompleteInput);
  183. }
  184. SECTION("After colon") {
  185. DeserializationError err = deserializeJson(doc, "{\"hello\":");
  186. REQUIRE(err == DeserializationError::IncompleteInput);
  187. }
  188. SECTION("After value") {
  189. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"");
  190. REQUIRE(err == DeserializationError::IncompleteInput);
  191. }
  192. SECTION("After comma") {
  193. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\",");
  194. REQUIRE(err == DeserializationError::IncompleteInput);
  195. }
  196. }
  197. SECTION("Misc") {
  198. SECTION("A quoted key without value") {
  199. DeserializationError err = deserializeJson(doc, "{\"key\"}");
  200. REQUIRE(err == DeserializationError::InvalidInput);
  201. }
  202. SECTION("A non-quoted key without value") {
  203. DeserializationError err = deserializeJson(doc, "{key}");
  204. REQUIRE(err == DeserializationError::InvalidInput);
  205. }
  206. SECTION("A dangling comma") {
  207. DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\",}");
  208. REQUIRE(err == DeserializationError::InvalidInput);
  209. }
  210. SECTION("null as a key") {
  211. DeserializationError err = deserializeJson(doc, "{null:\"value\"}");
  212. REQUIRE(err == DeserializationError::Ok);
  213. }
  214. SECTION("Repeated key") {
  215. DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
  216. REQUIRE(err == DeserializationError::Ok);
  217. }
  218. }
  219. SECTION("Block comments") {
  220. SECTION("Before opening brace") {
  221. DeserializationError err =
  222. deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
  223. JsonObject obj = doc.as<JsonObject>();
  224. REQUIRE(err == DeserializationError::Ok);
  225. REQUIRE(obj["hello"] == "world");
  226. }
  227. SECTION("After opening brace") {
  228. DeserializationError err =
  229. deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
  230. JsonObject obj = doc.as<JsonObject>();
  231. REQUIRE(err == DeserializationError::Ok);
  232. REQUIRE(obj["hello"] == "world");
  233. }
  234. SECTION("Before colon") {
  235. DeserializationError err =
  236. deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
  237. JsonObject obj = doc.as<JsonObject>();
  238. REQUIRE(err == DeserializationError::Ok);
  239. REQUIRE(obj["hello"] == "world");
  240. }
  241. SECTION("After colon") {
  242. DeserializationError err =
  243. deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
  244. JsonObject obj = doc.as<JsonObject>();
  245. REQUIRE(err == DeserializationError::Ok);
  246. REQUIRE(obj["hello"] == "world");
  247. }
  248. SECTION("Before closing brace") {
  249. DeserializationError err =
  250. deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
  251. JsonObject obj = doc.as<JsonObject>();
  252. REQUIRE(err == DeserializationError::Ok);
  253. REQUIRE(obj["hello"] == "world");
  254. }
  255. SECTION("After closing brace") {
  256. DeserializationError err =
  257. deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
  258. JsonObject obj = doc.as<JsonObject>();
  259. REQUIRE(err == DeserializationError::Ok);
  260. REQUIRE(obj["hello"] == "world");
  261. }
  262. SECTION("Before comma") {
  263. DeserializationError err = deserializeJson(
  264. doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
  265. JsonObject obj = doc.as<JsonObject>();
  266. REQUIRE(err == DeserializationError::Ok);
  267. REQUIRE(obj["hello"] == "world");
  268. REQUIRE(obj["answer"] == 42);
  269. }
  270. SECTION("After comma") {
  271. DeserializationError err = deserializeJson(
  272. doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
  273. JsonObject obj = doc.as<JsonObject>();
  274. REQUIRE(err == DeserializationError::Ok);
  275. REQUIRE(obj["hello"] == "world");
  276. REQUIRE(obj["answer"] == 42);
  277. }
  278. }
  279. SECTION("Trailing comments") {
  280. SECTION("Before opening brace") {
  281. DeserializationError err =
  282. deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
  283. JsonObject obj = doc.as<JsonObject>();
  284. REQUIRE(err == DeserializationError::Ok);
  285. REQUIRE(obj["hello"] == "world");
  286. }
  287. SECTION("After opening brace") {
  288. DeserializationError err =
  289. deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
  290. JsonObject obj = doc.as<JsonObject>();
  291. REQUIRE(err == DeserializationError::Ok);
  292. REQUIRE(obj["hello"] == "world");
  293. }
  294. SECTION("Before colon") {
  295. DeserializationError err =
  296. deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
  297. JsonObject obj = doc.as<JsonObject>();
  298. REQUIRE(err == DeserializationError::Ok);
  299. REQUIRE(obj["hello"] == "world");
  300. }
  301. SECTION("After colon") {
  302. DeserializationError err =
  303. deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
  304. JsonObject obj = doc.as<JsonObject>();
  305. REQUIRE(err == DeserializationError::Ok);
  306. REQUIRE(obj["hello"] == "world");
  307. }
  308. SECTION("Before closing brace") {
  309. DeserializationError err =
  310. deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
  311. JsonObject obj = doc.as<JsonObject>();
  312. REQUIRE(err == DeserializationError::Ok);
  313. REQUIRE(obj["hello"] == "world");
  314. }
  315. SECTION("After closing brace") {
  316. DeserializationError err =
  317. deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
  318. JsonObject obj = doc.as<JsonObject>();
  319. REQUIRE(err == DeserializationError::Ok);
  320. REQUIRE(obj["hello"] == "world");
  321. }
  322. SECTION("Before comma") {
  323. DeserializationError err = deserializeJson(
  324. doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
  325. JsonObject obj = doc.as<JsonObject>();
  326. REQUIRE(err == DeserializationError::Ok);
  327. REQUIRE(obj["hello"] == "world");
  328. REQUIRE(obj["answer"] == 42);
  329. }
  330. SECTION("After comma") {
  331. DeserializationError err = deserializeJson(
  332. doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
  333. JsonObject obj = doc.as<JsonObject>();
  334. REQUIRE(err == DeserializationError::Ok);
  335. REQUIRE(obj["hello"] == "world");
  336. REQUIRE(obj["answer"] == 42);
  337. }
  338. }
  339. SECTION("Dangling slash") {
  340. SECTION("Before opening brace") {
  341. DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
  342. REQUIRE(err == DeserializationError::InvalidInput);
  343. }
  344. SECTION("After opening brace") {
  345. DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
  346. REQUIRE(err == DeserializationError::InvalidInput);
  347. }
  348. SECTION("Before colon") {
  349. DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
  350. REQUIRE(err == DeserializationError::InvalidInput);
  351. }
  352. SECTION("After colon") {
  353. DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
  354. REQUIRE(err == DeserializationError::InvalidInput);
  355. }
  356. SECTION("Before closing brace") {
  357. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
  358. REQUIRE(err == DeserializationError::InvalidInput);
  359. }
  360. SECTION("After closing brace") {
  361. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
  362. JsonObject obj = doc.as<JsonObject>();
  363. REQUIRE(err == DeserializationError::Ok);
  364. REQUIRE(obj["hello"] == "world");
  365. }
  366. SECTION("Before comma") {
  367. DeserializationError err =
  368. deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
  369. REQUIRE(err == DeserializationError::InvalidInput);
  370. }
  371. SECTION("After comma") {
  372. DeserializationError err =
  373. deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
  374. REQUIRE(err == DeserializationError::InvalidInput);
  375. }
  376. }
  377. SECTION("Should clear the JsonObject") {
  378. deserializeJson(doc, "{\"hello\":\"world\"}");
  379. deserializeJson(doc, "{}");
  380. JsonObject obj = doc.as<JsonObject>();
  381. REQUIRE(doc.is<JsonObject>());
  382. REQUIRE(obj.size() == 0);
  383. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  384. }
  385. }