test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "cJSON.h"
  23. /* Used by some code below as an example datatype. */
  24. struct record
  25. {
  26. const char *precision;
  27. double lat;
  28. double lon;
  29. const char *address;
  30. const char *city;
  31. const char *state;
  32. const char *zip;
  33. const char *country;
  34. };
  35. /* Create a bunch of objects as demonstration. */
  36. static int print_preallocated(cJSON *root)
  37. {
  38. /* declarations */
  39. char *out = NULL;
  40. char *buf = NULL;
  41. char *buf_fail = NULL;
  42. size_t len = 0;
  43. size_t len_fail = 0;
  44. /* formatted print */
  45. out = cJSON_Print(root);
  46. /* create buffer to succeed */
  47. /* the extra 5 bytes are because of inaccuracies when reserving memory */
  48. len = strlen(out) + 5;
  49. buf = (char*)malloc(len);
  50. if (buf == NULL)
  51. {
  52. printf("Failed to allocate memory.\n");
  53. return 1;
  54. }
  55. /* create buffer to fail */
  56. len_fail = strlen(out);
  57. buf_fail = (char*)malloc(len_fail);
  58. if (buf_fail == NULL)
  59. {
  60. printf("Failed to allocate memory.\n");
  61. return 1;
  62. }
  63. /* Print to buffer */
  64. if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
  65. printf("cJSON_PrintPreallocated failed!\n");
  66. if (strcmp(out, buf) != 0) {
  67. printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
  68. printf("cJSON_Print result:\n%s\n", out);
  69. printf("cJSON_PrintPreallocated result:\n%s\n", buf);
  70. }
  71. free(out);
  72. free(buf_fail);
  73. free(buf);
  74. return -1;
  75. }
  76. /* success */
  77. printf("%s\n", buf);
  78. /* force it to fail */
  79. if (cJSON_PrintPreallocated(root, buf_fail, (int)len_fail, 1)) {
  80. printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n");
  81. printf("cJSON_Print result:\n%s\n", out);
  82. printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail);
  83. free(out);
  84. free(buf_fail);
  85. free(buf);
  86. return -1;
  87. }
  88. free(out);
  89. free(buf_fail);
  90. free(buf);
  91. return 0;
  92. }
  93. /* Create a bunch of objects as demonstration. */
  94. void create_objects(void)
  95. {
  96. /* declare a few. */
  97. cJSON *root = NULL;
  98. cJSON *fmt = NULL;
  99. cJSON *img = NULL;
  100. cJSON *thm = NULL;
  101. cJSON *fld = NULL;
  102. int i = 0;
  103. /* Our "days of the week" array: */
  104. const char *strings[7] =
  105. {
  106. "Sunday",
  107. "Monday",
  108. "Tuesday",
  109. "Wednesday",
  110. "Thursday",
  111. "Friday",
  112. "Saturday"
  113. };
  114. /* Our matrix: */
  115. int numbers[3][3] =
  116. {
  117. {0, -1, 0},
  118. {1, 0, 0},
  119. {0 ,0, 1}
  120. };
  121. /* Our "gallery" item: */
  122. int ids[4] = { 116, 943, 234, 38793 };
  123. /* Our array of "records": */
  124. struct record fields[2] =
  125. {
  126. {
  127. "zip",
  128. 37.7668,
  129. -1.223959e+2,
  130. "",
  131. "SAN FRANCISCO",
  132. "CA",
  133. "94107",
  134. "US"
  135. },
  136. {
  137. "zip",
  138. 37.371991,
  139. -1.22026e+2,
  140. "",
  141. "SUNNYVALE",
  142. "CA",
  143. "94085",
  144. "US"
  145. }
  146. };
  147. volatile double zero = 0.0;
  148. /* Here we construct some JSON standards, from the JSON site. */
  149. /* Our "Video" datatype: */
  150. root = cJSON_CreateObject();
  151. cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
  152. cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
  153. cJSON_AddStringToObject(fmt, "type", "rect");
  154. cJSON_AddNumberToObject(fmt, "width", 1920);
  155. cJSON_AddNumberToObject(fmt, "height", 1080);
  156. cJSON_AddFalseToObject (fmt, "interlace");
  157. cJSON_AddNumberToObject(fmt, "frame rate", 24);
  158. /* Print to text */
  159. if (print_preallocated(root) != 0) {
  160. cJSON_Delete(root);
  161. return;
  162. }
  163. cJSON_Delete(root);
  164. /* Our "days of the week" array: */
  165. root = cJSON_CreateStringArray(strings, 7);
  166. if (print_preallocated(root) != 0) {
  167. cJSON_Delete(root);
  168. return;
  169. }
  170. cJSON_Delete(root);
  171. /* Our matrix: */
  172. root = cJSON_CreateArray();
  173. for (i = 0; i < 3; i++)
  174. {
  175. cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], 3));
  176. }
  177. /* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
  178. if (print_preallocated(root) != 0) {
  179. cJSON_Delete(root);
  180. return;
  181. }
  182. cJSON_Delete(root);
  183. /* Our "gallery" item: */
  184. root = cJSON_CreateObject();
  185. cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject());
  186. cJSON_AddNumberToObject(img, "Width", 800);
  187. cJSON_AddNumberToObject(img, "Height", 600);
  188. cJSON_AddStringToObject(img, "Title", "View from 15th Floor");
  189. cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject());
  190. cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
  191. cJSON_AddNumberToObject(thm, "Height", 125);
  192. cJSON_AddStringToObject(thm, "Width", "100");
  193. cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));
  194. if (print_preallocated(root) != 0) {
  195. cJSON_Delete(root);
  196. return;
  197. }
  198. cJSON_Delete(root);
  199. /* Our array of "records": */
  200. root = cJSON_CreateArray();
  201. for (i = 0; i < 2; i++)
  202. {
  203. cJSON_AddItemToArray(root, fld = cJSON_CreateObject());
  204. cJSON_AddStringToObject(fld, "precision", fields[i].precision);
  205. cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
  206. cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
  207. cJSON_AddStringToObject(fld, "Address", fields[i].address);
  208. cJSON_AddStringToObject(fld, "City", fields[i].city);
  209. cJSON_AddStringToObject(fld, "State", fields[i].state);
  210. cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
  211. cJSON_AddStringToObject(fld, "Country", fields[i].country);
  212. }
  213. /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
  214. if (print_preallocated(root) != 0) {
  215. cJSON_Delete(root);
  216. return;
  217. }
  218. cJSON_Delete(root);
  219. root = cJSON_CreateObject();
  220. cJSON_AddNumberToObject(root, "number", 1.0 / zero);
  221. if (print_preallocated(root) != 0) {
  222. cJSON_Delete(root);
  223. return;
  224. }
  225. cJSON_Delete(root);
  226. }
  227. /*
  228. int CJSON_CDECL main(void)
  229. {
  230. // print the version
  231. printf("Version: %s\n", cJSON_Version());
  232. // Now some samplecode for building objects concisely:
  233. create_objects();
  234. return 0;
  235. }*/