Skip to content

Commit a306da7

Browse files
committed
Merge branch 'lo/json-writer-docs'
In-code docstring updates. * lo/json-writer-docs: json-writer: describe the usage of jw_* functions json-writer: add docstrings to jw_* functions
2 parents 96d1278 + da69229 commit a306da7

File tree

2 files changed

+171
-4
lines changed

2 files changed

+171
-4
lines changed

json-writer.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ static void append_sub_jw(struct json_writer *jw,
268268
strbuf_addbuf(&jw->json, &value->json);
269269
}
270270

271-
/*
272-
* Append existing (properly terminated) JSON sub-data (object or array)
273-
* as-is onto the given JSON data.
274-
*/
275271
void jw_object_sub_jw(struct json_writer *jw, const char *key,
276272
const struct json_writer *value)
277273
{

json-writer.h

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,34 @@
2828
* object/array) -or- by building them inline in one pass. This is a
2929
* personal style and/or data shape choice.
3030
*
31+
* USAGE:
32+
* ======
33+
*
34+
* - Initialize the json_writer with jw_init.
35+
*
36+
* - Open an object as the main data structure with jw_object_begin.
37+
* Append a key-value pair to it using the jw_object_<type> functions.
38+
* Conclude with jw_end.
39+
*
40+
* - Alternatively, open an array as the main data structure with
41+
* jw_array_begin. Append a value to it using the jw_array_<type>
42+
* functions. Conclude with jw_end.
43+
*
44+
* - Append a new, unterminated array or object to the current
45+
* object using the jw_object_inline_begin_{array, object} functions.
46+
* Similarly, append a new, unterminated array or object to
47+
* the current array using the jw_array_inline_begin_{array, object}
48+
* functions.
49+
*
50+
* - Append other json_writer as a value to the current array or object
51+
* using the jw_{array, object}_sub_jw functions.
52+
*
53+
* - Extend the current array with an null-terminated array of strings
54+
* by using jw_array_argv or with a fixed number of elements of a
55+
* array of string by using jw_array_argc_argv.
56+
*
57+
* - Release the json_writer after using it by calling jw_release.
58+
*
3159
* See t/helper/test-json-writer.c for various usage examples.
3260
*
3361
* LIMITATIONS:
@@ -69,42 +97,185 @@ struct json_writer
6997
.open_stack = STRBUF_INIT, \
7098
}
7199

100+
/*
101+
* Initialize a json_writer with empty values.
102+
*/
72103
void jw_init(struct json_writer *jw);
104+
105+
/*
106+
* Release the internal buffers of a json_writer.
107+
*/
73108
void jw_release(struct json_writer *jw);
74109

110+
/*
111+
* Begin the json_writer using an object as the top-level data structure. If
112+
* pretty is set to 1, the result will be a human-readable and indented JSON,
113+
* and if it is set to 0 the result will be minified single-line JSON.
114+
*/
75115
void jw_object_begin(struct json_writer *jw, int pretty);
116+
117+
/*
118+
* Begin the json_writer using an array as the top-level data structure. If
119+
* pretty is set to 1, the result will be a human-readable and indented JSON,
120+
* and if it is set to 0 the result will be minified single-line JSON.
121+
*/
76122
void jw_array_begin(struct json_writer *jw, int pretty);
77123

124+
/*
125+
* Append a string field to the current object of the json_writer, given its key
126+
* and its value. Trigger a BUG when not in an object.
127+
*/
78128
void jw_object_string(struct json_writer *jw, const char *key,
79129
const char *value);
130+
131+
/*
132+
* Append an int field to the current object of the json_writer, given its key
133+
* and its value. Trigger a BUG when not in an object.
134+
*/
80135
void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value);
136+
137+
/*
138+
* Append a double field to the current object of the json_writer, given its key
139+
* and its value. The precision parameter defines the number of significant
140+
* digits, where -1 can be used for maximum precision. Trigger a BUG when not in
141+
* an object.
142+
*/
81143
void jw_object_double(struct json_writer *jw, const char *key, int precision,
82144
double value);
145+
146+
/*
147+
* Append a boolean field set to true to the current object of the json_writer,
148+
* given its key. Trigger a BUG when not in an object.
149+
*/
83150
void jw_object_true(struct json_writer *jw, const char *key);
151+
152+
/*
153+
* Append a boolean field set to false to the current object of the json_writer,
154+
* given its key. Trigger a BUG when not in an object.
155+
*/
84156
void jw_object_false(struct json_writer *jw, const char *key);
157+
158+
/*
159+
* Append a boolean field to the current object of the json_writer, given its
160+
* key and its value. Trigger a BUG when not in an object.
161+
*/
85162
void jw_object_bool(struct json_writer *jw, const char *key, int value);
163+
164+
/*
165+
* Append a null field to the current object of the json_writer, given its key.
166+
* Trigger a BUG when not in an object.
167+
*/
86168
void jw_object_null(struct json_writer *jw, const char *key);
169+
170+
/*
171+
* Append a field to the current object of the json_writer, given its key and
172+
* another json_writer that represents its content. Trigger a BUG when not in
173+
* an object.
174+
*/
87175
void jw_object_sub_jw(struct json_writer *jw, const char *key,
88176
const struct json_writer *value);
89177

178+
/*
179+
* Start an object as the value of a field in the current object of the
180+
* json_writer. Trigger a BUG when not in an object.
181+
*/
90182
void jw_object_inline_begin_object(struct json_writer *jw, const char *key);
183+
184+
/*
185+
* Start an array as the value of a field in the current object of the
186+
* json_writer. Trigger a BUG when not in an object.
187+
*/
91188
void jw_object_inline_begin_array(struct json_writer *jw, const char *key);
92189

190+
/*
191+
* Append a string value to the current array of the json_writer. Trigger a BUG
192+
* when not in an array.
193+
*/
93194
void jw_array_string(struct json_writer *jw, const char *value);
195+
196+
/*
197+
* Append an int value to the current array of the json_writer. Trigger a BUG
198+
* when not in an array.
199+
*/
94200
void jw_array_intmax(struct json_writer *jw, intmax_t value);
201+
202+
/*
203+
* Append a double value to the current array of the json_writer. The precision
204+
* parameter defines the number of significant digits, where -1 can be used for
205+
* maximum precision. Trigger a BUG when not in an array.
206+
*/
95207
void jw_array_double(struct json_writer *jw, int precision, double value);
208+
209+
/*
210+
* Append a true value to the current array of the json_writer. Trigger a BUG
211+
* when not in an array.
212+
*/
96213
void jw_array_true(struct json_writer *jw);
214+
215+
/*
216+
* Append a false value to the current array of the json_writer. Trigger a BUG
217+
* when not in an array.
218+
*/
97219
void jw_array_false(struct json_writer *jw);
220+
221+
/*
222+
* Append a boolean value to the current array of the json_writer. Trigger a BUG
223+
* when not in an array.
224+
*/
98225
void jw_array_bool(struct json_writer *jw, int value);
226+
227+
/*
228+
* Append a null value to the current array of the json_writer. Trigger a BUG
229+
* when not in an array.
230+
*/
99231
void jw_array_null(struct json_writer *jw);
232+
233+
/*
234+
* Append a json_writer as a value to the current array of the
235+
* json_writer. Trigger a BUG when not in an array.
236+
*/
100237
void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value);
238+
239+
/*
240+
* Append the first argc values from the argv array of strings to the current
241+
* array of the json_writer. Trigger a BUG when not in an array.
242+
*
243+
* This function does not provide safety for cases where the array has less than
244+
* argc values.
245+
*/
101246
void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv);
247+
248+
/*
249+
* Append a null-terminated array of strings to the current array of the
250+
* json_writer. Trigger a BUG when not in an array.
251+
*/
102252
void jw_array_argv(struct json_writer *jw, const char **argv);
103253

254+
/*
255+
* Start an object as a value in the current array of the json_writer. Trigger a
256+
* BUG when not in an array.
257+
*/
104258
void jw_array_inline_begin_object(struct json_writer *jw);
259+
260+
/*
261+
* Start an array as a value in the current array. Trigger a BUG when not in an
262+
* array.
263+
*/
105264
void jw_array_inline_begin_array(struct json_writer *jw);
106265

266+
/*
267+
* Return whether the json_writer is terminated. In other words, if the all the
268+
* objects and arrays are already closed.
269+
*/
107270
int jw_is_terminated(const struct json_writer *jw);
271+
272+
/*
273+
* Terminates the current object or array of the json_writer. In other words,
274+
* append a ] if the current array is not closed or } if the current object
275+
* is not closed.
276+
*
277+
* Abort the execution if there's no object or array that can be terminated.
278+
*/
108279
void jw_end(struct json_writer *jw);
109280

110281
#endif /* JSON_WRITER_H */

0 commit comments

Comments
 (0)