You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+140Lines changed: 140 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ Ultralightweight JSON parser in ANSI C.
12
12
*[Working with the data structure](#working-with-the-data-structure)
13
13
*[Parsing JSON](#parsing-json)
14
14
*[Printing JSON](#printing-json)
15
+
*[Example](#example)
15
16
*[Some JSON](#some-json)
16
17
*[Here's the structure](#heres-the-structure)
17
18
*[Caveats](#caveats)
@@ -252,6 +253,145 @@ If you have a rough idea of how big your resulting string will be, you can use `
252
253
253
254
These dynamic buffer allocations can be completely avoided by using `cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)`. It takes a buffer to a pointer to print to and it's length. If the length is reached, printing will fail and it returns `0`, in case of success `1` is returned. Note that you should provide 5 bytes more than is actually needed, because cJSON is not 100% accurate in estimating if the provided memory is enough.
254
255
256
+
### Example
257
+
In this example we want to build and parse the following JSON:
258
+
259
+
```json
260
+
{
261
+
"name": "Awesome 4K",
262
+
"resolutions": [
263
+
{
264
+
"width": 1280,
265
+
"height": 720
266
+
},
267
+
{
268
+
"width": 1920,
269
+
"height": 1080
270
+
},
271
+
{
272
+
"width": 3840,
273
+
"height": 2160
274
+
}
275
+
]
276
+
}
277
+
```
278
+
279
+
#### Printing
280
+
Let's build the above JSON and print it to a string:
281
+
```c
282
+
//create a monitor with a list of supported resolutions
283
+
char* create_monitor(void) {
284
+
const unsigned int resolution_numbers[3][2] = {
285
+
{1280, 720},
286
+
{1920, 1080},
287
+
{3840, 2160}
288
+
};
289
+
char *string = NULL;
290
+
cJSON *name = NULL;
291
+
cJSON *resolutions = NULL;
292
+
cJSON *resolution = NULL;
293
+
cJSON *width = NULL;
294
+
cJSON *height = NULL;
295
+
296
+
cJSON *monitor = cJSON_CreateObject();
297
+
if (monitor == NULL) {
298
+
goto end;
299
+
}
300
+
301
+
name = cJSON_CreateString("Awesome 4K");
302
+
if (name == NULL) {
303
+
goto end;
304
+
}
305
+
/* after creation was successful, immediately add it to the monitor,
306
+
* thereby transfering ownership of the pointer to it */
if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height)) {
377
+
status = 0;
378
+
goto end;
379
+
}
380
+
381
+
if ((width->valuedouble == 1920) && (height->valuedouble == 1080)) {
382
+
status = 1;
383
+
goto end;
384
+
}
385
+
}
386
+
387
+
end:
388
+
cJSON_Delete(monitor_json);
389
+
return status;
390
+
}
391
+
```
392
+
393
+
Note that there are no NULL checks except for the result of `cJSON_Parse`, because `cJSON_GetObjectItemCaseSensitive` checks for `NULL` inputs already, so a `NULL` value is just propagated and `cJSON_IsNumber` and `cJSON_IsString` return `0` if the input is `NULL`.
0 commit comments