2 Copyright (c) 2009 Dave Gamble
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 /* Parse text to JSON, then render back to text, and print! */
30 char *out;cJSON *json;
32 json=cJSON_Parse(text);
33 if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
36 out=cJSON_Print(json);
43 /* Read a file, parse, render back, etc. */
44 void dofile(char *filename)
46 FILE *f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET);
47 char *data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);
52 /* Used by some code below as an example datatype. */
53 struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; };
55 /* Create a bunch of objects as demonstration. */
58 cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */
60 /* 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);
72 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */
74 /* Our "days of the week" array: */
75 const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
76 root=cJSON_CreateStringArray(strings,7);
78 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
81 int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}};
82 root=cJSON_CreateArray();
83 for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3));
85 /* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
87 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
90 /* Our "gallery" item: */
91 int ids[4]={116,943,234,38793};
92 root=cJSON_CreateObject();
93 cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject());
94 cJSON_AddNumberToObject(img,"Width",800);
95 cJSON_AddNumberToObject(img,"Height",600);
96 cJSON_AddStringToObject(img,"Title","View from 15th Floor");
97 cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject());
98 cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
99 cJSON_AddNumberToObject(thm,"Height",125);
100 cJSON_AddStringToObject(thm,"Width","100");
101 cJSON_AddItemToObject(img,"IDs", cJSON_CreateIntArray(ids,4));
103 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
105 /* Our array of "records": */
106 struct record fields[2]={
107 {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"},
108 {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}};
110 root=cJSON_CreateArray();
113 cJSON_AddItemToArray(root,fld=cJSON_CreateObject());
114 cJSON_AddStringToObject(fld, "precision", fields[i].precision);
115 cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
116 cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
117 cJSON_AddStringToObject(fld, "Address", fields[i].address);
118 cJSON_AddStringToObject(fld, "City", fields[i].city);
119 cJSON_AddStringToObject(fld, "State", fields[i].state);
120 cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
121 cJSON_AddStringToObject(fld, "Country", fields[i].country);
124 /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
126 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
130 int main (int argc, const char * argv[]) {
131 /* a bunch of json: */
132 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}";
133 char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]";
134 char text3[]="[\n [0, -1, 0],\n [1, 0, 0],\n [0, 0, 1]\n ]\n";
135 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 }";
136 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 ]";
138 /* Process each json textblock by parsing, then rebuilding: */
145 /* Parse standard testfiles: */
146 /* dofile("../../tests/test1"); */
147 /* dofile("../../tests/test2"); */
148 /* dofile("../../tests/test3"); */
149 /* dofile("../../tests/test4"); */
150 /* dofile("../../tests/test5"); */
152 /* Now some samplecode for building objects concisely: */