1 /*
2 Copyright (c) 2009 Dave Gamble
3
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:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
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
20 THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "cJSON.h"
26
27 /* Parse text to JSON, then render back to text, and print! */
doit(char * text)28 void doit(char *text)
29 {
30 char *out;cJSON *json;
31
32 json=cJSON_Parse(text);
33 if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
34 else
35 {
36 out=cJSON_Print(json);
37 cJSON_Delete(json);
38 printf("%s\n",out);
39 free(out);
40 }
41 }
42
43 /* Read a file, parse, render back, etc. */
dofile(char * filename)44 void dofile(char *filename)
45 {
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);
48 doit(data);
49 free(data);
50 }
51
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; };
54
55 /* Create a bunch of objects as demonstration. */
create_objects()56 void create_objects()
57 {
58 cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */
59
60 /* Here we construct some JSON standards, from the JSON site. */
61
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
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. */
73
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);
77
78 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
79
80 /* Our matrix: */
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));
84
85 /* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
86
87 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
88
89
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));
102
103 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
104
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"}};
109
110 root=cJSON_CreateArray();
111 for (i=0;i<2;i++)
112 {
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);
122 }
123
124 /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
125
126 out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
127
128 }
129
main(int argc,const char * argv[])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 ]";
137
138 /* Process each json textblock by parsing, then rebuilding: */
139 doit(text1);
140 doit(text2);
141 doit(text3);
142 doit(text4);
143 doit(text5);
144
145 /* Parse standard testfiles: */
146 /* dofile("../../tests/test1"); */
147 /* dofile("../../tests/test2"); */
148 /* dofile("../../tests/test3"); */
149 /* dofile("../../tests/test4"); */
150 /* dofile("../../tests/test5"); */
151
152 /* Now some samplecode for building objects concisely: */
153 create_objects();
154
155 return 0;
156 }
157