Skip to content

Commit fba60a4

Browse files
lucasoshirogitster
authored andcommitted
json-writer: add docstrings to jw_* functions
Add a docstring for each function that manipulates json_writers. Helped-by: Junio C Hamano <[email protected]> Helped-by: Patrick Steinhardt <[email protected]> Helped-by: Karthik Nayak <[email protected]> Signed-off-by: Lucas Seiki Oshiro <[email protected]> Acked-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb96e16 commit fba60a4

File tree

2 files changed

+143
-4
lines changed

2 files changed

+143
-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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,185 @@ struct json_writer
6969
.open_stack = STRBUF_INIT, \
7070
}
7171

72+
/*
73+
* Initialize a json_writer with empty values.
74+
*/
7275
void jw_init(struct json_writer *jw);
76+
77+
/*
78+
* Release the internal buffers of a json_writer.
79+
*/
7380
void jw_release(struct json_writer *jw);
7481

82+
/*
83+
* Begin the json_writer using an object as the top-level data structure. If
84+
* pretty is set to 1, the result will be a human-readable and indented JSON,
85+
* and if it is set to 0 the result will be minified single-line JSON.
86+
*/
7587
void jw_object_begin(struct json_writer *jw, int pretty);
88+
89+
/*
90+
* Begin the json_writer using an array as the top-level data structure. If
91+
* pretty is set to 1, the result will be a human-readable and indented JSON,
92+
* and if it is set to 0 the result will be minified single-line JSON.
93+
*/
7694
void jw_array_begin(struct json_writer *jw, int pretty);
7795

96+
/*
97+
* Append a string field to the current object of the json_writer, given its key
98+
* and its value. Trigger a BUG when not in an object.
99+
*/
78100
void jw_object_string(struct json_writer *jw, const char *key,
79101
const char *value);
102+
103+
/*
104+
* Append an int field to the current object of the json_writer, given its key
105+
* and its value. Trigger a BUG when not in an object.
106+
*/
80107
void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value);
108+
109+
/*
110+
* Append a double field to the current object of the json_writer, given its key
111+
* and its value. The precision parameter defines the number of significant
112+
* digits, where -1 can be used for maximum precision. Trigger a BUG when not in
113+
* an object.
114+
*/
81115
void jw_object_double(struct json_writer *jw, const char *key, int precision,
82116
double value);
117+
118+
/*
119+
* Append a boolean field set to true to the current object of the json_writer,
120+
* given its key. Trigger a BUG when not in an object.
121+
*/
83122
void jw_object_true(struct json_writer *jw, const char *key);
123+
124+
/*
125+
* Append a boolean field set to false to the current object of the json_writer,
126+
* given its key. Trigger a BUG when not in an object.
127+
*/
84128
void jw_object_false(struct json_writer *jw, const char *key);
129+
130+
/*
131+
* Append a boolean field to the current object of the json_writer, given its
132+
* key and its value. Trigger a BUG when not in an object.
133+
*/
85134
void jw_object_bool(struct json_writer *jw, const char *key, int value);
135+
136+
/*
137+
* Append a null field to the current object of the json_writer, given its key.
138+
* Trigger a BUG when not in an object.
139+
*/
86140
void jw_object_null(struct json_writer *jw, const char *key);
141+
142+
/*
143+
* Append a field to the current object of the json_writer, given its key and
144+
* another json_writer that represents its content. Trigger a BUG when not in
145+
* an object.
146+
*/
87147
void jw_object_sub_jw(struct json_writer *jw, const char *key,
88148
const struct json_writer *value);
89149

150+
/*
151+
* Start an object as the value of a field in the current object of the
152+
* json_writer. Trigger a BUG when not in an object.
153+
*/
90154
void jw_object_inline_begin_object(struct json_writer *jw, const char *key);
155+
156+
/*
157+
* Start an array as the value of a field in the current object of the
158+
* json_writer. Trigger a BUG when not in an object.
159+
*/
91160
void jw_object_inline_begin_array(struct json_writer *jw, const char *key);
92161

162+
/*
163+
* Append a string value to the current array of the json_writer. Trigger a BUG
164+
* when not in an array.
165+
*/
93166
void jw_array_string(struct json_writer *jw, const char *value);
167+
168+
/*
169+
* Append an int value to the current array of the json_writer. Trigger a BUG
170+
* when not in an array.
171+
*/
94172
void jw_array_intmax(struct json_writer *jw, intmax_t value);
173+
174+
/*
175+
* Append a double value to the current array of the json_writer. The precision
176+
* parameter defines the number of significant digits, where -1 can be used for
177+
* maximum precision. Trigger a BUG when not in an array.
178+
*/
95179
void jw_array_double(struct json_writer *jw, int precision, double value);
180+
181+
/*
182+
* Append a true value to the current array of the json_writer. Trigger a BUG
183+
* when not in an array.
184+
*/
96185
void jw_array_true(struct json_writer *jw);
186+
187+
/*
188+
* Append a false value to the current array of the json_writer. Trigger a BUG
189+
* when not in an array.
190+
*/
97191
void jw_array_false(struct json_writer *jw);
192+
193+
/*
194+
* Append a boolean value to the current array of the json_writer. Trigger a BUG
195+
* when not in an array.
196+
*/
98197
void jw_array_bool(struct json_writer *jw, int value);
198+
199+
/*
200+
* Append a null value to the current array of the json_writer. Trigger a BUG
201+
* when not in an array.
202+
*/
99203
void jw_array_null(struct json_writer *jw);
204+
205+
/*
206+
* Append a json_writer as a value to the current array of the
207+
* json_writer. Trigger a BUG when not in an array.
208+
*/
100209
void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value);
210+
211+
/*
212+
* Append the first argc values from the argv array of strings to the current
213+
* array of the json_writer. Trigger a BUG when not in an array.
214+
*
215+
* This function does not provide safety for cases where the array has less than
216+
* argc values.
217+
*/
101218
void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv);
219+
220+
/*
221+
* Append a null-terminated array of strings to the current array of the
222+
* json_writer. Trigger a BUG when not in an array.
223+
*/
102224
void jw_array_argv(struct json_writer *jw, const char **argv);
103225

226+
/*
227+
* Start an object as a value in the current array of the json_writer. Trigger a
228+
* BUG when not in an array.
229+
*/
104230
void jw_array_inline_begin_object(struct json_writer *jw);
231+
232+
/*
233+
* Start an array as a value in the current array. Trigger a BUG when not in an
234+
* array.
235+
*/
105236
void jw_array_inline_begin_array(struct json_writer *jw);
106237

238+
/*
239+
* Return whether the json_writer is terminated. In other words, if the all the
240+
* objects and arrays are already closed.
241+
*/
107242
int jw_is_terminated(const struct json_writer *jw);
243+
244+
/*
245+
* Terminates the current object or array of the json_writer. In other words,
246+
* append a ] if the current array is not closed or } if the current object
247+
* is not closed.
248+
*
249+
* Abort the execution if there's no object or array that can be terminated.
250+
*/
108251
void jw_end(struct json_writer *jw);
109252

110253
#endif /* JSON_WRITER_H */

0 commit comments

Comments
 (0)