1 /*
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
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 <string.h>
26 #include "cJSON.h"
27
28 /* Used by some code below as an example datatype. */
29 struct record
30 {
31 const char *precision;
32 double lat;
33 double lon;
34 const char *address;
35 const char *city;
36 const char *state;
37 const char *zip;
38 const char *country;
39 };
40
41
42 /* Create a bunch of objects as demonstration. */
print_preallocated(cJSON * root)43 static int print_preallocated(cJSON *root)
44 {
45 /* declarations */
46 char *out = NULL;
47 char *buf = NULL;
48 char *buf_fail = NULL;
49 size_t len = 0;
50 size_t len_fail = 0;
51
52 /* formatted print */
53 out = cJSON_Print(root);
54
55 /* create buffer to succeed */
56 /* the extra 5 bytes are because of inaccuracies when reserving memory */
57 len = strlen(out) + 5;
58 buf = (char*)malloc(len);
59 if (buf == NULL)
60 {
61 printf("Failed to allocate memory.\n");
62 exit(1);
63 }
64
65 /* create buffer to fail */
66 len_fail = strlen(out);
67 buf_fail = (char*)malloc(len_fail);
68 if (buf_fail == NULL)
69 {
70 printf("Failed to allocate memory.\n");
71 exit(1);
72 }
73
74 /* Print to buffer */
75 if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
76 printf("cJSON_PrintPreallocated failed!\n");
77 if (strcmp(out, buf) != 0) {
78 printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
79 printf("cJSON_Print result:\n%s\n", out);
80 printf("cJSON_PrintPreallocated result:\n%s\n", buf);
81 }
82 free(out);
83 free(buf_fail);
84 free(buf);
85 return -1;
86 }
87
88 /* success */
89 printf("%s\n", buf);
90
91 /* force it to fail */
92 if (cJSON_PrintPreallocated(root, buf_fail, (int)len_fail, 1)) {
93 printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n");
94 printf("cJSON_Print result:\n%s\n", out);
95 printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail);
96 free(out);
97 free(buf_fail);
98 free(buf);
99 return -1;
100 }
101
102 free(out);
103 free(buf_fail);
104 free(buf);
105 return 0;
106 }
107
108 /* Create a bunch of objects as demonstration. */
create_objects(void)109 static void create_objects(void)
110 {
111 /* declare a few. */
112 cJSON *root = NULL;
113 cJSON *fmt = NULL;
114 cJSON *img = NULL;
115 cJSON *thm = NULL;
116 cJSON *fld = NULL;
117 int i = 0;
118
119 /* Our "days of the week" array: */
120 const char *strings[7] =
121 {
122 "Sunday",
123 "Monday",
124 "Tuesday",
125 "Wednesday",
126 "Thursday",
127 "Friday",
128 "Saturday"
129 };
130 /* Our matrix: */
131 int numbers[3][3] =
132 {
133 {0, -1, 0},
134 {1, 0, 0},
135 {0 ,0, 1}
136 };
137 /* Our "gallery" item: */
138 int ids[4] = { 116, 943, 234, 38793 };
139 /* Our array of "records": */
140 struct record fields[2] =
141 {
142 {
143 "zip",
144 37.7668,
145 -1.223959e+2,
146 "",
147 "SAN FRANCISCO",
148 "CA",
149 "94107",
150 "US"
151 },
152 {
153 "zip",
154 37.371991,
155 -1.22026e+2,
156 "",
157 "SUNNYVALE",
158 "CA",
159 "94085",
160 "US"
161 }
162 };
163 volatile double zero = 0.0;
164
165 /* Here we construct some JSON standards, from the JSON site. */
166
167 /* Our "Video" datatype: */
168 root = cJSON_CreateObject();
169 cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
170 cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
171 cJSON_AddStringToObject(fmt, "type", "rect");
172 cJSON_AddNumberToObject(fmt, "width", 1920);
173 cJSON_AddNumberToObject(fmt, "height", 1080);
174 cJSON_AddFalseToObject (fmt, "interlace");
175 cJSON_AddNumberToObject(fmt, "frame rate", 24);
176
177 /* Print to text */
178 if (print_preallocated(root) != 0) {
179 cJSON_Delete(root);
180 exit(EXIT_FAILURE);
181 }
182 cJSON_Delete(root);
183
184 /* Our "days of the week" array: */
185 root = cJSON_CreateStringArray(strings, 7);
186
187 if (print_preallocated(root) != 0) {
188 cJSON_Delete(root);
189 exit(EXIT_FAILURE);
190 }
191 cJSON_Delete(root);
192
193 /* Our matrix: */
194 root = cJSON_CreateArray();
195 for (i = 0; i < 3; i++)
196 {
197 cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], 3));
198 }
199
200 /* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
201
202 if (print_preallocated(root) != 0) {
203 cJSON_Delete(root);
204 exit(EXIT_FAILURE);
205 }
206 cJSON_Delete(root);
207
208 /* Our "gallery" item: */
209 root = cJSON_CreateObject();
210 cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject());
211 cJSON_AddNumberToObject(img, "Width", 800);
212 cJSON_AddNumberToObject(img, "Height", 600);
213 cJSON_AddStringToObject(img, "Title", "View from 15th Floor");
214 cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject());
215 cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
216 cJSON_AddNumberToObject(thm, "Height", 125);
217 cJSON_AddStringToObject(thm, "Width", "100");
218 cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));
219
220 if (print_preallocated(root) != 0) {
221 cJSON_Delete(root);
222 exit(EXIT_FAILURE);
223 }
224 cJSON_Delete(root);
225
226 /* Our array of "records": */
227 root = cJSON_CreateArray();
228 for (i = 0; i < 2; i++)
229 {
230 cJSON_AddItemToArray(root, fld = cJSON_CreateObject());
231 cJSON_AddStringToObject(fld, "precision", fields[i].precision);
232 cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
233 cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
234 cJSON_AddStringToObject(fld, "Address", fields[i].address);
235 cJSON_AddStringToObject(fld, "City", fields[i].city);
236 cJSON_AddStringToObject(fld, "State", fields[i].state);
237 cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
238 cJSON_AddStringToObject(fld, "Country", fields[i].country);
239 }
240
241 /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
242
243 if (print_preallocated(root) != 0) {
244 cJSON_Delete(root);
245 exit(EXIT_FAILURE);
246 }
247 cJSON_Delete(root);
248
249 root = cJSON_CreateObject();
250 cJSON_AddNumberToObject(root, "number", 1.0 / zero);
251
252 if (print_preallocated(root) != 0) {
253 cJSON_Delete(root);
254 exit(EXIT_FAILURE);
255 }
256 cJSON_Delete(root);
257 }
258
main(void)259 int CJSON_CDECL main(void)
260 {
261 /* print the version */
262 printf("Version: %s\n", cJSON_Version());
263
264 /* Now some samplecode for building objects concisely: */
265 create_objects();
266
267 return 0;
268 }
269