object.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 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 comma") {
  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 comma") {
  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<const char*>() == 0);
  152. REQUIRE(obj["key2"].as<const 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. SECTION("Repeated key with zero copy mode") { // issue #1697
  220. char input[] = "{a:{b:{c:1}},a:2}";
  221. DeserializationError err = deserializeJson(doc, input);
  222. REQUIRE(err == DeserializationError::Ok);
  223. REQUIRE(doc["a"] == 2);
  224. }
  225. SECTION("NUL in keys") { // we don't support NULs in keys
  226. DeserializationError err =
  227. deserializeJson(doc, "{\"x\\u0000a\":1,\"x\\u0000b\":2}");
  228. REQUIRE(err == DeserializationError::Ok);
  229. REQUIRE(doc.as<std::string>() == "{\"x\":2}");
  230. }
  231. }
  232. SECTION("Should clear the JsonObject") {
  233. deserializeJson(doc, "{\"hello\":\"world\"}");
  234. deserializeJson(doc, "{}");
  235. JsonObject obj = doc.as<JsonObject>();
  236. REQUIRE(doc.is<JsonObject>());
  237. REQUIRE(obj.size() == 0);
  238. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  239. }
  240. SECTION("Issue #1335") {
  241. std::string json("{\"a\":{},\"b\":{}}");
  242. deserializeJson(doc, json);
  243. CHECK(doc.as<std::string>() == json);
  244. }
  245. }