Merge branch 'jk/use-compat-util-in-test-tool'
[git/debian.git] / json-writer.c
blobaadb9dbddc33ef38a45342defd5fcccfd9c07d57
1 #include "cache.h"
2 #include "json-writer.h"
4 void jw_init(struct json_writer *jw)
6 strbuf_init(&jw->json, 0);
7 strbuf_init(&jw->open_stack, 0);
8 jw->need_comma = 0;
9 jw->pretty = 0;
12 void jw_release(struct json_writer *jw)
14 strbuf_release(&jw->json);
15 strbuf_release(&jw->open_stack);
19 * Append JSON-quoted version of the given string to 'out'.
21 static void append_quoted_string(struct strbuf *out, const char *in)
23 unsigned char c;
25 strbuf_addch(out, '"');
26 while ((c = *in++) != '\0') {
27 if (c == '"')
28 strbuf_addstr(out, "\\\"");
29 else if (c == '\\')
30 strbuf_addstr(out, "\\\\");
31 else if (c == '\n')
32 strbuf_addstr(out, "\\n");
33 else if (c == '\r')
34 strbuf_addstr(out, "\\r");
35 else if (c == '\t')
36 strbuf_addstr(out, "\\t");
37 else if (c == '\f')
38 strbuf_addstr(out, "\\f");
39 else if (c == '\b')
40 strbuf_addstr(out, "\\b");
41 else if (c < 0x20)
42 strbuf_addf(out, "\\u%04x", c);
43 else
44 strbuf_addch(out, c);
46 strbuf_addch(out, '"');
49 static void indent_pretty(struct json_writer *jw)
51 int k;
53 for (k = 0; k < jw->open_stack.len; k++)
54 strbuf_addstr(&jw->json, " ");
58 * Begin an object or array (either top-level or nested within the currently
59 * open object or array).
61 static void begin(struct json_writer *jw, char ch_open, int pretty)
63 jw->pretty = pretty;
65 strbuf_addch(&jw->json, ch_open);
67 strbuf_addch(&jw->open_stack, ch_open);
68 jw->need_comma = 0;
72 * Assert that the top of the open-stack is an object.
74 static void assert_in_object(const struct json_writer *jw, const char *key)
76 if (!jw->open_stack.len)
77 BUG("json-writer: object: missing jw_object_begin(): '%s'", key);
78 if (jw->open_stack.buf[jw->open_stack.len - 1] != '{')
79 BUG("json-writer: object: not in object: '%s'", key);
83 * Assert that the top of the open-stack is an array.
85 static void assert_in_array(const struct json_writer *jw)
87 if (!jw->open_stack.len)
88 BUG("json-writer: array: missing jw_array_begin()");
89 if (jw->open_stack.buf[jw->open_stack.len - 1] != '[')
90 BUG("json-writer: array: not in array");
94 * Add comma if we have already seen a member at this level.
96 static void maybe_add_comma(struct json_writer *jw)
98 if (jw->need_comma)
99 strbuf_addch(&jw->json, ',');
100 else
101 jw->need_comma = 1;
104 static void fmt_double(struct json_writer *jw, int precision,
105 double value)
107 if (precision < 0) {
108 strbuf_addf(&jw->json, "%f", value);
109 } else {
110 struct strbuf fmt = STRBUF_INIT;
111 strbuf_addf(&fmt, "%%.%df", precision);
112 strbuf_addf(&jw->json, fmt.buf, value);
113 strbuf_release(&fmt);
117 static void object_common(struct json_writer *jw, const char *key)
119 assert_in_object(jw, key);
120 maybe_add_comma(jw);
122 if (jw->pretty) {
123 strbuf_addch(&jw->json, '\n');
124 indent_pretty(jw);
127 append_quoted_string(&jw->json, key);
128 strbuf_addch(&jw->json, ':');
129 if (jw->pretty)
130 strbuf_addch(&jw->json, ' ');
133 static void array_common(struct json_writer *jw)
135 assert_in_array(jw);
136 maybe_add_comma(jw);
138 if (jw->pretty) {
139 strbuf_addch(&jw->json, '\n');
140 indent_pretty(jw);
145 * Assert that the given JSON object or JSON array has been properly
146 * terminated. (Has closing bracket.)
148 static void assert_is_terminated(const struct json_writer *jw)
150 if (jw->open_stack.len)
151 BUG("json-writer: object: missing jw_end(): '%s'",
152 jw->json.buf);
155 void jw_object_begin(struct json_writer *jw, int pretty)
157 begin(jw, '{', pretty);
160 void jw_object_string(struct json_writer *jw, const char *key, const char *value)
162 object_common(jw, key);
163 append_quoted_string(&jw->json, value);
166 void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value)
168 object_common(jw, key);
169 strbuf_addf(&jw->json, "%"PRIdMAX, value);
172 void jw_object_double(struct json_writer *jw, const char *key, int precision,
173 double value)
175 object_common(jw, key);
176 fmt_double(jw, precision, value);
179 void jw_object_true(struct json_writer *jw, const char *key)
181 object_common(jw, key);
182 strbuf_addstr(&jw->json, "true");
185 void jw_object_false(struct json_writer *jw, const char *key)
187 object_common(jw, key);
188 strbuf_addstr(&jw->json, "false");
191 void jw_object_bool(struct json_writer *jw, const char *key, int value)
193 if (value)
194 jw_object_true(jw, key);
195 else
196 jw_object_false(jw, key);
199 void jw_object_null(struct json_writer *jw, const char *key)
201 object_common(jw, key);
202 strbuf_addstr(&jw->json, "null");
205 static void increase_indent(struct strbuf *sb,
206 const struct json_writer *jw,
207 int indent)
209 int k;
211 strbuf_reset(sb);
212 for (k = 0; k < jw->json.len; k++) {
213 char ch = jw->json.buf[k];
214 strbuf_addch(sb, ch);
215 if (ch == '\n')
216 strbuf_addchars(sb, ' ', indent);
220 static void kill_indent(struct strbuf *sb,
221 const struct json_writer *jw)
223 int k;
224 int eat_it = 0;
226 strbuf_reset(sb);
227 for (k = 0; k < jw->json.len; k++) {
228 char ch = jw->json.buf[k];
229 if (eat_it && ch == ' ')
230 continue;
231 if (ch == '\n') {
232 eat_it = 1;
233 continue;
235 eat_it = 0;
236 strbuf_addch(sb, ch);
240 static void append_sub_jw(struct json_writer *jw,
241 const struct json_writer *value)
244 * If both are pretty, increase the indentation of the sub_jw
245 * to better fit under the super.
247 * If the super is pretty, but the sub_jw is compact, leave the
248 * sub_jw compact. (We don't want to parse and rebuild the sub_jw
249 * for this debug-ish feature.)
251 * If the super is compact, and the sub_jw is pretty, convert
252 * the sub_jw to compact.
254 * If both are compact, keep the sub_jw compact.
256 if (jw->pretty && jw->open_stack.len && value->pretty) {
257 struct strbuf sb = STRBUF_INIT;
258 increase_indent(&sb, value, jw->open_stack.len * 2);
259 strbuf_addbuf(&jw->json, &sb);
260 strbuf_release(&sb);
261 return;
263 if (!jw->pretty && value->pretty) {
264 struct strbuf sb = STRBUF_INIT;
265 kill_indent(&sb, value);
266 strbuf_addbuf(&jw->json, &sb);
267 strbuf_release(&sb);
268 return;
271 strbuf_addbuf(&jw->json, &value->json);
275 * Append existing (properly terminated) JSON sub-data (object or array)
276 * as-is onto the given JSON data.
278 void jw_object_sub_jw(struct json_writer *jw, const char *key,
279 const struct json_writer *value)
281 assert_is_terminated(value);
283 object_common(jw, key);
284 append_sub_jw(jw, value);
287 void jw_object_inline_begin_object(struct json_writer *jw, const char *key)
289 object_common(jw, key);
291 jw_object_begin(jw, jw->pretty);
294 void jw_object_inline_begin_array(struct json_writer *jw, const char *key)
296 object_common(jw, key);
298 jw_array_begin(jw, jw->pretty);
301 void jw_array_begin(struct json_writer *jw, int pretty)
303 begin(jw, '[', pretty);
306 void jw_array_string(struct json_writer *jw, const char *value)
308 array_common(jw);
309 append_quoted_string(&jw->json, value);
312 void jw_array_intmax(struct json_writer *jw, intmax_t value)
314 array_common(jw);
315 strbuf_addf(&jw->json, "%"PRIdMAX, value);
318 void jw_array_double(struct json_writer *jw, int precision, double value)
320 array_common(jw);
321 fmt_double(jw, precision, value);
324 void jw_array_true(struct json_writer *jw)
326 array_common(jw);
327 strbuf_addstr(&jw->json, "true");
330 void jw_array_false(struct json_writer *jw)
332 array_common(jw);
333 strbuf_addstr(&jw->json, "false");
336 void jw_array_bool(struct json_writer *jw, int value)
338 if (value)
339 jw_array_true(jw);
340 else
341 jw_array_false(jw);
344 void jw_array_null(struct json_writer *jw)
346 array_common(jw);
347 strbuf_addstr(&jw->json, "null");
350 void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value)
352 assert_is_terminated(value);
354 array_common(jw);
355 append_sub_jw(jw, value);
358 void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv)
360 int k;
362 for (k = 0; k < argc; k++)
363 jw_array_string(jw, argv[k]);
366 void jw_array_argv(struct json_writer *jw, const char **argv)
368 while (*argv)
369 jw_array_string(jw, *argv++);
372 void jw_array_inline_begin_object(struct json_writer *jw)
374 array_common(jw);
376 jw_object_begin(jw, jw->pretty);
379 void jw_array_inline_begin_array(struct json_writer *jw)
381 array_common(jw);
383 jw_array_begin(jw, jw->pretty);
386 int jw_is_terminated(const struct json_writer *jw)
388 return !jw->open_stack.len;
391 void jw_end(struct json_writer *jw)
393 char ch_open;
394 int len;
396 if (!jw->open_stack.len)
397 BUG("json-writer: too many jw_end(): '%s'", jw->json.buf);
399 len = jw->open_stack.len - 1;
400 ch_open = jw->open_stack.buf[len];
402 strbuf_setlen(&jw->open_stack, len);
403 jw->need_comma = 1;
405 if (jw->pretty) {
406 strbuf_addch(&jw->json, '\n');
407 indent_pretty(jw);
410 if (ch_open == '{')
411 strbuf_addch(&jw->json, '}');
412 else
413 strbuf_addch(&jw->json, ']');