1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2 /*
3 * Simple streaming JSON writer
4 *
5 * This takes care of the annoying bits of JSON syntax like the commas
6 * after elements
7 *
8 * Authors: Stephen Hemminger <[email protected]>
9 *
10 * Borrowed from Linux kernel [5.17.0]: tools/bpf/bpftool/json_writer.[hc]
11 */
12
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <stdarg.h>
16 #include <assert.h>
17 #include <malloc.h>
18 #include <inttypes.h>
19 #include <stdint.h>
20
21 #include "../lib/sg_json_builder.h"
22 #include "sg_pr2serr.h"
23
24 #define MY_NAME "sg_tst_json_builder"
25
26
27 static json_serialize_opts out_settings = {
28 json_serialize_mode_multiline,
29 0,
30 4
31 };
32
33 int
main(int argc,char * argv[])34 main(int argc, char * argv[])
35 {
36 size_t len;
37 sgj_state jstate;
38 sgj_state * jstp = &jstate;
39 json_value * jv1p;
40 json_value * jv2p;
41 json_value * jv3p = json_object_new(0);
42 json_value * jvp = NULL;
43 json_value * jv4p;
44 json_value * jv5p;
45 json_value * ja1p = json_array_new(0);
46 json_value * ja2p;
47 json_value * jsp = json_string_new("hello world 1");
48 json_value * js2p = json_string_new("hello world 2");
49 json_value * js3p = json_string_new("hello world 3");
50 json_value * js10 = json_string_new("good-bye world");
51 json_value * js11 = json_string_new("good-bye world 2");
52 json_value * js12 = json_string_new("duplicate name 1");
53 char b[8192];
54
55 sgj_init_state(jstp, NULL);
56 jvp = sgj_start_r(MY_NAME, "0.02 20220503", argc, argv, jstp);
57 jv1p = json_object_push(jvp, "contents", jsp);
58
59 if (jvp == jv1p)
60 printf("jvp == jv1p\n");
61 else
62 printf("jvp != jv1p\n");
63
64 #if 1
65 json_array_push(ja1p, js2p);
66 jv2p = json_object_push(jvp, "extra", js3p);
67 if (jv2p)
68 printf("jv2p->type=%d\n", jv2p->type);
69 else
70 printf("jv2p is NULL\n");
71 ja2p = json_array_push(ja1p, json_string_new(
72 "test double quote, etc: \" world \\ 99\t\ttwo tabs"));
73 if (ja2p)
74 printf("ja2p->type=%d\n", ja2p->type);
75 else
76 printf("ja2p is NULL\n");
77 // json_object_push(ja2p, "boo", json_string_new("hello world 88"));
78 json_object_push(jvp, "a_array", ja1p);
79 jv4p = json_object_push(jvp, "a_object", jv3p);
80 if (jv4p)
81 printf("jv4p->type=%d\n", jv4p->type);
82 else
83 printf("jv4p is NULL\n");
84 json_object_push(jv4p, "test", js10);
85 json_object_push(jv4p, "test2", js11);
86 json_object_push(jv4p, "test", js12);
87 // ja3p = json_array_push(ja2p, json_string_new("good-bye"));
88 // jv4p = json_object_push(jvp, "a_array", ja2p);
89 // jv5p = json_object_merge(jvp, ja1p);
90 #endif
91 jv5p = jvp;
92
93 len = json_measure_ex(jv5p, out_settings);
94 printf("jvp length: %zu bytes\n", len);
95 if (len < sizeof(b)) {
96 json_serialize_ex(b, jv5p, out_settings);
97 printf("json serialized:\n");
98 printf("%s\n", b);
99 } else
100 printf("since json output length [%zu] > 8192, skip outputting\n",
101 len);
102
103 json_builder_free(jvp);
104 return 0;
105 }
106
107
108 #if 0
109 int main(int argc, char **argv)
110 {
111 json_writer_t *wr = jsonw_new(stdout);
112
113 jsonw_start_object(wr);
114 jsonw_pretty(wr, true);
115 jsonw_name(wr, "Vyatta");
116 jsonw_start_object(wr);
117 jsonw_string_field(wr, "url", "http://vyatta.com");
118 jsonw_uint_field(wr, "downloads", 2000000ul);
119 jsonw_float_field(wr, "stock", 8.16);
120
121 jsonw_name(wr, "ARGV");
122 jsonw_start_array(wr);
123 while (--argc)
124 jsonw_string(wr, *++argv);
125 jsonw_end_array(wr);
126
127 jsonw_name(wr, "empty");
128 jsonw_start_array(wr);
129 jsonw_end_array(wr);
130
131 jsonw_name(wr, "NIL");
132 jsonw_start_object(wr);
133 jsonw_end_object(wr);
134
135 jsonw_null_field(wr, "my_null");
136
137 jsonw_name(wr, "special chars");
138 jsonw_start_array(wr);
139 jsonw_string_field(wr, "slash", "/");
140 jsonw_string_field(wr, "newline", "\n");
141 jsonw_string_field(wr, "tab", "\t");
142 jsonw_string_field(wr, "ff", "\f");
143 jsonw_string_field(wr, "quote", "\"");
144 jsonw_string_field(wr, "tick", "\'");
145 jsonw_string_field(wr, "backslash", "\\");
146 jsonw_end_array(wr);
147
148 jsonw_name(wr, "ARGV");
149 jsonw_start_array(wr);
150 jsonw_string(wr, "boo: appended or new entry?");
151 jsonw_end_array(wr);
152
153 jsonw_end_object(wr);
154
155 jsonw_end_object(wr);
156 jsonw_destroy(&wr);
157 return 0;
158 }
159
160 #endif
161