4 The facility wrap-json is based on the pack/unpack API on the
5 libray jansson. The two chapters below are copied from the
6 documentation of jansson library copyrighted by Petri Lehtinen
12 This section describes functions that help to create, or *pack*, complex
13 JSON values, especially nested objects and arrays. Value building is
14 based on a *format string* that is used to tell the functions about the
17 For example, the format string `"i"` specifies a single integer value,
18 while the format string `"[ssb]"` or the equivalent `"[s, s, b]"`
19 specifies an array value with two strings and a boolean as its items:
21 /* Create the JSON integer 42 */
22 wrap_json_pack(&result, "i", 42);
24 /* Create the JSON array ["foo", "bar", true] */
25 wrap_json_pack(&result, "[ssb]", "foo", "bar", 1);
27 Here's the full list of format specifiers. The type in parentheses
28 denotes the resulting JSON type, and the type in brackets (if any)
29 denotes the C type that is expected as the corresponding argument or
32 `s` (string) \[const char \*\]
34 : Convert a null terminated UTF-8 string to a JSON string.
36 `s?` (string) \[const char \*\]
38 : Like `s`, but if the argument is *NULL*, output a JSON null value.
40 `s*` (string) \[const char \*\]
42 : Like `s`, but if the argument is *NULL*, do not output any value.
43 This format can only be used inside an object or an array. If used
44 inside an object, the corresponding key is additionally suppressed
45 when the value is omitted. See below for an example.
47 `s#` (string) \[const char \*, int\]
49 : Convert a UTF-8 buffer of a given length to a JSON string.
51 `s%` (string) \[const char \*, size\_t\]
53 : Like `s#` but the length argument is of type size\_t.
57 : Like `s`, but concatenate to the previous string. Only valid after
58 `s`, `s#`, `+` or `+#`.
60 `+#` \[const char \*, int\]
62 : Like `s#`, but concatenate to the previous string. Only valid after
63 `s`, `s#`, `+` or `+#`.
65 `+%` (string) \[const char \*, size\_t\]
67 : Like `+#` but the length argument is of type size\_t.
69 `y` (byte array) \[const uint8_t \*, size\_t\]
71 : Convert the byte array whose length is given to
72 its base64url string representation.
74 `Y` (byte array) \[const uint8_t \*, size\_t\]
76 : Like 'y' but output is base64.
80 : Output a JSON null value. No argument is consumed.
84 : Convert a C int to JSON boolean value. Zero is converted to `false`
85 and non-zero to `true`.
89 : Convert a C int to JSON integer.
91 `I` (integer) \[json\_int\_t\]
93 : Convert a C json\_int\_t to JSON integer.
97 : Convert a C double to JSON real.
99 `o` (any value) \[json\_t \*\]
101 : Output any given JSON value as-is. If the value is added to an array
102 or object, the reference to the value passed to `o` is stolen by the
105 `O` (any value) \[json\_t \*\]
107 : Like `o`, but the argument's reference count is incremented. This is
108 useful if you pack into an array or object and want to keep the
109 reference for the JSON value consumed by `O` to yourself.
111 `o?`, `O?` (any value) \[json\_t \*\]
113 : Like `o` and `O`, respectively, but if the argument is *NULL*,
114 output a JSON null value.
116 `o*`, `O*` (any value) \[json\_t \*\]
118 : Like `o` and `O`, respectively, but if the argument is *NULL*, do
119 not output any value. This format can only be used inside an object
120 or an array. If used inside an object, the corresponding key is
121 additionally suppressed. See below for an example.
125 : Build an array with contents from the inner format string. `fmt` may
126 contain objects and arrays, i.e. recursive value building is
131 : Build an object with contents from the inner format string `fmt`.
132 The first, third, etc. format specifier represent a key, and must be
133 a string (see `s`, `s#`, `+` and `+#` above), as object keys are
134 always strings. The second, fourth, etc. format specifier represent
135 a value. Any value may be an object or array, i.e. recursive value
136 building is supported.
138 Whitespace, `:` and `,` are ignored.
142 /* Build an empty JSON object */
143 wrap_json_pack(&result, "{}");
145 /* Build the JSON object {"foo": 42, "bar": 7} */
146 wrap_json_pack(&result, "{sisi}", "foo", 42, "bar", 7);
148 /* Like above, ':', ',' and whitespace are ignored */
149 wrap_json_pack(&result, "{s:i, s:i}", "foo", 42, "bar", 7);
151 /* Build the JSON array [[1, 2], {"cool": true}] */
152 wrap_json_pack(&result, "[[i,i],{s:b}]", 1, 2, "cool", 1);
154 /* Build a string from a non-null terminated buffer */
155 char buffer[4] = {'t', 'e', 's', 't'};
156 wrap_json_pack(&result, "s#", buffer, 4);
158 /* Concatenate strings together to build the JSON string "foobarbaz" */
159 wrap_json_pack(&result, "s++", "foo", "bar", "baz");
161 /* Create an empty object or array when optional members are missing */
162 wrap_json_pack(&result, "{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL);
163 wrap_json_pack(&result, "[s*,o*,O*]", NULL, NULL, NULL);
165 Parsing and Validating Values
166 -----------------------------
168 This section describes functions that help to validate complex values
169 and extract, or *unpack*, data from them. Like building values
170 <apiref-pack>, this is also based on format strings.
172 While a JSON value is unpacked, the type specified in the format string
173 is checked to match that of the JSON value. This is the validation part
174 of the process. In addition to this, the unpacking functions can also
175 check that all items of arrays and objects are unpacked. This check be
176 enabled with the format specifier `!` or by using the flag
177 `JSON_STRICT`. See below for details.
179 Here's the full list of format specifiers. The type in parentheses
180 denotes the JSON type, and the type in brackets (if any) denotes the C
181 type whose address should be passed.
183 `s` (string) \[const char \*\]
185 : Convert a JSON string to a pointer to a null terminated UTF-8
186 string. The resulting string is extracted by using
187 json\_string\_value() internally, so it exists as long as there are
188 still references to the corresponding JSON string.
190 `s%` (string) \[const char \*, size\_t \*\]
192 : Convert a JSON string to a pointer to a null terminated UTF-8 string
195 `y` (byte array) \[uint8_t \*\*, size\_t \*\]
197 : Convert an input string base64url encoded to its
198 byte array representation. The result and its length
199 are stored. The returned buffer must be freed by the caller.
201 `Y` (byte array) \[uint8_t \*\*, size\_t \*\]
203 : Like 'y' but input is base64.
207 : Expect a JSON null value. Nothing is extracted.
209 `b` (boolean) \[int\]
211 : Convert a JSON boolean value to a C int, so that `true` is converted
212 to 1 and `false` to 0.
214 `i` (integer) \[int\]
216 : Convert a JSON integer to C int.
218 `I` (integer) \[json\_int\_t\]
220 : Convert a JSON integer to C json\_int\_t.
222 `f` (real) \[double\]
224 : Convert a JSON real to C double.
226 `F` (integer or real) \[double\]
228 : Convert a JSON number (integer or real) to C double.
230 `o` (any value) \[json\_t \*\]
232 : Store a JSON value with no conversion to a json\_t pointer.
234 `O` (any value) \[json\_t \*\]
236 : Like `O`, but the JSON value's reference count is incremented.
240 : Convert each item in the JSON array according to the inner format
241 string. `fmt` may contain objects and arrays, i.e. recursive value
242 extraction is supported.
246 : Convert each item in the JSON object according to the inner format
247 string `fmt`. The first, third, etc. format specifier represent a
248 key, and must be `s`. The corresponding argument to unpack functions
249 is read as the object key. The second fourth, etc. format specifier
250 represent a value and is written to the address given as the
251 corresponding argument. **Note** that every other argument is read
252 from and every other is written to.
254 `fmt` may contain objects and arrays as values, i.e. recursive value
255 extraction is supported.
259 : This special format specifier is used to enable the check that all
260 object and array items are accessed, on a per-value basis. It must
261 appear inside an array or object as the last format specifier before
262 the closing bracket or brace.
266 : This special format specifier is the opposite of `!`. This is the default.
267 It must appear inside an array or object as the last format specifier
268 before the closing bracket or brace.
270 Whitespace, `:` and `,` are ignored.
274 /* root is the JSON integer 42 */
276 wrap_json_unpack(root, "i", &myint);
279 /* root is the JSON object {"foo": "bar", "quux": true} */
282 wrap_json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
283 assert(strcmp(str, "bar") == 0 && boolean == 1);
285 /* root is the JSON array [[1, 2], {"baz": null} */
286 wrap_json_check(root, "[[i,i], {s:n}]", "baz");
287 /* returns 0 for validation success, nothing is extracted */
289 /* root is the JSON array [1, 2, 3, 4, 5] */
291 wrap_json_unpack(root, "[ii!]", &myint1, &myint2);
292 /* returns -1 for failed validation */
294 /* root is an empty JSON object */
295 int myint = 0, myint2 = 0, myint3 = 0;
296 wrap_json_unpack(root, "{s?i, s?[ii]}",
298 "bar", &myint2, &myint3);
299 /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */
305 Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
307 Permission is hereby granted, free of charge, to any person obtaining a copy
308 of this software and associated documentation files (the "Software"), to deal
309 in the Software without restriction, including without limitation the rights
310 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
311 copies of the Software, and to permit persons to whom the Software is
312 furnished to do so, subject to the following conditions:
314 The above copyright notice and this permission notice shall be included in
315 all copies or substantial portions of the Software.
317 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
318 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
319 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
320 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
321 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
322 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN