2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
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
28 /* Used by some code below as an example datatype. */
31 const char *precision;
42 /* Create a bunch of objects as demonstration. */
43 static int print_preallocated(cJSON *root)
48 char *buf_fail = NULL;
53 out = cJSON_Print(root);
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);
61 printf("Failed to allocate memory.\n");
65 /* create buffer to fail */
66 len_fail = strlen(out);
67 buf_fail = (char*)malloc(len_fail);
70 printf("Failed to allocate memory.\n");
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);
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);
108 /* Create a bunch of objects as demonstration. */
109 static void create_objects(void)
119 /* Our "days of the week" array: */
120 const char *strings[7] =
137 /* Our "gallery" item: */
138 int ids[4] = { 116, 943, 234, 38793 };
139 /* Our array of "records": */
140 struct record fields[2] =
163 volatile double zero = 0.0;
165 /* Here we construct some JSON standards, from the JSON site. */
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);
178 if (print_preallocated(root) != 0) {
184 /* Our "days of the week" array: */
185 root = cJSON_CreateStringArray(strings, 7);
187 if (print_preallocated(root) != 0) {
194 root = cJSON_CreateArray();
195 for (i = 0; i < 3; i++)
197 cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], 3));
200 /* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
202 if (print_preallocated(root) != 0) {
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));
220 if (print_preallocated(root) != 0) {
226 /* Our array of "records": */
227 root = cJSON_CreateArray();
228 for (i = 0; i < 2; i++)
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);
241 /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
243 if (print_preallocated(root) != 0) {
249 root = cJSON_CreateObject();
250 cJSON_AddNumberToObject(root, "number", 1.0 / zero);
252 if (print_preallocated(root) != 0) {
261 /* print the version */
262 printf("Version: %s\n", cJSON_Version());
264 /* Now some samplecode for building objects concisely: */