|
7 | 7 |
|
8 | 8 | Quick example (docs coming soon):
|
9 | 9 |
|
10 |
| - json_value * arr = json_array_new(0); |
11 |
| - json_array_push(arr, json_string_new("Hello world!")); |
12 |
| - json_array_push(arr, json_integer_new(128)); |
| 10 | +```c |
| 11 | +json_value * arr = json_array_new(0); |
| 12 | +json_array_push(arr, json_string_new("Hello world!")); |
| 13 | +json_array_push(arr, json_integer_new(128)); |
13 | 14 |
|
14 |
| - char * buf = malloc(json_measure(arr)); |
15 |
| - json_serialize(buf, arr); |
| 15 | +char * buf = malloc(json_measure(arr)); |
| 16 | +json_serialize(buf, arr); |
16 | 17 |
|
17 |
| - printf("%s\n", buf); |
| 18 | +printf("%s\n", buf); |
| 19 | +``` |
18 | 20 |
|
| 21 | +``` |
19 | 22 | > [ "Hello world!", 128 ]
|
| 23 | +``` |
20 | 24 |
|
21 | 25 | json-builder is fully interoperable with json-parser:
|
22 | 26 |
|
23 |
| - char json[] = "[ 1, 2, 3 ]"; |
| 27 | +```c |
| 28 | +char json[] = "[ 1, 2, 3 ]"; |
24 | 29 |
|
25 |
| - json_settings settings = {}; |
26 |
| - settings.value_extra = json_builder_extra; /* space for json-builder state */ |
| 30 | +json_settings settings = {}; |
| 31 | +settings.value_extra = json_builder_extra; /* space for json-builder state */ |
27 | 32 |
|
28 |
| - char error[128]; |
29 |
| - json_value * arr = json_parse_ex(&settings, json, strlen(json), error); |
| 33 | +char error[128]; |
| 34 | +json_value * arr = json_parse_ex(&settings, json, strlen(json), error); |
30 | 35 |
|
31 |
| - /* Now serialize it again. |
32 |
| - */ |
33 |
| - char * buf = malloc(json_measure(arr)); |
34 |
| - json_serialize(buf, arr); |
| 36 | +/* Now serialize it again. */ |
| 37 | +char * buf = malloc(json_measure(arr)); |
| 38 | +json_serialize(buf, arr); |
35 | 39 |
|
36 |
| - printf("%s\n", buf); |
| 40 | +printf("%s\n", buf); |
| 41 | +``` |
37 | 42 |
|
| 43 | +``` |
38 | 44 | > [ 1, 2, 3 ]
|
| 45 | +``` |
39 | 46 |
|
40 | 47 | Note that values created by or modified by json-builder must be freed with
|
41 | 48 | `json_builder_free` instead of `json_value_free`, otherwise the memory of the
|
|
0 commit comments