object.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. REQUIRE(doc["a"] == 2);
  218. }
  219. }
  220. SECTION("Block comments") {
  221. SECTION("Before opening brace") {
  222. DeserializationError err =
  223. deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
  224. JsonObject obj = doc.as<JsonObject>();
  225. REQUIRE(err == DeserializationError::Ok);
  226. REQUIRE(obj["hello"] == "world");
  227. }
  228. SECTION("After opening brace") {
  229. DeserializationError err =
  230. deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
  231. JsonObject obj = doc.as<JsonObject>();
  232. REQUIRE(err == DeserializationError::Ok);
  233. REQUIRE(obj["hello"] == "world");
  234. }
  235. SECTION("Before colon") {
  236. DeserializationError err =
  237. deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
  238. JsonObject obj = doc.as<JsonObject>();
  239. REQUIRE(err == DeserializationError::Ok);
  240. REQUIRE(obj["hello"] == "world");
  241. }
  242. SECTION("After colon") {
  243. DeserializationError err =
  244. deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
  245. JsonObject obj = doc.as<JsonObject>();
  246. REQUIRE(err == DeserializationError::Ok);
  247. REQUIRE(obj["hello"] == "world");
  248. }
  249. SECTION("Before closing brace") {
  250. DeserializationError err =
  251. deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
  252. JsonObject obj = doc.as<JsonObject>();
  253. REQUIRE(err == DeserializationError::Ok);
  254. REQUIRE(obj["hello"] == "world");
  255. }
  256. SECTION("After closing brace") {
  257. DeserializationError err =
  258. deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
  259. JsonObject obj = doc.as<JsonObject>();
  260. REQUIRE(err == DeserializationError::Ok);
  261. REQUIRE(obj["hello"] == "world");
  262. }
  263. SECTION("Before comma") {
  264. DeserializationError err = deserializeJson(
  265. doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
  266. JsonObject obj = doc.as<JsonObject>();
  267. REQUIRE(err == DeserializationError::Ok);
  268. REQUIRE(obj["hello"] == "world");
  269. REQUIRE(obj["answer"] == 42);
  270. }
  271. SECTION("After comma") {
  272. DeserializationError err = deserializeJson(
  273. doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
  274. JsonObject obj = doc.as<JsonObject>();
  275. REQUIRE(err == DeserializationError::Ok);
  276. REQUIRE(obj["hello"] == "world");
  277. REQUIRE(obj["answer"] == 42);
  278. }
  279. }
  280. SECTION("Trailing comments") {
  281. SECTION("Before opening brace") {
  282. DeserializationError err =
  283. deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
  284. JsonObject obj = doc.as<JsonObject>();
  285. REQUIRE(err == DeserializationError::Ok);
  286. REQUIRE(obj["hello"] == "world");
  287. }
  288. SECTION("After opening brace") {
  289. DeserializationError err =
  290. deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
  291. JsonObject obj = doc.as<JsonObject>();
  292. REQUIRE(err == DeserializationError::Ok);
  293. REQUIRE(obj["hello"] == "world");
  294. }
  295. SECTION("Before colon") {
  296. DeserializationError err =
  297. deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
  298. JsonObject obj = doc.as<JsonObject>();
  299. REQUIRE(err == DeserializationError::Ok);
  300. REQUIRE(obj["hello"] == "world");
  301. }
  302. SECTION("After colon") {
  303. DeserializationError err =
  304. deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
  305. JsonObject obj = doc.as<JsonObject>();
  306. REQUIRE(err == DeserializationError::Ok);
  307. REQUIRE(obj["hello"] == "world");
  308. }
  309. SECTION("Before closing brace") {
  310. DeserializationError err =
  311. deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
  312. JsonObject obj = doc.as<JsonObject>();
  313. REQUIRE(err == DeserializationError::Ok);
  314. REQUIRE(obj["hello"] == "world");
  315. }
  316. SECTION("After closing brace") {
  317. DeserializationError err =
  318. deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
  319. JsonObject obj = doc.as<JsonObject>();
  320. REQUIRE(err == DeserializationError::Ok);
  321. REQUIRE(obj["hello"] == "world");
  322. }
  323. SECTION("Before comma") {
  324. DeserializationError err = deserializeJson(
  325. doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
  326. JsonObject obj = doc.as<JsonObject>();
  327. REQUIRE(err == DeserializationError::Ok);
  328. REQUIRE(obj["hello"] == "world");
  329. REQUIRE(obj["answer"] == 42);
  330. }
  331. SECTION("After comma") {
  332. DeserializationError err = deserializeJson(
  333. doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
  334. JsonObject obj = doc.as<JsonObject>();
  335. REQUIRE(err == DeserializationError::Ok);
  336. REQUIRE(obj["hello"] == "world");
  337. REQUIRE(obj["answer"] == 42);
  338. }
  339. }
  340. SECTION("Dangling slash") {
  341. SECTION("Before opening brace") {
  342. DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
  343. REQUIRE(err == DeserializationError::InvalidInput);
  344. }
  345. SECTION("After opening brace") {
  346. DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
  347. REQUIRE(err == DeserializationError::InvalidInput);
  348. }
  349. SECTION("Before colon") {
  350. DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
  351. REQUIRE(err == DeserializationError::InvalidInput);
  352. }
  353. SECTION("After colon") {
  354. DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
  355. REQUIRE(err == DeserializationError::InvalidInput);
  356. }
  357. SECTION("Before closing brace") {
  358. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
  359. REQUIRE(err == DeserializationError::InvalidInput);
  360. }
  361. SECTION("After closing brace") {
  362. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
  363. JsonObject obj = doc.as<JsonObject>();
  364. REQUIRE(err == DeserializationError::Ok);
  365. REQUIRE(obj["hello"] == "world");
  366. }
  367. SECTION("Before comma") {
  368. DeserializationError err =
  369. deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
  370. REQUIRE(err == DeserializationError::InvalidInput);
  371. }
  372. SECTION("After comma") {
  373. DeserializationError err =
  374. deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
  375. REQUIRE(err == DeserializationError::InvalidInput);
  376. }
  377. }
  378. SECTION("Should clear the JsonObject") {
  379. deserializeJson(doc, "{\"hello\":\"world\"}");
  380. deserializeJson(doc, "{}");
  381. JsonObject obj = doc.as<JsonObject>();
  382. REQUIRE(doc.is<JsonObject>());
  383. REQUIRE(obj.size() == 0);
  384. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  385. }
  386. }