RyanJsonBaseTestLoadJson.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "RyanJsonBaseTest.h"
  2. /* --------------------------------------------------------------------- */
  3. RyanJsonBool_e RyanJsonBaseTestLoadJson()
  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}]}";
  18. json = RyanJsonParse(jsonstr);
  19. RyanJsonCheckReturnFlase(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 测试序列化错误json结构
  34. *
  35. */
  36. // \"inter\":16poi, 无效数字
  37. json = RyanJsonParse("{\"inter\":16poi,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  38. "\"item\":{\"inter\":"
  39. "16,\"double\":16.89,\"string\":\"hello\","
  40. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  41. "89,16.89,16.89,16."
  42. "89],\"arrayString\":[\"hello\",\"hello\","
  43. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  44. "\"double\":16.89,"
  45. "\"string\":\"hello\",\"boolTrue\":true,"
  46. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  47. "\"boolFalse\":false,"
  48. "\"null\":null}]}");
  49. RyanJsonCheckCode(NULL == json, {
  50. RyanJsonDelete(json);
  51. return RyanJsonFalse;
  52. });
  53. // \"double\":16.8yu9,, 无效浮点数
  54. json = RyanJsonParse("{\"inter\":16,\"double\":16.8yu9,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  55. "\"item\":{\"inter\":16,"
  56. "\"double\":16.89,\"string\":\"hello\","
  57. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  58. "89,16.89,16.89,16."
  59. "89],\"arrayString\":[\"hello\",\"hello\","
  60. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  61. "\"double\":16.89,"
  62. "\"string\":\"hello\",\"boolTrue\":true,"
  63. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  64. "\"boolFalse\":false,"
  65. "\"null\":null}]}");
  66. RyanJsonCheckCode(NULL == json, {
  67. RyanJsonDelete(json);
  68. return RyanJsonFalse;
  69. });
  70. // boolTrue 设置为 tru
  71. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":tru,\"boolFalse\":false,\"null\":null,"
  72. "\"item\":{\"inter\":16,"
  73. "\"double\":16.89,\"string\":\"hello\","
  74. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  75. "89,16.89,16.89,16."
  76. "89],\"arrayString\":[\"hello\",\"hello\","
  77. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  78. "\"double\":16.89,"
  79. "\"string\":\"hello\",\"boolTrue\":true,"
  80. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  81. "\"boolFalse\":false,"
  82. "\"null\":null}]}");
  83. RyanJsonCheckCode(NULL == json, {
  84. RyanJsonDelete(json);
  85. return RyanJsonFalse;
  86. });
  87. // boolFalse 设置为 fale
  88. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":fale,\"null\":null,"
  89. "\"item\":{\"inter\":16,"
  90. "\"double\":16.89,\"string\":\"hello\","
  91. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  92. "89,16.89,16.89,16."
  93. "89],\"arrayString\":[\"hello\",\"hello\","
  94. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  95. "\"double\":16.89,"
  96. "\"string\":\"hello\",\"boolTrue\":true,"
  97. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  98. "\"boolFalse\":false,"
  99. "\"null\":null}]}");
  100. RyanJsonCheckCode(NULL == json, {
  101. RyanJsonDelete(json);
  102. return RyanJsonFalse;
  103. });
  104. // null 设置为 nul
  105. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":nul,"
  106. "\"item\":{\"inter\":16,"
  107. "\"double\":16.89,\"string\":\"hello\","
  108. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  109. "89,16.89,16.89,16."
  110. "89],\"arrayString\":[\"hello\",\"hello\","
  111. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  112. "\"double\":16.89,"
  113. "\"string\":\"hello\",\"boolTrue\":true,"
  114. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  115. "\"boolFalse\":false,"
  116. "\"null\":null}]}");
  117. RyanJsonCheckCode(NULL == json, {
  118. RyanJsonDelete(json);
  119. return RyanJsonFalse;
  120. });
  121. // null 设置为 NULL
  122. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":NULL,"
  123. "\"item\":{\"inter\":16,"
  124. "\"double\":16.89,\"string\":\"hello\","
  125. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  126. "89,16.89,16.89,16."
  127. "89],\"arrayString\":[\"hello\",\"hello\","
  128. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  129. "\"double\":16.89,"
  130. "\"string\":\"hello\",\"boolTrue\":true,"
  131. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  132. "\"boolFalse\":false,"
  133. "\"null\":null}]}");
  134. RyanJsonCheckCode(NULL == json, {
  135. RyanJsonDelete(json);
  136. return RyanJsonFalse;
  137. });
  138. // \"inter\":16后面少个,
  139. json = RyanJsonParse("{\"inter\":16\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  140. "\"item\":{\"inter\":16,"
  141. "\"double\":16.89,\"string\":\"hello\","
  142. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  143. "89,16.89,16.89,16."
  144. "89],\"arrayString\":[\"hello\",\"hello\","
  145. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  146. "\"double\":16.89,"
  147. "\"string\":\"hello\",\"boolTrue\":true,"
  148. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  149. "\"boolFalse\":false,"
  150. "\"null\":null}]}");
  151. RyanJsonCheckCode(NULL == json, {
  152. RyanJsonDelete(json);
  153. return RyanJsonFalse;
  154. });
  155. // array数组项少一个,
  156. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  157. "\"item\":{\"inter\":16,"
  158. "\"double\":16.89,\"string\":\"hello\","
  159. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  160. "89,16.89,16.89,16."
  161. "89],\"arrayString\":[\"hello\",\"hello\","
  162. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  163. "\"double\":16.89,"
  164. "\"string\":\"hello\",\"boolTrue\":true,"
  165. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  166. "\"boolFalse\":false,"
  167. "\"null\":null}]}");
  168. RyanJsonCheckCode(NULL == json, {
  169. RyanJsonDelete(json);
  170. return RyanJsonFalse;
  171. });
  172. // \"item:{\"inter\":16,\" 少一个"
  173. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  174. "\"item:{\"inter\":16,"
  175. "\"double\":16.89,\"string\":\"hello\","
  176. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  177. "89,16.89,16.89,16."
  178. "89],\"arrayString\":[\"hello\",\"hello\","
  179. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  180. "\"double\":16.89,"
  181. "\"string\":\"hello\",\"boolTrue\":true,"
  182. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  183. "\"boolFalse\":false,"
  184. "\"null\":null}]}");
  185. RyanJsonCheckCode(NULL == json, {
  186. RyanJsonDelete(json);
  187. return RyanJsonFalse;
  188. });
  189. // \"item\":{\"inter\":16,double\" 少一个"
  190. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  191. "\"item\":{\"inter\":16,"
  192. "double\":16.89,\"string\":\"hello\","
  193. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  194. "89,16.89,16.89,16."
  195. "89],\"arrayString\":[\"hello\",\"hello\","
  196. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  197. "\"double\":16.89,"
  198. "\"string\":\"hello\",\"boolTrue\":true,"
  199. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  200. "\"boolFalse\":false,"
  201. "\"null\":null}]}");
  202. RyanJsonCheckCode(NULL == json, {
  203. RyanJsonDelete(json);
  204. return RyanJsonFalse;
  205. });
  206. // \"item\":{\"inter\":16,\"\"double\" 多一个"
  207. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  208. "\"item\":{\"inter\":16,"
  209. "\"\"double\":16.89,\"string\":\"hello\","
  210. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  211. "89,16.89,16.89,16."
  212. "89],\"arrayString\":[\"hello\",\"hello\","
  213. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  214. "\"double\":16.89,"
  215. "\"string\":\"hello\",\"boolTrue\":true,"
  216. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  217. "\"boolFalse\":false,"
  218. "\"null\":null}]}");
  219. RyanJsonCheckCode(NULL == json, {
  220. RyanJsonDelete(json);
  221. return RyanJsonFalse;
  222. });
  223. // \"item\":{\"inter\":16\",\"double\" 多一个"
  224. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  225. "\"item\":{\"inter\":16\","
  226. "\"double\":16.89,\"string\":\"hello\","
  227. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16."
  228. "89,16.89,16.89,16."
  229. "89],\"arrayString\":[\"hello\",\"hello\","
  230. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  231. "\"double\":16.89,"
  232. "\"string\":\"hello\",\"boolTrue\":true,"
  233. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  234. "\"boolFalse\":false,"
  235. "\"null\":null}]}");
  236. RyanJsonCheckCode(NULL == json, {
  237. RyanJsonDelete(json);
  238. return RyanJsonFalse;
  239. });
  240. // \"arrayInt\":[16,16,16m,16,16] 无效数组数字
  241. json = RyanJsonParse("{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  242. "\"item\":{\"inter\":16,"
  243. "\"double\":16.89,\"string\":\"hello\","
  244. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16m,16,16],\"arrayDouble\":[16.89,"
  245. "16.89,16.89,16.89,16."
  246. "89],\"arrayString\":[\"hello\",\"hello\","
  247. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  248. "\"double\":16.89,"
  249. "\"string\":\"hello\",\"boolTrue\":true,"
  250. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  251. "\"boolFalse\":false,"
  252. "\"null\":null}]}");
  253. RyanJsonCheckCode(NULL == json, {
  254. RyanJsonDelete(json);
  255. return RyanJsonFalse;
  256. });
  257. return RyanJsonTrue;
  258. }