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
|
#ifndef JSON_WRITER_H
#define JSON_WRITER_H
/*
* JSON data structures are defined at:
* [1] https://www.ietf.org/rfc/rfc7159.txt
* [2] https://www.json.org/
*
* The JSON-writer API allows one to build JSON data structures using a
* simple wrapper around a "struct strbuf" buffer. It is intended as a
* simple API to build output strings; it is not intended to be a general
* object model for JSON data. In particular, it does not re-order keys
* in an object (dictionary), it does not de-dup keys in an object, and
* it does not allow lookup or parsing of JSON data.
*
* All string values (both keys and string r-values) are properly quoted
* and escaped if they contain special characters.
*
* These routines create compact JSON data (with no unnecessary whitespace,
* newlines, or indenting). If you get an unexpected response, verify
* that you're not expecting a pretty JSON string.
*
* Both "JSON objects" (aka sets of k/v pairs) and "JSON array" can be
* constructed using a 'begin append* end' model.
*
* Nested objects and arrays can either be constructed bottom up (by
* creating sub object/arrays first and appending them to the super
* object/array) -or- by building them inline in one pass. This is a
* personal style and/or data shape choice.
*
* USAGE:
* ======
*
* - Initialize the json_writer with jw_init.
*
* - Open an object as the main data structure with jw_object_begin.
* Append a key-value pair to it using the jw_object_<type> functions.
* Conclude with jw_end.
*
* - Alternatively, open an array as the main data structure with
* jw_array_begin. Append a value to it using the jw_array_<type>
* functions. Conclude with jw_end.
*
* - Append a new, unterminated array or object to the current
* object using the jw_object_inline_begin_{array, object} functions.
* Similarly, append a new, unterminated array or object to
* the current array using the jw_array_inline_begin_{array, object}
* functions.
*
* - Append other json_writer as a value to the current array or object
* using the jw_{array, object}_sub_jw functions.
*
* - Extend the current array with an null-terminated array of strings
* by using jw_array_argv or with a fixed number of elements of a
* array of string by using jw_array_argc_argv.
*
* - Release the json_writer after using it by calling jw_release.
*
* See t/helper/test-json-writer.c for various usage examples.
*
* LIMITATIONS:
* ============
*
* The JSON specification [1,2] defines string values as Unicode data
* and probably UTF-8 encoded. The current json-writer API does not
* enforce this and will write any string as received. However, it will
* properly quote and backslash-escape them as necessary. It is up to
* the caller to UTF-8 encode their strings *before* passing them to this
* API. This layer should not have to try to guess the encoding or locale
* of the given strings.
*/
#include "strbuf.h"
struct json_writer
{
/*
* Buffer of the in-progress JSON currently being composed.
*/
struct strbuf json;
/*
* Simple stack of the currently open array and object forms.
* This is a string of '{' and '[' characters indicating the
* currently unterminated forms. This is used to ensure the
* properly closing character is used when popping a level and
* to know when the JSON is completely closed.
*/
struct strbuf open_stack;
unsigned int need_comma:1;
unsigned int pretty:1;
};
#define JSON_WRITER_INIT { \
.json = STRBUF_INIT, \
.open_stack = STRBUF_INIT, \
}
/*
* Initialize a json_writer with empty values.
*/
void jw_init(struct json_writer *jw);
/*
* Release the internal buffers of a json_writer.
*/
void jw_release(struct json_writer *jw);
/*
* Begin the json_writer using an object as the top-level data structure. If
* pretty is set to 1, the result will be a human-readable and indented JSON,
* and if it is set to 0 the result will be minified single-line JSON.
*/
void jw_object_begin(struct json_writer *jw, int pretty);
/*
* Begin the json_writer using an array as the top-level data structure. If
* pretty is set to 1, the result will be a human-readable and indented JSON,
* and if it is set to 0 the result will be minified single-line JSON.
*/
void jw_array_begin(struct json_writer *jw, int pretty);
/*
* Append a string field to the current object of the json_writer, given its key
* and its value. Trigger a BUG when not in an object.
*/
void jw_object_string(struct json_writer *jw, const char *key,
const char *value);
/*
* Append an int field to the current object of the json_writer, given its key
* and its value. Trigger a BUG when not in an object.
*/
void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value);
/*
* Append a double field to the current object of the json_writer, given its key
* and its value. The precision parameter defines the number of significant
* digits, where -1 can be used for maximum precision. Trigger a BUG when not in
* an object.
*/
void jw_object_double(struct json_writer *jw, const char *key, int precision,
double value);
/*
* Append a boolean field set to true to the current object of the json_writer,
* given its key. Trigger a BUG when not in an object.
*/
void jw_object_true(struct json_writer *jw, const char *key);
/*
* Append a boolean field set to false to the current object of the json_writer,
* given its key. Trigger a BUG when not in an object.
*/
void jw_object_false(struct json_writer *jw, const char *key);
/*
* Append a boolean field to the current object of the json_writer, given its
* key and its value. Trigger a BUG when not in an object.
*/
void jw_object_bool(struct json_writer *jw, const char *key, int value);
/*
* Append a null field to the current object of the json_writer, given its key.
* Trigger a BUG when not in an object.
*/
void jw_object_null(struct json_writer *jw, const char *key);
/*
* Append a field to the current object of the json_writer, given its key and
* another json_writer that represents its content. Trigger a BUG when not in
* an object.
*/
void jw_object_sub_jw(struct json_writer *jw, const char *key,
const struct json_writer *value);
/*
* Start an object as the value of a field in the current object of the
* json_writer. Trigger a BUG when not in an object.
*/
void jw_object_inline_begin_object(struct json_writer *jw, const char *key);
/*
* Start an array as the value of a field in the current object of the
* json_writer. Trigger a BUG when not in an object.
*/
void jw_object_inline_begin_array(struct json_writer *jw, const char *key);
/*
* Append a string value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_string(struct json_writer *jw, const char *value);
/*
* Append an int value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_intmax(struct json_writer *jw, intmax_t value);
/*
* Append a double value to the current array of the json_writer. The precision
* parameter defines the number of significant digits, where -1 can be used for
* maximum precision. Trigger a BUG when not in an array.
*/
void jw_array_double(struct json_writer *jw, int precision, double value);
/*
* Append a true value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_true(struct json_writer *jw);
/*
* Append a false value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_false(struct json_writer *jw);
/*
* Append a boolean value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_bool(struct json_writer *jw, int value);
/*
* Append a null value to the current array of the json_writer. Trigger a BUG
* when not in an array.
*/
void jw_array_null(struct json_writer *jw);
/*
* Append a json_writer as a value to the current array of the
* json_writer. Trigger a BUG when not in an array.
*/
void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value);
/*
* Append the first argc values from the argv array of strings to the current
* array of the json_writer. Trigger a BUG when not in an array.
*
* This function does not provide safety for cases where the array has less than
* argc values.
*/
void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv);
/*
* Append a null-terminated array of strings to the current array of the
* json_writer. Trigger a BUG when not in an array.
*/
void jw_array_argv(struct json_writer *jw, const char **argv);
/*
* Start an object as a value in the current array of the json_writer. Trigger a
* BUG when not in an array.
*/
void jw_array_inline_begin_object(struct json_writer *jw);
/*
* Start an array as a value in the current array. Trigger a BUG when not in an
* array.
*/
void jw_array_inline_begin_array(struct json_writer *jw);
/*
* Return whether the json_writer is terminated. In other words, if the all the
* objects and arrays are already closed.
*/
int jw_is_terminated(const struct json_writer *jw);
/*
* Terminates the current object or array of the json_writer. In other words,
* append a ] if the current array is not closed or } if the current object
* is not closed.
*
* Abort the execution if there's no object or array that can be terminated.
*/
void jw_end(struct json_writer *jw);
#endif /* JSON_WRITER_H */
|