1 #include "git-compat-util.h"
2 #include "json-writer.h"
4 void jw_init(struct json_writer
*jw
)
6 struct json_writer blank
= JSON_WRITER_INIT
;
7 memcpy(jw
, &blank
, sizeof(*jw
));;
10 void jw_release(struct json_writer
*jw
)
12 strbuf_release(&jw
->json
);
13 strbuf_release(&jw
->open_stack
);
17 * Append JSON-quoted version of the given string to 'out'.
19 static void append_quoted_string(struct strbuf
*out
, const char *in
)
23 strbuf_addch(out
, '"');
24 while ((c
= *in
++) != '\0') {
26 strbuf_addstr(out
, "\\\"");
28 strbuf_addstr(out
, "\\\\");
30 strbuf_addstr(out
, "\\n");
32 strbuf_addstr(out
, "\\r");
34 strbuf_addstr(out
, "\\t");
36 strbuf_addstr(out
, "\\f");
38 strbuf_addstr(out
, "\\b");
40 strbuf_addf(out
, "\\u%04x", c
);
44 strbuf_addch(out
, '"');
47 static void indent_pretty(struct json_writer
*jw
)
49 strbuf_addstrings(&jw
->json
, " ", jw
->open_stack
.len
);
53 * Begin an object or array (either top-level or nested within the currently
54 * open object or array).
56 static void begin(struct json_writer
*jw
, char ch_open
, int pretty
)
60 strbuf_addch(&jw
->json
, ch_open
);
62 strbuf_addch(&jw
->open_stack
, ch_open
);
67 * Assert that the top of the open-stack is an object.
69 static void assert_in_object(const struct json_writer
*jw
, const char *key
)
71 if (!jw
->open_stack
.len
)
72 BUG("json-writer: object: missing jw_object_begin(): '%s'", key
);
73 if (jw
->open_stack
.buf
[jw
->open_stack
.len
- 1] != '{')
74 BUG("json-writer: object: not in object: '%s'", key
);
78 * Assert that the top of the open-stack is an array.
80 static void assert_in_array(const struct json_writer
*jw
)
82 if (!jw
->open_stack
.len
)
83 BUG("json-writer: array: missing jw_array_begin()");
84 if (jw
->open_stack
.buf
[jw
->open_stack
.len
- 1] != '[')
85 BUG("json-writer: array: not in array");
89 * Add comma if we have already seen a member at this level.
91 static void maybe_add_comma(struct json_writer
*jw
)
94 strbuf_addch(&jw
->json
, ',');
99 static void fmt_double(struct json_writer
*jw
, int precision
,
103 strbuf_addf(&jw
->json
, "%f", value
);
105 struct strbuf fmt
= STRBUF_INIT
;
106 strbuf_addf(&fmt
, "%%.%df", precision
);
107 strbuf_addf(&jw
->json
, fmt
.buf
, value
);
108 strbuf_release(&fmt
);
112 static void object_common(struct json_writer
*jw
, const char *key
)
114 assert_in_object(jw
, key
);
118 strbuf_addch(&jw
->json
, '\n');
122 append_quoted_string(&jw
->json
, key
);
123 strbuf_addch(&jw
->json
, ':');
125 strbuf_addch(&jw
->json
, ' ');
128 static void array_common(struct json_writer
*jw
)
134 strbuf_addch(&jw
->json
, '\n');
140 * Assert that the given JSON object or JSON array has been properly
141 * terminated. (Has closing bracket.)
143 static void assert_is_terminated(const struct json_writer
*jw
)
145 if (jw
->open_stack
.len
)
146 BUG("json-writer: object: missing jw_end(): '%s'",
150 void jw_object_begin(struct json_writer
*jw
, int pretty
)
152 begin(jw
, '{', pretty
);
155 void jw_object_string(struct json_writer
*jw
, const char *key
, const char *value
)
157 object_common(jw
, key
);
158 append_quoted_string(&jw
->json
, value
);
161 void jw_object_intmax(struct json_writer
*jw
, const char *key
, intmax_t value
)
163 object_common(jw
, key
);
164 strbuf_addf(&jw
->json
, "%"PRIdMAX
, value
);
167 void jw_object_double(struct json_writer
*jw
, const char *key
, int precision
,
170 object_common(jw
, key
);
171 fmt_double(jw
, precision
, value
);
174 void jw_object_true(struct json_writer
*jw
, const char *key
)
176 object_common(jw
, key
);
177 strbuf_addstr(&jw
->json
, "true");
180 void jw_object_false(struct json_writer
*jw
, const char *key
)
182 object_common(jw
, key
);
183 strbuf_addstr(&jw
->json
, "false");
186 void jw_object_bool(struct json_writer
*jw
, const char *key
, int value
)
189 jw_object_true(jw
, key
);
191 jw_object_false(jw
, key
);
194 void jw_object_null(struct json_writer
*jw
, const char *key
)
196 object_common(jw
, key
);
197 strbuf_addstr(&jw
->json
, "null");
200 static void increase_indent(struct strbuf
*sb
,
201 const struct json_writer
*jw
,
207 for (k
= 0; k
< jw
->json
.len
; k
++) {
208 char ch
= jw
->json
.buf
[k
];
209 strbuf_addch(sb
, ch
);
211 strbuf_addchars(sb
, ' ', indent
);
215 static void kill_indent(struct strbuf
*sb
,
216 const struct json_writer
*jw
)
222 for (k
= 0; k
< jw
->json
.len
; k
++) {
223 char ch
= jw
->json
.buf
[k
];
224 if (eat_it
&& ch
== ' ')
231 strbuf_addch(sb
, ch
);
235 static void append_sub_jw(struct json_writer
*jw
,
236 const struct json_writer
*value
)
239 * If both are pretty, increase the indentation of the sub_jw
240 * to better fit under the super.
242 * If the super is pretty, but the sub_jw is compact, leave the
243 * sub_jw compact. (We don't want to parse and rebuild the sub_jw
244 * for this debug-ish feature.)
246 * If the super is compact, and the sub_jw is pretty, convert
247 * the sub_jw to compact.
249 * If both are compact, keep the sub_jw compact.
251 if (jw
->pretty
&& jw
->open_stack
.len
&& value
->pretty
) {
252 struct strbuf sb
= STRBUF_INIT
;
253 increase_indent(&sb
, value
, jw
->open_stack
.len
* 2);
254 strbuf_addbuf(&jw
->json
, &sb
);
258 if (!jw
->pretty
&& value
->pretty
) {
259 struct strbuf sb
= STRBUF_INIT
;
260 kill_indent(&sb
, value
);
261 strbuf_addbuf(&jw
->json
, &sb
);
266 strbuf_addbuf(&jw
->json
, &value
->json
);
270 * Append existing (properly terminated) JSON sub-data (object or array)
271 * as-is onto the given JSON data.
273 void jw_object_sub_jw(struct json_writer
*jw
, const char *key
,
274 const struct json_writer
*value
)
276 assert_is_terminated(value
);
278 object_common(jw
, key
);
279 append_sub_jw(jw
, value
);
282 void jw_object_inline_begin_object(struct json_writer
*jw
, const char *key
)
284 object_common(jw
, key
);
286 jw_object_begin(jw
, jw
->pretty
);
289 void jw_object_inline_begin_array(struct json_writer
*jw
, const char *key
)
291 object_common(jw
, key
);
293 jw_array_begin(jw
, jw
->pretty
);
296 void jw_array_begin(struct json_writer
*jw
, int pretty
)
298 begin(jw
, '[', pretty
);
301 void jw_array_string(struct json_writer
*jw
, const char *value
)
304 append_quoted_string(&jw
->json
, value
);
307 void jw_array_intmax(struct json_writer
*jw
, intmax_t value
)
310 strbuf_addf(&jw
->json
, "%"PRIdMAX
, value
);
313 void jw_array_double(struct json_writer
*jw
, int precision
, double value
)
316 fmt_double(jw
, precision
, value
);
319 void jw_array_true(struct json_writer
*jw
)
322 strbuf_addstr(&jw
->json
, "true");
325 void jw_array_false(struct json_writer
*jw
)
328 strbuf_addstr(&jw
->json
, "false");
331 void jw_array_bool(struct json_writer
*jw
, int value
)
339 void jw_array_null(struct json_writer
*jw
)
342 strbuf_addstr(&jw
->json
, "null");
345 void jw_array_sub_jw(struct json_writer
*jw
, const struct json_writer
*value
)
347 assert_is_terminated(value
);
350 append_sub_jw(jw
, value
);
353 void jw_array_argc_argv(struct json_writer
*jw
, int argc
, const char **argv
)
357 for (k
= 0; k
< argc
; k
++)
358 jw_array_string(jw
, argv
[k
]);
361 void jw_array_argv(struct json_writer
*jw
, const char **argv
)
364 jw_array_string(jw
, *argv
++);
367 void jw_array_inline_begin_object(struct json_writer
*jw
)
371 jw_object_begin(jw
, jw
->pretty
);
374 void jw_array_inline_begin_array(struct json_writer
*jw
)
378 jw_array_begin(jw
, jw
->pretty
);
381 int jw_is_terminated(const struct json_writer
*jw
)
383 return !jw
->open_stack
.len
;
386 void jw_end(struct json_writer
*jw
)
391 if (!jw
->open_stack
.len
)
392 BUG("json-writer: too many jw_end(): '%s'", jw
->json
.buf
);
394 len
= jw
->open_stack
.len
- 1;
395 ch_open
= jw
->open_stack
.buf
[len
];
397 strbuf_setlen(&jw
->open_stack
, len
);
401 strbuf_addch(&jw
->json
, '\n');
406 strbuf_addch(&jw
->json
, '}');
408 strbuf_addch(&jw
->json
, ']');