|
28 | 28 | * object/array) -or- by building them inline in one pass. This is a
|
29 | 29 | * personal style and/or data shape choice.
|
30 | 30 | *
|
| 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 | + * |
31 | 59 | * See t/helper/test-json-writer.c for various usage examples.
|
32 | 60 | *
|
33 | 61 | * LIMITATIONS:
|
@@ -69,42 +97,185 @@ struct json_writer
|
69 | 97 | .open_stack = STRBUF_INIT, \
|
70 | 98 | }
|
71 | 99 |
|
| 100 | +/* |
| 101 | + * Initialize a json_writer with empty values. |
| 102 | + */ |
72 | 103 | void jw_init(struct json_writer *jw);
|
| 104 | + |
| 105 | +/* |
| 106 | + * Release the internal buffers of a json_writer. |
| 107 | + */ |
73 | 108 | void jw_release(struct json_writer *jw);
|
74 | 109 |
|
| 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 | + */ |
75 | 115 | 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 | + */ |
76 | 122 | void jw_array_begin(struct json_writer *jw, int pretty);
|
77 | 123 |
|
| 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 | + */ |
78 | 128 | void jw_object_string(struct json_writer *jw, const char *key,
|
79 | 129 | 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 | + */ |
80 | 135 | 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 | + */ |
81 | 143 | void jw_object_double(struct json_writer *jw, const char *key, int precision,
|
82 | 144 | 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 | + */ |
83 | 150 | 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 | + */ |
84 | 156 | 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 | + */ |
85 | 162 | 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 | + */ |
86 | 168 | 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 | + */ |
87 | 175 | void jw_object_sub_jw(struct json_writer *jw, const char *key,
|
88 | 176 | const struct json_writer *value);
|
89 | 177 |
|
| 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 | + */ |
90 | 182 | 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 | + */ |
91 | 188 | void jw_object_inline_begin_array(struct json_writer *jw, const char *key);
|
92 | 189 |
|
| 190 | +/* |
| 191 | + * Append a string value to the current array of the json_writer. Trigger a BUG |
| 192 | + * when not in an array. |
| 193 | + */ |
93 | 194 | 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 | + */ |
94 | 200 | 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 | + */ |
95 | 207 | 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 | + */ |
96 | 213 | 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 | + */ |
97 | 219 | 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 | + */ |
98 | 225 | 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 | + */ |
99 | 231 | 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 | + */ |
100 | 237 | 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 | + */ |
101 | 246 | 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 | + */ |
102 | 252 | void jw_array_argv(struct json_writer *jw, const char **argv);
|
103 | 253 |
|
| 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 | + */ |
104 | 258 | 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 | + */ |
105 | 264 | void jw_array_inline_begin_array(struct json_writer *jw);
|
106 | 265 |
|
| 266 | +/* |
| 267 | + * Return whether the json_writer is terminated. In other words, if the all the |
| 268 | + * objects and arrays are already closed. |
| 269 | + */ |
107 | 270 | 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 | + */ |
108 | 279 | void jw_end(struct json_writer *jw);
|
109 | 280 |
|
110 | 281 | #endif /* JSON_WRITER_H */
|
0 commit comments