summaryrefslogtreecommitdiff
path: root/json-writer.c
blob: aadb9dbddc33ef38a45342defd5fcccfd9c07d57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "cache.h"
#include "json-writer.h"
 
void jw_init(struct json_writer *jw)
{
	strbuf_init(&jw->json, 0);
	strbuf_init(&jw->open_stack, 0);
	jw->need_comma = 0;
	jw->pretty = 0;
}
 
void jw_release(struct json_writer *jw)
{
	strbuf_release(&jw->json);
	strbuf_release(&jw->open_stack);
}
 
/*
 * Append JSON-quoted version of the given string to 'out'.
 */
static void append_quoted_string(struct strbuf *out, const char *in)
{
	unsigned char c;
 
	strbuf_addch(out, '"');
	while ((c = *in++) != '\0') {
		if (c == '"')
			strbuf_addstr(out, "\\\"");
		else if (c == '\\')
			strbuf_addstr(out, "\\\\");
		else if (c == '\n')
			strbuf_addstr(out, "\\n");
		else if (c == '\r')
			strbuf_addstr(out, "\\r");
		else if (c == '\t')
			strbuf_addstr(out, "\\t");
		else if (c == '\f')
			strbuf_addstr(out, "\\f");
		else if (c == '\b')
			strbuf_addstr(out, "\\b");
		else if (c < 0x20)
			strbuf_addf(out, "\\u%04x", c);
		else
			strbuf_addch(out, c);
	}
	strbuf_addch(out, '"');
}
 
static void indent_pretty(struct json_writer *jw)
{
	int k;
 
	for (k = 0; k < jw->open_stack.len; k++)
		strbuf_addstr(&jw->json, "  ");
}
 
/*
 * Begin an object or array (either top-level or nested within the currently
 * open object or array).
 */
static void begin(struct json_writer *jw, char ch_open, int pretty)
{
	jw->pretty = pretty;
 
	strbuf_addch(&jw->json, ch_open);
 
	strbuf_addch(&jw->open_stack, ch_open);
	jw->need_comma = 0;
}
 
/*
 * Assert that the top of the open-stack is an object.
 */
static void assert_in_object(const struct json_writer *jw, const char *key)
{
	if (!jw->open_stack.len)
		BUG("json-writer: object: missing jw_object_begin(): '%s'", key);
	if (jw->open_stack.buf[jw->open_stack.len - 1] != '{')
		BUG("json-writer: object: not in object: '%s'", key);
}
 
/*
 * Assert that the top of the open-stack is an array.
 */
static void assert_in_array(const struct json_writer *jw)
{
	if (!jw->open_stack.len)
		BUG("json-writer: array: missing jw_array_begin()");
	if (jw->open_stack.buf[jw->open_stack.len - 1] != '[')
		BUG("json-writer: array: not in array");
}
 
/*
 * Add comma if we have already seen a member at this level.
 */
static void maybe_add_comma(struct json_writer *jw)
{
	if (jw->need_comma)
		strbuf_addch(&jw->json, ',');
	else
		jw->need_comma = 1;
}
 
static void fmt_double(struct json_writer *jw, int precision,
			      double value)
{
	if (precision < 0) {
		strbuf_addf(&jw->json, "%f", value);
	} else {
		struct strbuf fmt = STRBUF_INIT;
		strbuf_addf(&fmt, "%%.%df", precision);
		strbuf_addf(&jw->json, fmt.buf, value);
		strbuf_release(&fmt);
	}
}
 
static void object_common(struct json_writer *jw, const char *key)
{
	assert_in_object(jw, key);
	maybe_add_comma(jw);
 
	if (jw->pretty) {
		strbuf_addch(&jw->json, '\n');
		indent_pretty(jw);
	}
 
	append_quoted_string(&jw->json, key);
	strbuf_addch(&jw->json, ':');
	if (jw->pretty)
		strbuf_addch(&jw->json, ' ');
}
 
static void array_common(struct json_writer *jw)
{
	assert_in_array(jw);
	maybe_add_comma(jw);
 
	if (jw->pretty) {
		strbuf_addch(&jw->json, '\n');
		indent_pretty(jw);
	}
}
 
/*
 * Assert that the given JSON object or JSON array has been properly
 * terminated.  (Has closing bracket.)
 */
static void assert_is_terminated(const struct json_writer *jw)
{
	if (jw->open_stack.len)
		BUG("json-writer: object: missing jw_end(): '%s'",
		    jw->json.buf);
}
 
void jw_object_begin(struct json_writer *jw, int pretty)
{
	begin(jw, '{', pretty);
}
 
void jw_object_string(struct json_writer *jw, const char *key, const char *value)
{
	object_common(jw, key);
	append_quoted_string(&jw->json, value);
}
 
void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value)
{
	object_common(jw, key);
	strbuf_addf(&jw->json, "%"PRIdMAX, value);
}
 
void jw_object_double(struct json_writer *jw, const char *key, int precision,
		      double value)
{
	object_common(jw, key);
	fmt_double(jw, precision, value);
}
 
void jw_object_true(struct json_writer *jw, const char *key)
{
	object_common(jw, key);
	strbuf_addstr(&jw->json, "true");
}
 
void jw_object_false(struct json_writer *jw, const char *key)
{
	object_common(jw, key);
	strbuf_addstr(&jw->json, "false");
}
 
void jw_object_bool(struct json_writer *jw, const char *key, int value)
{
	if (value)
		jw_object_true(jw, key);
	else
		jw_object_false(jw, key);
}
 
void jw_object_null(struct json_writer *jw, const char *key)
{
	object_common(jw, key);
	strbuf_addstr(&jw->json, "null");
}
 
static void increase_indent(struct strbuf *sb,
			    const struct json_writer *jw,
			    int indent)
{
	int k;
 
	strbuf_reset(sb);
	for (k = 0; k < jw->json.len; k++) {
		char ch = jw->json.buf[k];
		strbuf_addch(sb, ch);
		if (ch == '\n')
			strbuf_addchars(sb, ' ', indent);
	}
}
 
static void kill_indent(struct strbuf *sb,
			const struct json_writer *jw)
{
	int k;
	int eat_it = 0;
 
	strbuf_reset(sb);
	for (k = 0; k < jw->json.len; k++) {
		char ch = jw->json.buf[k];
		if (eat_it && ch == ' ')
			continue;
		if (ch == '\n') {
			eat_it = 1;
			continue;
		}
		eat_it = 0;
		strbuf_addch(sb, ch);
	}
}
 
static void append_sub_jw(struct json_writer *jw,
			  const struct json_writer *value)
{
	/*
	 * If both are pretty, increase the indentation of the sub_jw
	 * to better fit under the super.
	 *
	 * If the super is pretty, but the sub_jw is compact, leave the
	 * sub_jw compact.  (We don't want to parse and rebuild the sub_jw
	 * for this debug-ish feature.)
	 *
	 * If the super is compact, and the sub_jw is pretty, convert
	 * the sub_jw to compact.
	 *
	 * If both are compact, keep the sub_jw compact.
	 */
	if (jw->pretty && jw->open_stack.len && value->pretty) {
		struct strbuf sb = STRBUF_INIT;
		increase_indent(&sb, value, jw->open_stack.len * 2);
		strbuf_addbuf(&jw->json, &sb);
		strbuf_release(&sb);
		return;
	}
	if (!jw->pretty && value->pretty) {
		struct strbuf sb = STRBUF_INIT;
		kill_indent(&sb, value);
		strbuf_addbuf(&jw->json, &sb);
		strbuf_release(&sb);
		return;
	}
 
	strbuf_addbuf(&jw->json, &value->json);
}
 
/*
 * Append existing (properly terminated) JSON sub-data (object or array)
 * as-is onto the given JSON data.
 */
void jw_object_sub_jw(struct json_writer *jw, const char *key,
		      const struct json_writer *value)
{
	assert_is_terminated(value);
 
	object_common(jw, key);
	append_sub_jw(jw, value);
}
 
void jw_object_inline_begin_object(struct json_writer *jw, const char *key)
{
	object_common(jw, key);
 
	jw_object_begin(jw, jw->pretty);
}
 
void jw_object_inline_begin_array(struct json_writer *jw, const char *key)
{
	object_common(jw, key);
 
	jw_array_begin(jw, jw->pretty);
}
 
void jw_array_begin(struct json_writer *jw, int pretty)
{
	begin(jw, '[', pretty);
}
 
void jw_array_string(struct json_writer *jw, const char *value)
{
	array_common(jw);
	append_quoted_string(&jw->json, value);
}
 
void jw_array_intmax(struct json_writer *jw, intmax_t value)
{
	array_common(jw);
	strbuf_addf(&jw->json, "%"PRIdMAX, value);
}
 
void jw_array_double(struct json_writer *jw, int precision, double value)
{
	array_common(jw);
	fmt_double(jw, precision, value);
}
 
void jw_array_true(struct json_writer *jw)
{
	array_common(jw);
	strbuf_addstr(&jw->json, "true");
}
 
void jw_array_false(struct json_writer *jw)
{
	array_common(jw);
	strbuf_addstr(&jw->json, "false");
}
 
void jw_array_bool(struct json_writer *jw, int value)
{
	if (value)
		jw_array_true(jw);
	else
		jw_array_false(jw);
}
 
void jw_array_null(struct json_writer *jw)
{
	array_common(jw);
	strbuf_addstr(&jw->json, "null");
}
 
void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value)
{
	assert_is_terminated(value);
 
	array_common(jw);
	append_sub_jw(jw, value);
}
 
void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv)
{
	int k;
 
	for (k = 0; k < argc; k++)
		jw_array_string(jw, argv[k]);
}
 
void jw_array_argv(struct json_writer *jw, const char **argv)
{
	while (*argv)
		jw_array_string(jw, *argv++);
}
 
void jw_array_inline_begin_object(struct json_writer *jw)
{
	array_common(jw);
 
	jw_object_begin(jw, jw->pretty);
}
 
void jw_array_inline_begin_array(struct json_writer *jw)
{
	array_common(jw);
 
	jw_array_begin(jw, jw->pretty);
}
 
int jw_is_terminated(const struct json_writer *jw)
{
	return !jw->open_stack.len;
}
 
void jw_end(struct json_writer *jw)
{
	char ch_open;
	int len;
 
	if (!jw->open_stack.len)
		BUG("json-writer: too many jw_end(): '%s'", jw->json.buf);
 
	len = jw->open_stack.len - 1;
	ch_open = jw->open_stack.buf[len];
 
	strbuf_setlen(&jw->open_stack, len);
	jw->need_comma = 1;
 
	if (jw->pretty) {
		strbuf_addch(&jw->json, '\n');
		indent_pretty(jw);
	}
 
	if (ch_open == '{')
		strbuf_addch(&jw->json, '}');
	else
		strbuf_addch(&jw->json, ']');
}