RyanJsonBaseTestLoadJson.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "RyanJsonBaseTest.h"
  2. /* --------------------------------------------------------------------- */
  3. RyanJsonBool_e RyanJsonBaseTestLoadJson(void)
  4. {
  5. char *str = NULL;
  6. RyanJson_t json;
  7. char *jsonstr = "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":"
  8. "{\"inter\":16,\"double\":16."
  9. "89,\"string\":\"hello\","
  10. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,"
  11. "16.89,16.89,16.89],"
  12. "\"arrayString\":[\"hello\",\"hello\","
  13. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  14. "\"double\":16.89,\"string\":"
  15. "\"hello\",\"boolTrue\":true,"
  16. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  17. "\"boolFalse\":false,\"null\":null}],\"unicode\":\"😀\"}";
  18. json = RyanJsonParse(jsonstr);
  19. RyanJsonCheckReturnFalse(NULL != json);
  20. str = RyanJsonPrint(json, 250, RyanJsonFalse, NULL);
  21. RyanJsonCheckCode(0 == strcmp(str, jsonstr), {
  22. RyanJsonFree(str);
  23. RyanJsonDelete(json);
  24. return RyanJsonFalse;
  25. });
  26. RyanJsonFree(str);
  27. RyanJsonCheckCode(RyanJsonTrue == RyanJsonBaseTestCheckRoot(json), {
  28. RyanJsonDelete(json);
  29. return RyanJsonFalse;
  30. });
  31. RyanJsonDelete(json);
  32. /**
  33. * @brief 测试 Unicode
  34. *
  35. */
  36. char printfBuf[1024] = {0};
  37. json = RyanJsonParse("{\"emoji\":\"\\uD83D\\uDE00\"}");
  38. RyanJsonCheckReturnFalse(NULL != json);
  39. str = RyanJsonPrintPreallocated(json, printfBuf, sizeof(printfBuf), RyanJsonFalse, NULL);
  40. RyanJsonDelete(json);
  41. // 测试数字 0-9 分支: \u0030 = '0', \u0039 = '9'
  42. json = RyanJsonParse("{\"num\":\"\\u0030\\u0039\"}");
  43. RyanJsonCheckReturnFalse(NULL != json);
  44. str = RyanJsonPrintPreallocated(json, printfBuf, sizeof(printfBuf), RyanJsonFalse, NULL);
  45. RyanJsonCheckCode(0 == strcmp(str, "{\"num\":\"09\"}"), {
  46. RyanJsonDelete(json);
  47. return RyanJsonFalse;
  48. });
  49. RyanJsonDelete(json);
  50. // 测试小写 a-f 分支: \u0061 = 'a', \u0066 = 'f'
  51. json = RyanJsonParse("{\"lower\":\"\\u0061\\u0062\\u0063\\u0064\\u0065\\u0066\"}");
  52. RyanJsonCheckReturnFalse(NULL != json);
  53. str = RyanJsonPrintPreallocated(json, printfBuf, sizeof(printfBuf), RyanJsonFalse, NULL);
  54. RyanJsonCheckCode(0 == strcmp(str, "{\"lower\":\"abcdef\"}"), {
  55. RyanJsonDelete(json);
  56. return RyanJsonFalse;
  57. });
  58. RyanJsonDelete(json);
  59. // 测试大写 A-F 分支: \u0041 = 'A', \u0046 = 'F'
  60. json = RyanJsonParse("{\"upper\":\"\\u0041\\u0042\\u0043\\u0044\\u0045\\u0046\"}");
  61. RyanJsonCheckReturnFalse(NULL != json);
  62. str = RyanJsonPrintPreallocated(json, printfBuf, sizeof(printfBuf), RyanJsonFalse, NULL);
  63. RyanJsonCheckCode(0 == strcmp(str, "{\"upper\":\"ABCDEF\"}"), {
  64. RyanJsonDelete(json);
  65. return RyanJsonFalse;
  66. });
  67. RyanJsonDelete(json);
  68. // 测试混合大小写: \uAbCd (混合大小写十六进制)
  69. json = RyanJsonParse("{\"mixed\":\"\\uAbCd\"}");
  70. RyanJsonCheckReturnFalse(NULL != json);
  71. RyanJsonFree(RyanJsonPrint(json, 50, RyanJsonFalse, NULL));
  72. RyanJsonDelete(json);
  73. // 测试 default 分支 (非法十六进制字符 'G')
  74. json = RyanJsonParse("{\"invalid\":\"\\uGGGG\"}");
  75. RyanJsonCheckCode(NULL == json, {
  76. RyanJsonDelete(json);
  77. return RyanJsonFalse;
  78. });
  79. // 测试 default 分支 (非法十六进制字符 'Z')
  80. json = RyanJsonParse("{\"invalid\":\"\\u00ZZ\"}");
  81. RyanJsonCheckCode(NULL == json, {
  82. RyanJsonDelete(json);
  83. return RyanJsonFalse;
  84. });
  85. // 测试 default 分支 (非法十六进制字符 '!')
  86. json = RyanJsonParse("{\"invalid\":\"\\u00!!\"}");
  87. RyanJsonCheckCode(NULL == json, {
  88. RyanJsonDelete(json);
  89. return RyanJsonFalse;
  90. });
  91. /**
  92. * @brief 测试序列化错误json结构
  93. *
  94. */
  95. // \"inter\":16poi, 无效数字
  96. json = RyanJsonParse("{\"inter\":16poi,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  97. "\"item\":{\"inter\":"
  98. "16,\"double\":16.89,\"string\":\"hello\","
  99. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  100. "89,16.89,16.89,16."
  101. "89],\"arrayString\":[\"hello\",\"hello\","
  102. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  103. "\"double\":16.89,"
  104. "\"string\":\"hello\",\"boolTrue\":true,"
  105. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  106. "\"boolFalse\":false,"
  107. "\"null\":null}]}");
  108. RyanJsonCheckCode(NULL == json, {
  109. RyanJsonDelete(json);
  110. return RyanJsonFalse;
  111. });
  112. // \"double\":16.8yu9,, 无效浮点数
  113. json = RyanJsonParse("{\"inter\":16,\"double\":16.8yu9,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  114. "\"item\":{\"inter\":16,"
  115. "\"double\":16.89,\"string\":\"hello\","
  116. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  117. "89,16.89,16.89,16."
  118. "89],\"arrayString\":[\"hello\",\"hello\","
  119. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  120. "\"double\":16.89,"
  121. "\"string\":\"hello\",\"boolTrue\":true,"
  122. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  123. "\"boolFalse\":false,"
  124. "\"null\":null}]}");
  125. RyanJsonCheckCode(NULL == json, {
  126. RyanJsonDelete(json);
  127. return RyanJsonFalse;
  128. });
  129. // boolTrue 设置为 tru
  130. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":tru,\"boolFalse\":false,\"null\":null,"
  131. "\"item\":{\"inter\":16,"
  132. "\"double\":16.89,\"string\":\"hello\","
  133. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  134. "89,16.89,16.89,16."
  135. "89],\"arrayString\":[\"hello\",\"hello\","
  136. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  137. "\"double\":16.89,"
  138. "\"string\":\"hello\",\"boolTrue\":true,"
  139. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  140. "\"boolFalse\":false,"
  141. "\"null\":null}]}");
  142. RyanJsonCheckCode(NULL == json, {
  143. RyanJsonDelete(json);
  144. return RyanJsonFalse;
  145. });
  146. // boolFalse 设置为 fale
  147. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":fale,\"null\":null,"
  148. "\"item\":{\"inter\":16,"
  149. "\"double\":16.89,\"string\":\"hello\","
  150. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  151. "89,16.89,16.89,16."
  152. "89],\"arrayString\":[\"hello\",\"hello\","
  153. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  154. "\"double\":16.89,"
  155. "\"string\":\"hello\",\"boolTrue\":true,"
  156. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  157. "\"boolFalse\":false,"
  158. "\"null\":null}]}");
  159. RyanJsonCheckCode(NULL == json, {
  160. RyanJsonDelete(json);
  161. return RyanJsonFalse;
  162. });
  163. // null 设置为 nul
  164. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":nul,"
  165. "\"item\":{\"inter\":16,"
  166. "\"double\":16.89,\"string\":\"hello\","
  167. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  168. "89,16.89,16.89,16."
  169. "89],\"arrayString\":[\"hello\",\"hello\","
  170. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  171. "\"double\":16.89,"
  172. "\"string\":\"hello\",\"boolTrue\":true,"
  173. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  174. "\"boolFalse\":false,"
  175. "\"null\":null}]}");
  176. RyanJsonCheckCode(NULL == json, {
  177. RyanJsonDelete(json);
  178. return RyanJsonFalse;
  179. });
  180. // null 设置为 NULL
  181. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":NULL,"
  182. "\"item\":{\"inter\":16,"
  183. "\"double\":16.89,\"string\":\"hello\","
  184. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  185. "89,16.89,16.89,16."
  186. "89],\"arrayString\":[\"hello\",\"hello\","
  187. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  188. "\"double\":16.89,"
  189. "\"string\":\"hello\",\"boolTrue\":true,"
  190. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  191. "\"boolFalse\":false,"
  192. "\"null\":null}]}");
  193. RyanJsonCheckCode(NULL == json, {
  194. RyanJsonDelete(json);
  195. return RyanJsonFalse;
  196. });
  197. // \"inter\":16后面少个,
  198. json = RyanJsonParse("{\"inter\":16\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  199. "\"item\":{\"inter\":16,"
  200. "\"double\":16.89,\"string\":\"hello\","
  201. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  202. "89,16.89,16.89,16."
  203. "89],\"arrayString\":[\"hello\",\"hello\","
  204. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  205. "\"double\":16.89,"
  206. "\"string\":\"hello\",\"boolTrue\":true,"
  207. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  208. "\"boolFalse\":false,"
  209. "\"null\":null}]}");
  210. RyanJsonCheckCode(NULL == json, {
  211. RyanJsonDelete(json);
  212. return RyanJsonFalse;
  213. });
  214. // array数组项少一个,
  215. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  216. "\"item\":{\"inter\":16,"
  217. "\"double\":16.89,\"string\":\"hello\","
  218. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  219. "89,16.89,16.89,16."
  220. "89],\"arrayString\":[\"hello\",\"hello\","
  221. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  222. "\"double\":16.89,"
  223. "\"string\":\"hello\",\"boolTrue\":true,"
  224. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  225. "\"boolFalse\":false,"
  226. "\"null\":null}]}");
  227. RyanJsonCheckCode(NULL == json, {
  228. RyanJsonDelete(json);
  229. return RyanJsonFalse;
  230. });
  231. // \"item:{\"inter\":16,\" 少一个"
  232. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  233. "\"item:{\"inter\":16,"
  234. "\"double\":16.89,\"string\":\"hello\","
  235. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  236. "89,16.89,16.89,16."
  237. "89],\"arrayString\":[\"hello\",\"hello\","
  238. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  239. "\"double\":16.89,"
  240. "\"string\":\"hello\",\"boolTrue\":true,"
  241. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  242. "\"boolFalse\":false,"
  243. "\"null\":null}]}");
  244. RyanJsonCheckCode(NULL == json, {
  245. RyanJsonDelete(json);
  246. return RyanJsonFalse;
  247. });
  248. // \"item\":{\"inter\":16,double\" 少一个"
  249. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  250. "\"item\":{\"inter\":16,"
  251. "double\":16.89,\"string\":\"hello\","
  252. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  253. "89,16.89,16.89,16."
  254. "89],\"arrayString\":[\"hello\",\"hello\","
  255. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  256. "\"double\":16.89,"
  257. "\"string\":\"hello\",\"boolTrue\":true,"
  258. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  259. "\"boolFalse\":false,"
  260. "\"null\":null}]}");
  261. RyanJsonCheckCode(NULL == json, {
  262. RyanJsonDelete(json);
  263. return RyanJsonFalse;
  264. });
  265. // \"item\":{\"inter\":16,\"\"double\" 多一个"
  266. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  267. "\"item\":{\"inter\":16,"
  268. "\"\"double\":16.89,\"string\":\"hello\","
  269. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  270. "89,16.89,16.89,16."
  271. "89],\"arrayString\":[\"hello\",\"hello\","
  272. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  273. "\"double\":16.89,"
  274. "\"string\":\"hello\",\"boolTrue\":true,"
  275. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  276. "\"boolFalse\":false,"
  277. "\"null\":null}]}");
  278. RyanJsonCheckCode(NULL == json, {
  279. RyanJsonDelete(json);
  280. return RyanJsonFalse;
  281. });
  282. // \"item\":{\"inter\":16\",\"double\" 多一个"
  283. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  284. "\"item\":{\"inter\":16\","
  285. "\"double\":16.89,\"string\":\"hello\","
  286. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  287. "89,16.89,16.89,16."
  288. "89],\"arrayString\":[\"hello\",\"hello\","
  289. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  290. "\"double\":16.89,"
  291. "\"string\":\"hello\",\"boolTrue\":true,"
  292. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  293. "\"boolFalse\":false,"
  294. "\"null\":null}]}");
  295. RyanJsonCheckCode(NULL == json, {
  296. RyanJsonDelete(json);
  297. return RyanJsonFalse;
  298. });
  299. // \"arrayInt\":[16,16,16m,16,16] 无效数组数字
  300. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  301. "\"item\":{\"inter\":16,"
  302. "\"double\":16.89,\"string\":\"hello\","
  303. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16m,16,16],\"arrayDouble\":[16.89,"
  304. "16.89,16.89,16.89,16."
  305. "89],\"arrayString\":[\"hello\",\"hello\","
  306. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  307. "\"double\":16.89,"
  308. "\"string\":\"hello\",\"boolTrue\":true,"
  309. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  310. "\"boolFalse\":false,"
  311. "\"null\":null}]}");
  312. RyanJsonCheckCode(NULL == json, {
  313. RyanJsonDelete(json);
  314. return RyanJsonFalse;
  315. });
  316. return RyanJsonTrue;
  317. }