test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  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 "cJSON.h"
  22. /* Parse text to JSON, then render back to text, and print! */
  23. void doit(char *text)
  24. {
  25. char *out;cJSON *json;
  26. json=cJSON_Parse(text);
  27. if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
  28. else
  29. {
  30. out=cJSON_Print(json);
  31. cJSON_Delete(json);
  32. printf("%s\n",out);
  33. free(out);
  34. }
  35. }
  36. /* Read a file, parse, render back, etc. */
  37. void dofile(char *filename)
  38. {
  39. FILE *f;long len;char *data;
  40. f=fopen(filename,"rb");fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET);
  41. data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);
  42. doit(data);
  43. free(data);
  44. }
  45. /* Used by some code below as an example datatype. */
  46. struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; };
  47. /* Create a bunch of objects as demonstration. */
  48. void create_objects()
  49. {
  50. cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */
  51. /* Our "days of the week" array: */
  52. const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  53. /* Our matrix: */
  54. int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}};
  55. /* Our "gallery" item: */
  56. int ids[4]={116,943,234,38793};
  57. /* Our array of "records": */
  58. struct record fields[2]={
  59. {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"},
  60. {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}};
  61. /* Here we construct some JSON standards, from the JSON site. */
  62. /* Our "Video" datatype: */
  63. root=cJSON_CreateObject();
  64. cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
  65. cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
  66. cJSON_AddStringToObject(fmt,"type", "rect");
  67. cJSON_AddNumberToObject(fmt,"width", 1920);
  68. cJSON_AddNumberToObject(fmt,"height", 1080);
  69. cJSON_AddFalseToObject (fmt,"interlace");
  70. cJSON_AddNumberToObject(fmt,"frame rate", 24);
  71. out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */
  72. /* Our "days of the week" array: */
  73. root=cJSON_CreateStringArray(strings,7);
  74. out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
  75. /* Our matrix: */
  76. root=cJSON_CreateArray();
  77. for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3));
  78. /* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
  79. out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
  80. /* Our "gallery" item: */
  81. root=cJSON_CreateObject();
  82. cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject());
  83. cJSON_AddNumberToObject(img,"Width",800);
  84. cJSON_AddNumberToObject(img,"Height",600);
  85. cJSON_AddStringToObject(img,"Title","View from 15th Floor");
  86. cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject());
  87. cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
  88. cJSON_AddNumberToObject(thm,"Height",125);
  89. cJSON_AddStringToObject(thm,"Width","100");
  90. cJSON_AddItemToObject(img,"IDs", cJSON_CreateIntArray(ids,4));
  91. out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
  92. /* Our array of "records": */
  93. root=cJSON_CreateArray();
  94. for (i=0;i<2;i++)
  95. {
  96. cJSON_AddItemToArray(root,fld=cJSON_CreateObject());
  97. cJSON_AddStringToObject(fld, "precision", fields[i].precision);
  98. cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
  99. cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
  100. cJSON_AddStringToObject(fld, "Address", fields[i].address);
  101. cJSON_AddStringToObject(fld, "City", fields[i].city);
  102. cJSON_AddStringToObject(fld, "State", fields[i].state);
  103. cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
  104. cJSON_AddStringToObject(fld, "Country", fields[i].country);
  105. }
  106. /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
  107. out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
  108. }
  109. int main (int argc, const char * argv[]) {
  110. /* a bunch of json: */
  111. char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\": \"rect\", \n\"width\": 1920, \n\"height\": 1080, \n\"interlace\": false,\"frame rate\": 24\n}\n}";
  112. char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]";
  113. char text3[]="[\n [0, -1, 0],\n [1, 0, 0],\n [0, 0, 1]\n ]\n";
  114. char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http:/*www.example.com/image/481989943\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }";
  115. char text5[]="[\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.7668,\n \"Longitude\": -122.3959,\n \"Address\": \"\",\n \"City\": \"SAN FRANCISCO\",\n \"State\": \"CA\",\n \"Zip\": \"94107\",\n \"Country\": \"US\"\n },\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.371991,\n \"Longitude\": -122.026020,\n \"Address\": \"\",\n \"City\": \"SUNNYVALE\",\n \"State\": \"CA\",\n \"Zip\": \"94085\",\n \"Country\": \"US\"\n }\n ]";
  116. /* Process each json textblock by parsing, then rebuilding: */
  117. doit(text1);
  118. doit(text2);
  119. doit(text3);
  120. doit(text4);
  121. doit(text5);
  122. /* Parse standard testfiles: */
  123. /* dofile("../../tests/test1"); */
  124. /* dofile("../../tests/test2"); */
  125. /* dofile("../../tests/test3"); */
  126. /* dofile("../../tests/test4"); */
  127. /* dofile("../../tests/test5"); */
  128. /* Now some samplecode for building objects concisely: */
  129. create_objects();
  130. return 0;
  131. }