Skip to content

Commit 4ddb0cc

Browse files
committed
README: new doc: Example
1 parent 6e294a8 commit 4ddb0cc

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Ultralightweight JSON parser in ANSI C.
1212
* [Working with the data structure](#working-with-the-data-structure)
1313
* [Parsing JSON](#parsing-json)
1414
* [Printing JSON](#printing-json)
15+
* [Example](#example)
1516
* [Some JSON](#some-json)
1617
* [Here's the structure](#heres-the-structure)
1718
* [Caveats](#caveats)
@@ -252,6 +253,145 @@ If you have a rough idea of how big your resulting string will be, you can use `
252253

253254
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.
254255

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 */
307+
cJSON_AddItemToObject(monitor, "name", name);
308+
309+
resolutions = cJSON_CreateArray();
310+
if (resolutions == NULL) {
311+
goto end;
312+
}
313+
cJSON_AddItemToObject(monitor, "resolutions", resolutions);
314+
315+
for (size_t index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) {
316+
resolution = cJSON_CreateObject();
317+
if (resolution == NULL) {
318+
goto end;
319+
}
320+
cJSON_AddItemToArray(resolutions, resolution);
321+
322+
width = cJSON_CreateNumber(resolution_numbers[index][0]);
323+
if (width == NULL) {
324+
goto end;
325+
}
326+
cJSON_AddItemToObject(resolution, "width", width);
327+
328+
height = cJSON_CreateNumber(resolution_numbers[index][1]);
329+
if (height == NULL) {
330+
goto end;
331+
}
332+
cJSON_AddItemToObject(resolution, "height", height);
333+
}
334+
335+
string = cJSON_Print(monitor);
336+
if (string == NULL) {
337+
fprintf(stderr, "Failed to print monitor.\n");
338+
}
339+
340+
end:
341+
cJSON_Delete(monitor);
342+
return string;
343+
}
344+
```
345+
346+
#### Parsing
347+
In this example we will parse a JSON in the above format and check if the monitor supports a Full HD resolution while printing some diagnostic output:
348+
349+
```c
350+
/* return 1 if the monitor supports full hd, 0 otherwise */
351+
int supports_full_hd(const char * const monitor) {
352+
const cJSON *resolution = NULL;
353+
const cJSON *resolutions = NULL;
354+
const cJSON *name = NULL;
355+
int status = 0;
356+
cJSON *monitor_json = cJSON_Parse(monitor);
357+
if (monitor_json == NULL) {
358+
const char *error_ptr = cJSON_GetErrorPtr();
359+
if (error_ptr != NULL) {
360+
fprintf(stderr, "Error before: %s\n", error_ptr);
361+
}
362+
status = 0;
363+
goto end;
364+
}
365+
366+
name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name");
367+
if (cJSON_IsString(name) && (name->valuestring != NULL)) {
368+
printf("Checking monitor \"%s\"\n", name->valuestring);
369+
}
370+
371+
resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
372+
cJSON_ArrayForEach(resolution, resolutions) {
373+
cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
374+
cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");
375+
376+
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`.
394+
255395
### Some JSON:
256396

257397
```json

0 commit comments

Comments
 (0)