2190
|
1 |
.. _apiref:
|
|
2 |
|
|
3 |
*************
|
|
4 |
API Reference
|
|
5 |
*************
|
|
6 |
|
|
7 |
.. highlight:: c
|
|
8 |
|
|
9 |
Preliminaries
|
|
10 |
=============
|
|
11 |
|
|
12 |
All declarations are in :file:`jansson.h`, so it's enough to
|
|
13 |
|
|
14 |
::
|
|
15 |
|
|
16 |
#include <jansson.h>
|
|
17 |
|
|
18 |
in each source file.
|
|
19 |
|
|
20 |
All constants are prefixed with ``JSON_`` (except for those describing
|
|
21 |
the library version, prefixed with ``JANSSON_``). Other identifiers
|
|
22 |
are prefixed with ``json_``. Type names are suffixed with ``_t`` and
|
|
23 |
``typedef``\ 'd so that the ``struct`` keyword need not be used.
|
|
24 |
|
|
25 |
|
|
26 |
Library Version
|
|
27 |
===============
|
|
28 |
|
|
29 |
The Jansson version is of the form *A.B.C*, where *A* is the major
|
|
30 |
version, *B* is the minor version and *C* is the micro version. If the
|
|
31 |
micro version is zero, it's omitted from the version string, i.e. the
|
|
32 |
version string is just *A.B*.
|
|
33 |
|
|
34 |
When a new release only fixes bugs and doesn't add new features or
|
|
35 |
functionality, the micro version is incremented. When new features are
|
|
36 |
added in a backwards compatible way, the minor version is incremented
|
|
37 |
and the micro version is set to zero. When there are backwards
|
|
38 |
incompatible changes, the major version is incremented and others are
|
|
39 |
set to zero.
|
|
40 |
|
|
41 |
The following preprocessor constants specify the current version of
|
|
42 |
the library:
|
|
43 |
|
|
44 |
``JANSSON_MAJOR_VERSION``, ``JANSSON_MINOR_VERSION``, ``JANSSON_MICRO_VERSION``
|
|
45 |
Integers specifying the major, minor and micro versions,
|
|
46 |
respectively.
|
|
47 |
|
|
48 |
``JANSSON_VERSION``
|
|
49 |
A string representation of the current version, e.g. ``"1.2.1"`` or
|
|
50 |
``"1.3"``.
|
|
51 |
|
|
52 |
``JANSSON_VERSION_HEX``
|
|
53 |
A 3-byte hexadecimal representation of the version, e.g.
|
|
54 |
``0x010201`` for version 1.2.1 and ``0x010300`` for version 1.3.
|
|
55 |
This is useful in numeric comparisions, e.g.::
|
|
56 |
|
|
57 |
#if JANSSON_VERSION_HEX >= 0x010300
|
|
58 |
/* Code specific to version 1.3 and above */
|
|
59 |
#endif
|
|
60 |
|
|
61 |
|
|
62 |
Value Representation
|
|
63 |
====================
|
|
64 |
|
|
65 |
The JSON specification (:rfc:`4627`) defines the following data types:
|
|
66 |
*object*, *array*, *string*, *number*, *boolean*, and *null*. JSON
|
|
67 |
types are used dynamically; arrays and objects can hold any other data
|
|
68 |
type, including themselves. For this reason, Jansson's type system is
|
|
69 |
also dynamic in nature. There's one C type to represent all JSON
|
|
70 |
values, and this structure knows the type of the JSON value it holds.
|
|
71 |
|
|
72 |
.. type:: json_t
|
|
73 |
|
|
74 |
This data structure is used throughout the library to represent all
|
|
75 |
JSON values. It always contains the type of the JSON value it holds
|
|
76 |
and the value's reference count. The rest depends on the type of the
|
|
77 |
value.
|
|
78 |
|
|
79 |
Objects of :type:`json_t` are always used through a pointer. There
|
|
80 |
are APIs for querying the type, manipulating the reference count, and
|
|
81 |
for constructing and manipulating values of different types.
|
|
82 |
|
|
83 |
Unless noted otherwise, all API functions return an error value if an
|
|
84 |
error occurs. Depending on the function's signature, the error value
|
|
85 |
is either *NULL* or -1. Invalid arguments or invalid input are
|
|
86 |
apparent sources for errors. Memory allocation and I/O operations may
|
|
87 |
also cause errors.
|
|
88 |
|
|
89 |
|
|
90 |
Type
|
|
91 |
----
|
|
92 |
|
|
93 |
The type of a JSON value is queried and tested using the following
|
|
94 |
functions:
|
|
95 |
|
|
96 |
.. type:: enum json_type
|
|
97 |
|
|
98 |
The type of a JSON value. The following members are defined:
|
|
99 |
|
|
100 |
+--------------------+
|
|
101 |
| ``JSON_OBJECT`` |
|
|
102 |
+--------------------+
|
|
103 |
| ``JSON_ARRAY`` |
|
|
104 |
+--------------------+
|
|
105 |
| ``JSON_STRING`` |
|
|
106 |
+--------------------+
|
|
107 |
| ``JSON_INTEGER`` |
|
|
108 |
+--------------------+
|
|
109 |
| ``JSON_REAL`` |
|
|
110 |
+--------------------+
|
|
111 |
| ``JSON_TRUE`` |
|
|
112 |
+--------------------+
|
|
113 |
| ``JSON_FALSE`` |
|
|
114 |
+--------------------+
|
|
115 |
| ``JSON_NULL`` |
|
|
116 |
+--------------------+
|
|
117 |
|
|
118 |
These correspond to JSON object, array, string, number, boolean and
|
|
119 |
null. A number is represented by either a value of the type
|
|
120 |
``JSON_INTEGER`` or of the type ``JSON_REAL``. A true boolean value
|
|
121 |
is represented by a value of the type ``JSON_TRUE`` and false by a
|
|
122 |
value of the type ``JSON_FALSE``.
|
|
123 |
|
|
124 |
.. function:: int json_typeof(const json_t *json)
|
|
125 |
|
|
126 |
Return the type of the JSON value (a :type:`json_type` cast to
|
|
127 |
:type:`int`). *json* MUST NOT be *NULL*. This function is actually
|
|
128 |
implemented as a macro for speed.
|
|
129 |
|
|
130 |
.. function:: json_is_object(const json_t *json)
|
|
131 |
json_is_array(const json_t *json)
|
|
132 |
json_is_string(const json_t *json)
|
|
133 |
json_is_integer(const json_t *json)
|
|
134 |
json_is_real(const json_t *json)
|
|
135 |
json_is_true(const json_t *json)
|
|
136 |
json_is_false(const json_t *json)
|
|
137 |
json_is_null(const json_t *json)
|
|
138 |
|
|
139 |
These functions (actually macros) return true (non-zero) for values
|
|
140 |
of the given type, and false (zero) for values of other types and
|
|
141 |
for *NULL*.
|
|
142 |
|
|
143 |
.. function:: json_is_number(const json_t *json)
|
|
144 |
|
|
145 |
Returns true for values of types ``JSON_INTEGER`` and
|
|
146 |
``JSON_REAL``, and false for other types and for *NULL*.
|
|
147 |
|
|
148 |
.. function:: json_is_boolean(const json_t *json)
|
|
149 |
|
|
150 |
Returns true for types ``JSON_TRUE`` and ``JSON_FALSE``, and false
|
|
151 |
for values of other types and for *NULL*.
|
|
152 |
|
|
153 |
.. function:: json_boolean_value(const json_t *json)
|
|
154 |
|
|
155 |
Alias of :func:`json_is_true()`, i.e. returns 1 for ``JSON_TRUE``
|
|
156 |
and 0 otherwise.
|
|
157 |
|
|
158 |
.. versionadded:: 2.7
|
|
159 |
|
|
160 |
|
|
161 |
.. _apiref-reference-count:
|
|
162 |
|
|
163 |
Reference Count
|
|
164 |
---------------
|
|
165 |
|
|
166 |
The reference count is used to track whether a value is still in use
|
|
167 |
or not. When a value is created, it's reference count is set to 1. If
|
|
168 |
a reference to a value is kept (e.g. a value is stored somewhere for
|
|
169 |
later use), its reference count is incremented, and when the value is
|
|
170 |
no longer needed, the reference count is decremented. When the
|
|
171 |
reference count drops to zero, there are no references left, and the
|
|
172 |
value can be destroyed.
|
|
173 |
|
|
174 |
The following functions are used to manipulate the reference count.
|
|
175 |
|
|
176 |
.. function:: json_t *json_incref(json_t *json)
|
|
177 |
|
|
178 |
Increment the reference count of *json* if it's not *NULL*.
|
|
179 |
Returns *json*.
|
|
180 |
|
|
181 |
.. function:: void json_decref(json_t *json)
|
|
182 |
|
|
183 |
Decrement the reference count of *json*. As soon as a call to
|
|
184 |
:func:`json_decref()` drops the reference count to zero, the value
|
|
185 |
is destroyed and it can no longer be used.
|
|
186 |
|
|
187 |
Functions creating new JSON values set the reference count to 1. These
|
|
188 |
functions are said to return a **new reference**. Other functions
|
|
189 |
returning (existing) JSON values do not normally increase the
|
|
190 |
reference count. These functions are said to return a **borrowed
|
|
191 |
reference**. So, if the user will hold a reference to a value returned
|
|
192 |
as a borrowed reference, he must call :func:`json_incref`. As soon as
|
|
193 |
the value is no longer needed, :func:`json_decref` should be called
|
|
194 |
to release the reference.
|
|
195 |
|
|
196 |
Normally, all functions accepting a JSON value as an argument will
|
|
197 |
manage the reference, i.e. increase and decrease the reference count
|
|
198 |
as needed. However, some functions **steal** the reference, i.e. they
|
|
199 |
have the same result as if the user called :func:`json_decref()` on
|
|
200 |
the argument right after calling the function. These functions are
|
|
201 |
suffixed with ``_new`` or have ``_new_`` somewhere in their name.
|
|
202 |
|
|
203 |
For example, the following code creates a new JSON array and appends
|
|
204 |
an integer to it::
|
|
205 |
|
|
206 |
json_t *array, *integer;
|
|
207 |
|
|
208 |
array = json_array();
|
|
209 |
integer = json_integer(42);
|
|
210 |
|
|
211 |
json_array_append(array, integer);
|
|
212 |
json_decref(integer);
|
|
213 |
|
|
214 |
Note how the caller has to release the reference to the integer value
|
|
215 |
by calling :func:`json_decref()`. By using a reference stealing
|
|
216 |
function :func:`json_array_append_new()` instead of
|
|
217 |
:func:`json_array_append()`, the code becomes much simpler::
|
|
218 |
|
|
219 |
json_t *array = json_array();
|
|
220 |
json_array_append_new(array, json_integer(42));
|
|
221 |
|
|
222 |
In this case, the user doesn't have to explicitly release the
|
|
223 |
reference to the integer value, as :func:`json_array_append_new()`
|
|
224 |
steals the reference when appending the value to the array.
|
|
225 |
|
|
226 |
In the following sections it is clearly documented whether a function
|
|
227 |
will return a new or borrowed reference or steal a reference to its
|
|
228 |
argument.
|
|
229 |
|
|
230 |
|
|
231 |
Circular References
|
|
232 |
-------------------
|
|
233 |
|
|
234 |
A circular reference is created when an object or an array is,
|
|
235 |
directly or indirectly, inserted inside itself. The direct case is
|
|
236 |
simple::
|
|
237 |
|
|
238 |
json_t *obj = json_object();
|
|
239 |
json_object_set(obj, "foo", obj);
|
|
240 |
|
|
241 |
Jansson will refuse to do this, and :func:`json_object_set()` (and
|
|
242 |
all the other such functions for objects and arrays) will return with
|
|
243 |
an error status. The indirect case is the dangerous one::
|
|
244 |
|
|
245 |
json_t *arr1 = json_array(), *arr2 = json_array();
|
|
246 |
json_array_append(arr1, arr2);
|
|
247 |
json_array_append(arr2, arr1);
|
|
248 |
|
|
249 |
In this example, the array ``arr2`` is contained in the array
|
|
250 |
``arr1``, and vice versa. Jansson cannot check for this kind of
|
|
251 |
indirect circular references without a performance hit, so it's up to
|
|
252 |
the user to avoid them.
|
|
253 |
|
|
254 |
If a circular reference is created, the memory consumed by the values
|
|
255 |
cannot be freed by :func:`json_decref()`. The reference counts never
|
|
256 |
drops to zero because the values are keeping the references to each
|
|
257 |
other. Moreover, trying to encode the values with any of the encoding
|
|
258 |
functions will fail. The encoder detects circular references and
|
|
259 |
returns an error status.
|
|
260 |
|
|
261 |
|
|
262 |
True, False and Null
|
|
263 |
====================
|
|
264 |
|
|
265 |
These three values are implemented as singletons, so the returned
|
|
266 |
pointers won't change between invocations of these functions.
|
|
267 |
|
|
268 |
.. function:: json_t *json_true(void)
|
|
269 |
|
|
270 |
.. refcounting:: new
|
|
271 |
|
|
272 |
Returns the JSON true value.
|
|
273 |
|
|
274 |
.. function:: json_t *json_false(void)
|
|
275 |
|
|
276 |
.. refcounting:: new
|
|
277 |
|
|
278 |
Returns the JSON false value.
|
|
279 |
|
|
280 |
.. function:: json_t *json_boolean(val)
|
|
281 |
|
|
282 |
.. refcounting:: new
|
|
283 |
|
|
284 |
Returns JSON false if ``val`` is zero, and JSON true otherwise.
|
|
285 |
This is a macro, and equivalent to ``val ? json_true() :
|
|
286 |
json_false()``.
|
|
287 |
|
|
288 |
.. versionadded:: 2.4
|
|
289 |
|
|
290 |
|
|
291 |
.. function:: json_t *json_null(void)
|
|
292 |
|
|
293 |
.. refcounting:: new
|
|
294 |
|
|
295 |
Returns the JSON null value.
|
|
296 |
|
|
297 |
|
|
298 |
String
|
|
299 |
======
|
|
300 |
|
|
301 |
Jansson uses UTF-8 as the character encoding. All JSON strings must be
|
|
302 |
valid UTF-8 (or ASCII, as it's a subset of UTF-8). All Unicode
|
|
303 |
codepoints U+0000 through U+10FFFF are allowed, but you must use
|
|
304 |
length-aware functions if you wish to embed NUL bytes in strings.
|
|
305 |
|
|
306 |
.. function:: json_t *json_string(const char *value)
|
|
307 |
|
|
308 |
.. refcounting:: new
|
|
309 |
|
|
310 |
Returns a new JSON string, or *NULL* on error. *value* must be a
|
|
311 |
valid null terminated UTF-8 encoded Unicode string.
|
|
312 |
|
|
313 |
.. function:: json_t *json_stringn(const char *value, size_t len)
|
|
314 |
|
|
315 |
.. refcounting:: new
|
|
316 |
|
|
317 |
Like :func:`json_string`, but with explicit length, so *value* may
|
|
318 |
contain null characters or not be null terminated.
|
|
319 |
|
|
320 |
.. function:: json_t *json_string_nocheck(const char *value)
|
|
321 |
|
|
322 |
.. refcounting:: new
|
|
323 |
|
|
324 |
Like :func:`json_string`, but doesn't check that *value* is valid
|
|
325 |
UTF-8. Use this function only if you are certain that this really
|
|
326 |
is the case (e.g. you have already checked it by other means).
|
|
327 |
|
|
328 |
.. function:: json_t *json_stringn_nocheck(const char *value, size_t len)
|
|
329 |
|
|
330 |
.. refcounting:: new
|
|
331 |
|
|
332 |
Like :func:`json_string_nocheck`, but with explicit length, so
|
|
333 |
*value* may contain null characters or not be null terminated.
|
|
334 |
|
|
335 |
.. function:: const char *json_string_value(const json_t *string)
|
|
336 |
|
|
337 |
Returns the associated value of *string* as a null terminated UTF-8
|
|
338 |
encoded string, or *NULL* if *string* is not a JSON string.
|
|
339 |
|
|
340 |
The retuned value is read-only and must not be modified or freed by
|
|
341 |
the user. It is valid as long as *string* exists, i.e. as long as
|
|
342 |
its reference count has not dropped to zero.
|
|
343 |
|
|
344 |
.. function:: size_t json_string_length(const json_t *string)
|
|
345 |
|
|
346 |
Returns the length of *string* in its UTF-8 presentation, or zero
|
|
347 |
if *string* is not a JSON string.
|
|
348 |
|
|
349 |
.. function:: int json_string_set(const json_t *string, const char *value)
|
|
350 |
|
|
351 |
Sets the associated value of *string* to *value*. *value* must be a
|
|
352 |
valid UTF-8 encoded Unicode string. Returns 0 on success and -1 on
|
|
353 |
error.
|
|
354 |
|
|
355 |
.. function:: int json_string_setn(json_t *string, const char *value, size_t len)
|
|
356 |
|
|
357 |
Like :func:`json_string_set`, but with explicit length, so *value*
|
|
358 |
may contain null characters or not be null terminated.
|
|
359 |
|
|
360 |
.. function:: int json_string_set_nocheck(const json_t *string, const char *value)
|
|
361 |
|
|
362 |
Like :func:`json_string_set`, but doesn't check that *value* is
|
|
363 |
valid UTF-8. Use this function only if you are certain that this
|
|
364 |
really is the case (e.g. you have already checked it by other
|
|
365 |
means).
|
|
366 |
|
|
367 |
.. function:: int json_string_setn_nocheck(json_t *string, const char *value, size_t len)
|
|
368 |
|
|
369 |
Like :func:`json_string_set_nocheck`, but with explicit length,
|
|
370 |
so *value* may contain null characters or not be null terminated.
|
|
371 |
|
|
372 |
|
|
373 |
Number
|
|
374 |
======
|
|
375 |
|
|
376 |
The JSON specification only contains one numeric type, "number". The C
|
|
377 |
programming language has distinct types for integer and floating-point
|
|
378 |
numbers, so for practical reasons Jansson also has distinct types for
|
|
379 |
the two. They are called "integer" and "real", respectively. For more
|
|
380 |
information, see :ref:`rfc-conformance`.
|
|
381 |
|
|
382 |
.. type:: json_int_t
|
|
383 |
|
|
384 |
This is the C type that is used to store JSON integer values. It
|
|
385 |
represents the widest integer type available on your system. In
|
|
386 |
practice it's just a typedef of ``long long`` if your compiler
|
|
387 |
supports it, otherwise ``long``.
|
|
388 |
|
|
389 |
Usually, you can safely use plain ``int`` in place of
|
|
390 |
``json_int_t``, and the implicit C integer conversion handles the
|
|
391 |
rest. Only when you know that you need the full 64-bit range, you
|
|
392 |
should use ``json_int_t`` explicitly.
|
|
393 |
|
|
394 |
``JSON_INTEGER_IS_LONG_LONG``
|
|
395 |
This is a preprocessor variable that holds the value 1 if
|
|
396 |
:type:`json_int_t` is ``long long``, and 0 if it's ``long``. It
|
|
397 |
can be used as follows::
|
|
398 |
|
|
399 |
#if JSON_INTEGER_IS_LONG_LONG
|
|
400 |
/* Code specific for long long */
|
|
401 |
#else
|
|
402 |
/* Code specific for long */
|
|
403 |
#endif
|
|
404 |
|
|
405 |
``JSON_INTEGER_FORMAT``
|
|
406 |
This is a macro that expands to a :func:`printf()` conversion
|
|
407 |
specifier that corresponds to :type:`json_int_t`, without the
|
|
408 |
leading ``%`` sign, i.e. either ``"lld"`` or ``"ld"``. This macro
|
|
409 |
is required because the actual type of :type:`json_int_t` can be
|
|
410 |
either ``long`` or ``long long``, and :func:`printf()` reuiqres
|
|
411 |
different length modifiers for the two.
|
|
412 |
|
|
413 |
Example::
|
|
414 |
|
|
415 |
json_int_t x = 123123123;
|
|
416 |
printf("x is %" JSON_INTEGER_FORMAT "\n", x);
|
|
417 |
|
|
418 |
|
|
419 |
.. function:: json_t *json_integer(json_int_t value)
|
|
420 |
|
|
421 |
.. refcounting:: new
|
|
422 |
|
|
423 |
Returns a new JSON integer, or *NULL* on error.
|
|
424 |
|
|
425 |
.. function:: json_int_t json_integer_value(const json_t *integer)
|
|
426 |
|
|
427 |
Returns the associated value of *integer*, or 0 if *json* is not a
|
|
428 |
JSON integer.
|
|
429 |
|
|
430 |
.. function:: int json_integer_set(const json_t *integer, json_int_t value)
|
|
431 |
|
|
432 |
Sets the associated value of *integer* to *value*. Returns 0 on
|
|
433 |
success and -1 if *integer* is not a JSON integer.
|
|
434 |
|
|
435 |
.. function:: json_t *json_real(double value)
|
|
436 |
|
|
437 |
.. refcounting:: new
|
|
438 |
|
|
439 |
Returns a new JSON real, or *NULL* on error.
|
|
440 |
|
|
441 |
.. function:: double json_real_value(const json_t *real)
|
|
442 |
|
|
443 |
Returns the associated value of *real*, or 0.0 if *real* is not a
|
|
444 |
JSON real.
|
|
445 |
|
|
446 |
.. function:: int json_real_set(const json_t *real, double value)
|
|
447 |
|
|
448 |
Sets the associated value of *real* to *value*. Returns 0 on
|
|
449 |
success and -1 if *real* is not a JSON real.
|
|
450 |
|
|
451 |
In addition to the functions above, there's a common query function
|
|
452 |
for integers and reals:
|
|
453 |
|
|
454 |
.. function:: double json_number_value(const json_t *json)
|
|
455 |
|
|
456 |
Returns the associated value of the JSON integer or JSON real
|
|
457 |
*json*, cast to double regardless of the actual type. If *json* is
|
|
458 |
neither JSON real nor JSON integer, 0.0 is returned.
|
|
459 |
|
|
460 |
|
|
461 |
Array
|
|
462 |
=====
|
|
463 |
|
|
464 |
A JSON array is an ordered collection of other JSON values.
|
|
465 |
|
|
466 |
.. function:: json_t *json_array(void)
|
|
467 |
|
|
468 |
.. refcounting:: new
|
|
469 |
|
|
470 |
Returns a new JSON array, or *NULL* on error. Initially, the array
|
|
471 |
is empty.
|
|
472 |
|
|
473 |
.. function:: size_t json_array_size(const json_t *array)
|
|
474 |
|
|
475 |
Returns the number of elements in *array*, or 0 if *array* is NULL
|
|
476 |
or not a JSON array.
|
|
477 |
|
|
478 |
.. function:: json_t *json_array_get(const json_t *array, size_t index)
|
|
479 |
|
|
480 |
.. refcounting:: borrow
|
|
481 |
|
|
482 |
Returns the element in *array* at position *index*. The valid range
|
|
483 |
for *index* is from 0 to the return value of
|
|
484 |
:func:`json_array_size()` minus 1. If *array* is not a JSON array,
|
|
485 |
if *array* is *NULL*, or if *index* is out of range, *NULL* is
|
|
486 |
returned.
|
|
487 |
|
|
488 |
.. function:: int json_array_set(json_t *array, size_t index, json_t *value)
|
|
489 |
|
|
490 |
Replaces the element in *array* at position *index* with *value*.
|
|
491 |
The valid range for *index* is from 0 to the return value of
|
|
492 |
:func:`json_array_size()` minus 1. Returns 0 on success and -1 on
|
|
493 |
error.
|
|
494 |
|
|
495 |
.. function:: int json_array_set_new(json_t *array, size_t index, json_t *value)
|
|
496 |
|
|
497 |
Like :func:`json_array_set()` but steals the reference to *value*.
|
|
498 |
This is useful when *value* is newly created and not used after
|
|
499 |
the call.
|
|
500 |
|
|
501 |
.. function:: int json_array_append(json_t *array, json_t *value)
|
|
502 |
|
|
503 |
Appends *value* to the end of *array*, growing the size of *array*
|
|
504 |
by 1. Returns 0 on success and -1 on error.
|
|
505 |
|
|
506 |
.. function:: int json_array_append_new(json_t *array, json_t *value)
|
|
507 |
|
|
508 |
Like :func:`json_array_append()` but steals the reference to
|
|
509 |
*value*. This is useful when *value* is newly created and not used
|
|
510 |
after the call.
|
|
511 |
|
|
512 |
.. function:: int json_array_insert(json_t *array, size_t index, json_t *value)
|
|
513 |
|
|
514 |
Inserts *value* to *array* at position *index*, shifting the
|
|
515 |
elements at *index* and after it one position towards the end of
|
|
516 |
the array. Returns 0 on success and -1 on error.
|
|
517 |
|
|
518 |
.. function:: int json_array_insert_new(json_t *array, size_t index, json_t *value)
|
|
519 |
|
|
520 |
Like :func:`json_array_insert()` but steals the reference to
|
|
521 |
*value*. This is useful when *value* is newly created and not used
|
|
522 |
after the call.
|
|
523 |
|
|
524 |
.. function:: int json_array_remove(json_t *array, size_t index)
|
|
525 |
|
|
526 |
Removes the element in *array* at position *index*, shifting the
|
|
527 |
elements after *index* one position towards the start of the array.
|
|
528 |
Returns 0 on success and -1 on error. The reference count of the
|
|
529 |
removed value is decremented.
|
|
530 |
|
|
531 |
.. function:: int json_array_clear(json_t *array)
|
|
532 |
|
|
533 |
Removes all elements from *array*. Returns 0 on sucess and -1 on
|
|
534 |
error. The reference count of all removed values are decremented.
|
|
535 |
|
|
536 |
.. function:: int json_array_extend(json_t *array, json_t *other_array)
|
|
537 |
|
|
538 |
Appends all elements in *other_array* to the end of *array*.
|
|
539 |
Returns 0 on success and -1 on error.
|
|
540 |
|
|
541 |
The following macro can be used to iterate through all elements
|
|
542 |
in an array.
|
|
543 |
|
|
544 |
.. function:: json_array_foreach(array, index, value)
|
|
545 |
|
|
546 |
Iterate over every element of ``array``, running the block
|
|
547 |
of code that follows each time with the proper values set to
|
|
548 |
variables ``index`` and ``value``, of types :type:`size_t` and
|
|
549 |
:type:`json_t *` respectively. Example::
|
|
550 |
|
|
551 |
/* array is a JSON array */
|
|
552 |
size_t index;
|
|
553 |
json_t *value;
|
|
554 |
|
|
555 |
json_array_foreach(array, index, value) {
|
|
556 |
/* block of code that uses index and value */
|
|
557 |
}
|
|
558 |
|
|
559 |
The items are returned in increasing index order.
|
|
560 |
|
|
561 |
This macro expands to an ordinary ``for`` statement upon
|
|
562 |
preprocessing, so its performance is equivalent to that of
|
|
563 |
hand-written code using the array access functions.
|
|
564 |
The main advantage of this macro is that it abstracts
|
|
565 |
away the complexity, and makes for shorter, more
|
|
566 |
concise code.
|
|
567 |
|
|
568 |
.. versionadded:: 2.5
|
|
569 |
|
|
570 |
|
|
571 |
Object
|
|
572 |
======
|
|
573 |
|
|
574 |
A JSON object is a dictionary of key-value pairs, where the key is a
|
|
575 |
Unicode string and the value is any JSON value.
|
|
576 |
|
|
577 |
Even though NUL bytes are allowed in string values, they are not
|
|
578 |
allowed in object keys.
|
|
579 |
|
|
580 |
.. function:: json_t *json_object(void)
|
|
581 |
|
|
582 |
.. refcounting:: new
|
|
583 |
|
|
584 |
Returns a new JSON object, or *NULL* on error. Initially, the
|
|
585 |
object is empty.
|
|
586 |
|
|
587 |
.. function:: size_t json_object_size(const json_t *object)
|
|
588 |
|
|
589 |
Returns the number of elements in *object*, or 0 if *object* is not
|
|
590 |
a JSON object.
|
|
591 |
|
|
592 |
.. function:: json_t *json_object_get(const json_t *object, const char *key)
|
|
593 |
|
|
594 |
.. refcounting:: borrow
|
|
595 |
|
|
596 |
Get a value corresponding to *key* from *object*. Returns *NULL* if
|
|
597 |
*key* is not found and on error.
|
|
598 |
|
|
599 |
.. function:: int json_object_set(json_t *object, const char *key, json_t *value)
|
|
600 |
|
|
601 |
Set the value of *key* to *value* in *object*. *key* must be a
|
|
602 |
valid null terminated UTF-8 encoded Unicode string. If there
|
|
603 |
already is a value for *key*, it is replaced by the new value.
|
|
604 |
Returns 0 on success and -1 on error.
|
|
605 |
|
|
606 |
.. function:: int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
|
|
607 |
|
|
608 |
Like :func:`json_object_set`, but doesn't check that *key* is
|
|
609 |
valid UTF-8. Use this function only if you are certain that this
|
|
610 |
really is the case (e.g. you have already checked it by other
|
|
611 |
means).
|
|
612 |
|
|
613 |
.. function:: int json_object_set_new(json_t *object, const char *key, json_t *value)
|
|
614 |
|
|
615 |
Like :func:`json_object_set()` but steals the reference to
|
|
616 |
*value*. This is useful when *value* is newly created and not used
|
|
617 |
after the call.
|
|
618 |
|
|
619 |
.. function:: int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value)
|
|
620 |
|
|
621 |
Like :func:`json_object_set_new`, but doesn't check that *key* is
|
|
622 |
valid UTF-8. Use this function only if you are certain that this
|
|
623 |
really is the case (e.g. you have already checked it by other
|
|
624 |
means).
|
|
625 |
|
|
626 |
.. function:: int json_object_del(json_t *object, const char *key)
|
|
627 |
|
|
628 |
Delete *key* from *object* if it exists. Returns 0 on success, or
|
|
629 |
-1 if *key* was not found. The reference count of the removed value
|
|
630 |
is decremented.
|
|
631 |
|
|
632 |
.. function:: int json_object_clear(json_t *object)
|
|
633 |
|
|
634 |
Remove all elements from *object*. Returns 0 on success and -1 if
|
|
635 |
*object* is not a JSON object. The reference count of all removed
|
|
636 |
values are decremented.
|
|
637 |
|
|
638 |
.. function:: int json_object_update(json_t *object, json_t *other)
|
|
639 |
|
|
640 |
Update *object* with the key-value pairs from *other*, overwriting
|
|
641 |
existing keys. Returns 0 on success or -1 on error.
|
|
642 |
|
|
643 |
.. function:: int json_object_update_existing(json_t *object, json_t *other)
|
|
644 |
|
|
645 |
Like :func:`json_object_update()`, but only the values of existing
|
|
646 |
keys are updated. No new keys are created. Returns 0 on success or
|
|
647 |
-1 on error.
|
|
648 |
|
|
649 |
.. versionadded:: 2.3
|
|
650 |
|
|
651 |
.. function:: int json_object_update_missing(json_t *object, json_t *other)
|
|
652 |
|
|
653 |
Like :func:`json_object_update()`, but only new keys are created.
|
|
654 |
The value of any existing key is not changed. Returns 0 on success
|
|
655 |
or -1 on error.
|
|
656 |
|
|
657 |
.. versionadded:: 2.3
|
|
658 |
|
|
659 |
The following macro can be used to iterate through all key-value pairs
|
|
660 |
in an object.
|
|
661 |
|
|
662 |
.. function:: json_object_foreach(object, key, value)
|
|
663 |
|
|
664 |
Iterate over every key-value pair of ``object``, running the block
|
|
665 |
of code that follows each time with the proper values set to
|
|
666 |
variables ``key`` and ``value``, of types :type:`const char *` and
|
|
667 |
:type:`json_t *` respectively. Example::
|
|
668 |
|
|
669 |
/* obj is a JSON object */
|
|
670 |
const char *key;
|
|
671 |
json_t *value;
|
|
672 |
|
|
673 |
json_object_foreach(obj, key, value) {
|
|
674 |
/* block of code that uses key and value */
|
|
675 |
}
|
|
676 |
|
|
677 |
The items are not returned in any particular order.
|
|
678 |
|
|
679 |
This macro expands to an ordinary ``for`` statement upon
|
|
680 |
preprocessing, so its performance is equivalent to that of
|
|
681 |
hand-written iteration code using the object iteration protocol
|
|
682 |
(see below). The main advantage of this macro is that it abstracts
|
|
683 |
away the complexity behind iteration, and makes for shorter, more
|
|
684 |
concise code.
|
|
685 |
|
|
686 |
.. versionadded:: 2.3
|
|
687 |
|
|
688 |
|
|
689 |
The following functions implement an iteration protocol for objects,
|
|
690 |
allowing to iterate through all key-value pairs in an object. The
|
|
691 |
items are not returned in any particular order, as this would require
|
|
692 |
sorting due to the internal hashtable implementation.
|
|
693 |
|
|
694 |
.. function:: void *json_object_iter(json_t *object)
|
|
695 |
|
|
696 |
Returns an opaque iterator which can be used to iterate over all
|
|
697 |
key-value pairs in *object*, or *NULL* if *object* is empty.
|
|
698 |
|
|
699 |
.. function:: void *json_object_iter_at(json_t *object, const char *key)
|
|
700 |
|
|
701 |
Like :func:`json_object_iter()`, but returns an iterator to the
|
|
702 |
key-value pair in *object* whose key is equal to *key*, or NULL if
|
|
703 |
*key* is not found in *object*. Iterating forward to the end of
|
|
704 |
*object* only yields all key-value pairs of the object if *key*
|
|
705 |
happens to be the first key in the underlying hash table.
|
|
706 |
|
|
707 |
.. function:: void *json_object_iter_next(json_t *object, void *iter)
|
|
708 |
|
|
709 |
Returns an iterator pointing to the next key-value pair in *object*
|
|
710 |
after *iter*, or *NULL* if the whole object has been iterated
|
|
711 |
through.
|
|
712 |
|
|
713 |
.. function:: const char *json_object_iter_key(void *iter)
|
|
714 |
|
|
715 |
Extract the associated key from *iter*.
|
|
716 |
|
|
717 |
.. function:: json_t *json_object_iter_value(void *iter)
|
|
718 |
|
|
719 |
.. refcounting:: borrow
|
|
720 |
|
|
721 |
Extract the associated value from *iter*.
|
|
722 |
|
|
723 |
.. function:: int json_object_iter_set(json_t *object, void *iter, json_t *value)
|
|
724 |
|
|
725 |
Set the value of the key-value pair in *object*, that is pointed to
|
|
726 |
by *iter*, to *value*.
|
|
727 |
|
|
728 |
.. function:: int json_object_iter_set_new(json_t *object, void *iter, json_t *value)
|
|
729 |
|
|
730 |
Like :func:`json_object_iter_set()`, but steals the reference to
|
|
731 |
*value*. This is useful when *value* is newly created and not used
|
|
732 |
after the call.
|
|
733 |
|
|
734 |
.. function:: void *json_object_key_to_iter(const char *key)
|
|
735 |
|
|
736 |
Like :func:`json_object_iter_at()`, but much faster. Only works for
|
|
737 |
values returned by :func:`json_object_iter_key()`. Using other keys
|
|
738 |
will lead to segfaults. This function is used internally to
|
|
739 |
implement :func:`json_object_foreach`.
|
|
740 |
|
|
741 |
.. versionadded:: 2.3
|
|
742 |
|
|
743 |
The iteration protocol can be used for example as follows::
|
|
744 |
|
|
745 |
/* obj is a JSON object */
|
|
746 |
const char *key;
|
|
747 |
json_t *value;
|
|
748 |
|
|
749 |
void *iter = json_object_iter(obj);
|
|
750 |
while(iter)
|
|
751 |
{
|
|
752 |
key = json_object_iter_key(iter);
|
|
753 |
value = json_object_iter_value(iter);
|
|
754 |
/* use key and value ... */
|
|
755 |
iter = json_object_iter_next(obj, iter);
|
|
756 |
}
|
|
757 |
|
|
758 |
.. function:: void json_object_seed(size_t seed)
|
|
759 |
|
|
760 |
Seed the hash function used in Jansson's hashtable implementation.
|
|
761 |
The seed is used to randomize the hash function so that an
|
|
762 |
attacker cannot control its output.
|
|
763 |
|
|
764 |
If *seed* is 0, Jansson generates the seed itselfy by reading
|
|
765 |
random data from the operating system's entropy sources. If no
|
|
766 |
entropy sources are available, falls back to using a combination
|
|
767 |
of the current timestamp (with microsecond precision if possible)
|
|
768 |
and the process ID.
|
|
769 |
|
|
770 |
If called at all, this function must be called before any calls to
|
|
771 |
:func:`json_object()`, either explicit or implicit. If this
|
|
772 |
function is not called by the user, the first call to
|
|
773 |
:func:`json_object()` (either explicit or implicit) seeds the hash
|
|
774 |
function. See :ref:`portability-thread-safety` for notes on thread
|
|
775 |
safety.
|
|
776 |
|
|
777 |
If repeatable results are required, for e.g. unit tests, the hash
|
|
778 |
function can be "unrandomized" by calling :func:`json_object_seed`
|
|
779 |
with a constant value on program startup, e.g.
|
|
780 |
``json_object_seed(1)``.
|
|
781 |
|
|
782 |
.. versionadded:: 2.6
|
|
783 |
|
|
784 |
|
|
785 |
Error reporting
|
|
786 |
===============
|
|
787 |
|
|
788 |
Jansson uses a single struct type to pass error information to the
|
|
789 |
user. See sections :ref:`apiref-decoding`, :ref:`apiref-pack` and
|
|
790 |
:ref:`apiref-unpack` for functions that pass error information using
|
|
791 |
this struct.
|
|
792 |
|
|
793 |
.. type:: json_error_t
|
|
794 |
|
|
795 |
.. member:: char text[]
|
|
796 |
|
|
797 |
The error message (in UTF-8), or an empty string if a message is
|
|
798 |
not available.
|
|
799 |
|
|
800 |
.. member:: char source[]
|
|
801 |
|
|
802 |
Source of the error. This can be (a part of) the file name or a
|
|
803 |
special identifier in angle brackers (e.g. ``<string>``).
|
|
804 |
|
|
805 |
.. member:: int line
|
|
806 |
|
|
807 |
The line number on which the error occurred.
|
|
808 |
|
|
809 |
.. member:: int column
|
|
810 |
|
|
811 |
The column on which the error occurred. Note that this is the
|
|
812 |
*character column*, not the byte column, i.e. a multibyte UTF-8
|
|
813 |
character counts as one column.
|
|
814 |
|
|
815 |
.. member:: size_t position
|
|
816 |
|
|
817 |
The position in bytes from the start of the input. This is
|
|
818 |
useful for debugging Unicode encoding problems.
|
|
819 |
|
|
820 |
The normal use of :type:`json_error_t` is to allocate it on the stack,
|
|
821 |
and pass a pointer to a function. Example::
|
|
822 |
|
|
823 |
int main() {
|
|
824 |
json_t *json;
|
|
825 |
json_error_t error;
|
|
826 |
|
|
827 |
json = json_load_file("/path/to/file.json", 0, &error);
|
|
828 |
if(!json) {
|
|
829 |
/* the error variable contains error information */
|
|
830 |
}
|
|
831 |
...
|
|
832 |
}
|
|
833 |
|
|
834 |
Also note that if the call succeeded (``json != NULL`` in the above
|
|
835 |
example), the contents of ``error`` are generally left unspecified.
|
|
836 |
The decoding functions write to the ``position`` member also on
|
|
837 |
success. See :ref:`apiref-decoding` for more info.
|
|
838 |
|
|
839 |
All functions also accept *NULL* as the :type:`json_error_t` pointer,
|
|
840 |
in which case no error information is returned to the caller.
|
|
841 |
|
|
842 |
|
|
843 |
Encoding
|
|
844 |
========
|
|
845 |
|
|
846 |
This sections describes the functions that can be used to encode
|
|
847 |
values to JSON. By default, only objects and arrays can be encoded
|
|
848 |
directly, since they are the only valid *root* values of a JSON text.
|
|
849 |
To encode any JSON value, use the ``JSON_ENCODE_ANY`` flag (see
|
|
850 |
below).
|
|
851 |
|
|
852 |
By default, the output has no newlines, and spaces are used between
|
|
853 |
array and object elements for a readable output. This behavior can be
|
|
854 |
altered by using the ``JSON_INDENT`` and ``JSON_COMPACT`` flags
|
|
855 |
described below. A newline is never appended to the end of the encoded
|
|
856 |
JSON data.
|
|
857 |
|
|
858 |
Each function takes a *flags* parameter that controls some aspects of
|
|
859 |
how the data is encoded. Its default value is 0. The following macros
|
|
860 |
can be ORed together to obtain *flags*.
|
|
861 |
|
|
862 |
``JSON_INDENT(n)``
|
|
863 |
Pretty-print the result, using newlines between array and object
|
|
864 |
items, and indenting with *n* spaces. The valid range for *n* is
|
|
865 |
between 0 and 31 (inclusive), other values result in an undefined
|
|
866 |
output. If ``JSON_INDENT`` is not used or *n* is 0, no newlines are
|
|
867 |
inserted between array and object items.
|
|
868 |
|
|
869 |
The ``JSON_MAX_INDENT`` constant defines the maximum indentation
|
|
870 |
that can be used, and its value is 31.
|
|
871 |
|
|
872 |
.. versionchanged:: 2.7
|
|
873 |
Added ``JSON_MAX_INDENT``.
|
|
874 |
|
|
875 |
``JSON_COMPACT``
|
|
876 |
This flag enables a compact representation, i.e. sets the separator
|
|
877 |
between array and object items to ``","`` and between object keys
|
|
878 |
and values to ``":"``. Without this flag, the corresponding
|
|
879 |
separators are ``", "`` and ``": "`` for more readable output.
|
|
880 |
|
|
881 |
``JSON_ENSURE_ASCII``
|
|
882 |
If this flag is used, the output is guaranteed to consist only of
|
|
883 |
ASCII characters. This is achived by escaping all Unicode
|
|
884 |
characters outside the ASCII range.
|
|
885 |
|
|
886 |
``JSON_SORT_KEYS``
|
|
887 |
If this flag is used, all the objects in output are sorted by key.
|
|
888 |
This is useful e.g. if two JSON texts are diffed or visually
|
|
889 |
compared.
|
|
890 |
|
|
891 |
``JSON_PRESERVE_ORDER``
|
|
892 |
If this flag is used, object keys in the output are sorted into the
|
|
893 |
same order in which they were first inserted to the object. For
|
|
894 |
example, decoding a JSON text and then encoding with this flag
|
|
895 |
preserves the order of object keys.
|
|
896 |
|
|
897 |
``JSON_ENCODE_ANY``
|
|
898 |
Specifying this flag makes it possible to encode any JSON value on
|
|
899 |
its own. Without it, only objects and arrays can be passed as the
|
|
900 |
*root* value to the encoding functions.
|
|
901 |
|
|
902 |
**Note:** Encoding any value may be useful in some scenarios, but
|
|
903 |
it's generally discouraged as it violates strict compatiblity with
|
|
904 |
:rfc:`4627`. If you use this flag, don't expect interoperatibility
|
|
905 |
with other JSON systems.
|
|
906 |
|
|
907 |
.. versionadded:: 2.1
|
|
908 |
|
|
909 |
``JSON_ESCAPE_SLASH``
|
|
910 |
Escape the ``/`` characters in strings with ``\/``.
|
|
911 |
|
|
912 |
.. versionadded:: 2.4
|
|
913 |
|
|
914 |
``JSON_REAL_PRECISION(n)``
|
|
915 |
Output all real numbers with at most *n* digits of precision. The
|
|
916 |
valid range for *n* is between 0 and 31 (inclusive), and other
|
|
917 |
values result in an undefined behavior.
|
|
918 |
|
|
919 |
By default, the precision is 17, to correctly and losslessly encode
|
|
920 |
all IEEE 754 double precision floating point numbers.
|
|
921 |
|
|
922 |
.. versionadded:: 2.7
|
|
923 |
|
|
924 |
The following functions perform the actual JSON encoding. The result
|
|
925 |
is in UTF-8.
|
|
926 |
|
|
927 |
.. function:: char *json_dumps(const json_t *root, size_t flags)
|
|
928 |
|
|
929 |
Returns the JSON representation of *root* as a string, or *NULL* on
|
|
930 |
error. *flags* is described above. The return value must be freed
|
|
931 |
by the caller using :func:`free()`.
|
|
932 |
|
|
933 |
.. function:: int json_dumpf(const json_t *root, FILE *output, size_t flags)
|
|
934 |
|
|
935 |
Write the JSON representation of *root* to the stream *output*.
|
|
936 |
*flags* is described above. Returns 0 on success and -1 on error.
|
|
937 |
If an error occurs, something may have already been written to
|
|
938 |
*output*. In this case, the output is undefined and most likely not
|
|
939 |
valid JSON.
|
|
940 |
|
|
941 |
.. function:: int json_dump_file(const json_t *json, const char *path, size_t flags)
|
|
942 |
|
|
943 |
Write the JSON representation of *root* to the file *path*. If
|
|
944 |
*path* already exists, it is overwritten. *flags* is described
|
|
945 |
above. Returns 0 on success and -1 on error.
|
|
946 |
|
|
947 |
.. type:: json_dump_callback_t
|
|
948 |
|
|
949 |
A typedef for a function that's called by
|
|
950 |
:func:`json_dump_callback()`::
|
|
951 |
|
|
952 |
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
|
|
953 |
|
|
954 |
*buffer* points to a buffer containing a chunk of output, *size* is
|
|
955 |
the length of the buffer, and *data* is the corresponding
|
|
956 |
:func:`json_dump_callback()` argument passed through.
|
|
957 |
|
|
958 |
On error, the function should return -1 to stop the encoding
|
|
959 |
process. On success, it should return 0.
|
|
960 |
|
|
961 |
.. versionadded:: 2.2
|
|
962 |
|
|
963 |
.. function:: int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
|
|
964 |
|
|
965 |
Call *callback* repeatedly, passing a chunk of the JSON
|
|
966 |
representation of *root* each time. *flags* is described above.
|
|
967 |
Returns 0 on success and -1 on error.
|
|
968 |
|
|
969 |
.. versionadded:: 2.2
|
|
970 |
|
|
971 |
|
|
972 |
.. _apiref-decoding:
|
|
973 |
|
|
974 |
Decoding
|
|
975 |
========
|
|
976 |
|
|
977 |
This sections describes the functions that can be used to decode JSON
|
|
978 |
text to the Jansson representation of JSON data. The JSON
|
|
979 |
specification requires that a JSON text is either a serialized array
|
|
980 |
or object, and this requirement is also enforced with the following
|
|
981 |
functions. In other words, the top level value in the JSON text being
|
|
982 |
decoded must be either array or object. To decode any JSON value, use
|
|
983 |
the ``JSON_DECODE_ANY`` flag (see below).
|
|
984 |
|
|
985 |
See :ref:`rfc-conformance` for a discussion on Jansson's conformance
|
|
986 |
to the JSON specification. It explains many design decisions that
|
|
987 |
affect especially the behavior of the decoder.
|
|
988 |
|
|
989 |
Each function takes a *flags* parameter that can be used to control
|
|
990 |
the behavior of the decoder. Its default value is 0. The following
|
|
991 |
macros can be ORed together to obtain *flags*.
|
|
992 |
|
|
993 |
``JSON_REJECT_DUPLICATES``
|
|
994 |
Issue a decoding error if any JSON object in the input text
|
|
995 |
contains duplicate keys. Without this flag, the value of the last
|
|
996 |
occurence of each key ends up in the result. Key equivalence is
|
|
997 |
checked byte-by-byte, without special Unicode comparison
|
|
998 |
algorithms.
|
|
999 |
|
|
1000 |
.. versionadded:: 2.1
|
|
1001 |
|
|
1002 |
``JSON_DECODE_ANY``
|
|
1003 |
By default, the decoder expects an array or object as the input.
|
|
1004 |
With this flag enabled, the decoder accepts any valid JSON value.
|
|
1005 |
|
|
1006 |
**Note:** Decoding any value may be useful in some scenarios, but
|
|
1007 |
it's generally discouraged as it violates strict compatiblity with
|
|
1008 |
:rfc:`4627`. If you use this flag, don't expect interoperatibility
|
|
1009 |
with other JSON systems.
|
|
1010 |
|
|
1011 |
.. versionadded:: 2.3
|
|
1012 |
|
|
1013 |
``JSON_DISABLE_EOF_CHECK``
|
|
1014 |
By default, the decoder expects that its whole input constitutes a
|
|
1015 |
valid JSON text, and issues an error if there's extra data after
|
|
1016 |
the otherwise valid JSON input. With this flag enabled, the decoder
|
|
1017 |
stops after decoding a valid JSON array or object, and thus allows
|
|
1018 |
extra data after the JSON text.
|
|
1019 |
|
|
1020 |
Normally, reading will stop when the last ``]`` or ``}`` in the
|
|
1021 |
JSON input is encountered. If both ``JSON_DISABLE_EOF_CHECK`` and
|
|
1022 |
``JSON_DECODE_ANY`` flags are used, the decoder may read one extra
|
|
1023 |
UTF-8 code unit (up to 4 bytes of input). For example, decoding
|
|
1024 |
``4true`` correctly decodes the integer 4, but also reads the
|
|
1025 |
``t``. For this reason, if reading multiple consecutive values that
|
|
1026 |
are not arrays or objects, they should be separated by at least one
|
|
1027 |
whitespace character.
|
|
1028 |
|
|
1029 |
.. versionadded:: 2.1
|
|
1030 |
|
|
1031 |
``JSON_DECODE_INT_AS_REAL``
|
|
1032 |
JSON defines only one number type. Jansson distinguishes between
|
|
1033 |
ints and reals. For more information see :ref:`real-vs-integer`.
|
|
1034 |
With this flag enabled the decoder interprets all numbers as real
|
|
1035 |
values. Integers that do not have an exact double representation
|
|
1036 |
will silently result in a loss of precision. Integers that cause
|
|
1037 |
a double overflow will cause an error.
|
|
1038 |
|
|
1039 |
.. versionadded:: 2.5
|
|
1040 |
|
|
1041 |
``JSON_ALLOW_NUL``
|
|
1042 |
Allow ``\u0000`` escape inside string values. This is a safety
|
|
1043 |
measure; If you know your input can contain NUL bytes, use this
|
|
1044 |
flag. If you don't use this flag, you don't have to worry about NUL
|
|
1045 |
bytes inside strings unless you explicitly create themselves by
|
|
1046 |
using e.g. :func:`json_stringn()` or ``s#`` format specifier for
|
|
1047 |
:func:`json_pack()`.
|
|
1048 |
|
|
1049 |
Object keys cannot have embedded NUL bytes even if this flag is
|
|
1050 |
used.
|
|
1051 |
|
|
1052 |
.. versionadded:: 2.6
|
|
1053 |
|
|
1054 |
Each function also takes an optional :type:`json_error_t` parameter
|
|
1055 |
that is filled with error information if decoding fails. It's also
|
|
1056 |
updated on success; the number of bytes of input read is written to
|
|
1057 |
its ``position`` field. This is especially useful when using
|
|
1058 |
``JSON_DISABLE_EOF_CHECK`` to read multiple consecutive JSON texts.
|
|
1059 |
|
|
1060 |
.. versionadded:: 2.3
|
|
1061 |
Number of bytes of input read is written to the ``position`` field
|
|
1062 |
of the :type:`json_error_t` structure.
|
|
1063 |
|
|
1064 |
If no error or position information is needed, you can pass *NULL*.
|
|
1065 |
|
|
1066 |
The following functions perform the actual JSON decoding.
|
|
1067 |
|
|
1068 |
.. function:: json_t *json_loads(const char *input, size_t flags, json_error_t *error)
|
|
1069 |
|
|
1070 |
.. refcounting:: new
|
|
1071 |
|
|
1072 |
Decodes the JSON string *input* and returns the array or object it
|
|
1073 |
contains, or *NULL* on error, in which case *error* is filled with
|
|
1074 |
information about the error. *flags* is described above.
|
|
1075 |
|
|
1076 |
.. function:: json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
|
|
1077 |
|
|
1078 |
.. refcounting:: new
|
|
1079 |
|
|
1080 |
Decodes the JSON string *buffer*, whose length is *buflen*, and
|
|
1081 |
returns the array or object it contains, or *NULL* on error, in
|
|
1082 |
which case *error* is filled with information about the error. This
|
|
1083 |
is similar to :func:`json_loads()` except that the string doesn't
|
|
1084 |
need to be null-terminated. *flags* is described above.
|
|
1085 |
|
|
1086 |
.. versionadded:: 2.1
|
|
1087 |
|
|
1088 |
.. function:: json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
|
|
1089 |
|
|
1090 |
.. refcounting:: new
|
|
1091 |
|
|
1092 |
Decodes the JSON text in stream *input* and returns the array or
|
|
1093 |
object it contains, or *NULL* on error, in which case *error* is
|
|
1094 |
filled with information about the error. *flags* is described
|
|
1095 |
above.
|
|
1096 |
|
|
1097 |
This function will start reading the input from whatever position
|
|
1098 |
the input file was, without attempting to seek first. If an error
|
|
1099 |
occurs, the file position will be left indeterminate. On success,
|
|
1100 |
the file position will be at EOF, unless ``JSON_DISABLE_EOF_CHECK``
|
|
1101 |
flag was used. In this case, the file position will be at the first
|
|
1102 |
character after the last ``]`` or ``}`` in the JSON input. This
|
|
1103 |
allows calling :func:`json_loadf()` on the same ``FILE`` object
|
|
1104 |
multiple times, if the input consists of consecutive JSON texts,
|
|
1105 |
possibly separated by whitespace.
|
|
1106 |
|
|
1107 |
.. function:: json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
|
|
1108 |
|
|
1109 |
.. refcounting:: new
|
|
1110 |
|
|
1111 |
Decodes the JSON text in file *path* and returns the array or
|
|
1112 |
object it contains, or *NULL* on error, in which case *error* is
|
|
1113 |
filled with information about the error. *flags* is described
|
|
1114 |
above.
|
|
1115 |
|
|
1116 |
.. type:: json_load_callback_t
|
|
1117 |
|
|
1118 |
A typedef for a function that's called by
|
|
1119 |
:func:`json_load_callback()` to read a chunk of input data::
|
|
1120 |
|
|
1121 |
typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
|
|
1122 |
|
|
1123 |
*buffer* points to a buffer of *buflen* bytes, and *data* is the
|
|
1124 |
corresponding :func:`json_load_callback()` argument passed through.
|
|
1125 |
|
|
1126 |
On success, the function should return the number of bytes read; a
|
|
1127 |
returned value of 0 indicates that no data was read and that the
|
|
1128 |
end of file has been reached. On error, the function should return
|
|
1129 |
``(size_t)-1`` to abort the decoding process.
|
|
1130 |
|
|
1131 |
.. versionadded:: 2.4
|
|
1132 |
|
|
1133 |
.. function:: json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error)
|
|
1134 |
|
|
1135 |
.. refcounting:: new
|
|
1136 |
|
|
1137 |
Decodes the JSON text produced by repeated calls to *callback*, and
|
|
1138 |
returns the array or object it contains, or *NULL* on error, in
|
|
1139 |
which case *error* is filled with information about the error.
|
|
1140 |
*data* is passed through to *callback* on each call. *flags* is
|
|
1141 |
described above.
|
|
1142 |
|
|
1143 |
.. versionadded:: 2.4
|
|
1144 |
|
|
1145 |
|
|
1146 |
.. _apiref-pack:
|
|
1147 |
|
|
1148 |
Building Values
|
|
1149 |
===============
|
|
1150 |
|
|
1151 |
This section describes functions that help to create, or *pack*,
|
|
1152 |
complex JSON values, especially nested objects and arrays. Value
|
|
1153 |
building is based on a *format string* that is used to tell the
|
|
1154 |
functions about the expected arguments.
|
|
1155 |
|
|
1156 |
For example, the format string ``"i"`` specifies a single integer
|
|
1157 |
value, while the format string ``"[ssb]"`` or the equivalent ``"[s, s,
|
|
1158 |
b]"`` specifies an array value with two strings and a boolean as its
|
|
1159 |
items::
|
|
1160 |
|
|
1161 |
/* Create the JSON integer 42 */
|
|
1162 |
json_pack("i", 42);
|
|
1163 |
|
|
1164 |
/* Create the JSON array ["foo", "bar", true] */
|
|
1165 |
json_pack("[ssb]", "foo", "bar", 1);
|
|
1166 |
|
|
1167 |
Here's the full list of format specifiers. The type in parentheses
|
|
1168 |
denotes the resulting JSON type, and the type in brackets (if any)
|
|
1169 |
denotes the C type that is expected as the corresponding argument or
|
|
1170 |
arguments.
|
|
1171 |
|
|
1172 |
``s`` (string) [const char \*]
|
|
1173 |
Convert a NULL terminated UTF-8 string to a JSON string.
|
|
1174 |
|
|
1175 |
``s#`` (string) [const char \*, int]
|
|
1176 |
Convert a UTF-8 buffer of a given length to a JSON string.
|
|
1177 |
|
|
1178 |
.. versionadded:: 2.5
|
|
1179 |
|
|
1180 |
``s%`` (string) [const char \*, size_t]
|
|
1181 |
Like ``s#`` but the length argument is of type :type:`size_t`.
|
|
1182 |
|
|
1183 |
.. versionadded:: 2.6
|
|
1184 |
|
|
1185 |
``+`` [const char \*]
|
|
1186 |
Like ``s``, but concatenate to the previous string. Only valid
|
|
1187 |
after ``s``, ``s#``, ``+`` or ``+#``.
|
|
1188 |
|
|
1189 |
.. versionadded:: 2.5
|
|
1190 |
|
|
1191 |
``+#`` [const char \*, int]
|
|
1192 |
Like ``s#``, but concatenate to the previous string. Only valid
|
|
1193 |
after ``s``, ``s#``, ``+`` or ``+#``.
|
|
1194 |
|
|
1195 |
.. versionadded:: 2.5
|
|
1196 |
|
|
1197 |
``+%`` (string) [const char \*, size_t]
|
|
1198 |
Like ``+#`` but the length argument is of type :type:`size_t`.
|
|
1199 |
|
|
1200 |
.. versionadded:: 2.6
|
|
1201 |
|
|
1202 |
``n`` (null)
|
|
1203 |
Output a JSON null value. No argument is consumed.
|
|
1204 |
|
|
1205 |
``b`` (boolean) [int]
|
|
1206 |
Convert a C :type:`int` to JSON boolean value. Zero is converted
|
|
1207 |
to ``false`` and non-zero to ``true``.
|
|
1208 |
|
|
1209 |
``i`` (integer) [int]
|
|
1210 |
Convert a C :type:`int` to JSON integer.
|
|
1211 |
|
|
1212 |
``I`` (integer) [json_int_t]
|
|
1213 |
Convert a C :type:`json_int_t` to JSON integer.
|
|
1214 |
|
|
1215 |
``f`` (real) [double]
|
|
1216 |
Convert a C :type:`double` to JSON real.
|
|
1217 |
|
|
1218 |
``o`` (any value) [json_t \*]
|
|
1219 |
Output any given JSON value as-is. If the value is added to an
|
|
1220 |
array or object, the reference to the value passed to ``o`` is
|
|
1221 |
stolen by the container.
|
|
1222 |
|
|
1223 |
``O`` (any value) [json_t \*]
|
|
1224 |
Like ``o``, but the argument's reference count is incremented.
|
|
1225 |
This is useful if you pack into an array or object and want to
|
|
1226 |
keep the reference for the JSON value consumed by ``O`` to
|
|
1227 |
yourself.
|
|
1228 |
|
|
1229 |
``[fmt]`` (array)
|
|
1230 |
Build an array with contents from the inner format string. ``fmt``
|
|
1231 |
may contain objects and arrays, i.e. recursive value building is
|
|
1232 |
supported.
|
|
1233 |
|
|
1234 |
``{fmt}`` (object)
|
|
1235 |
Build an object with contents from the inner format string
|
|
1236 |
``fmt``. The first, third, etc. format specifier represent a key,
|
|
1237 |
and must be a string (see ``s``, ``s#``, ``+`` and ``+#`` above),
|
|
1238 |
as object keys are always strings. The second, fourth, etc. format
|
|
1239 |
specifier represent a value. Any value may be an object or array,
|
|
1240 |
i.e. recursive value building is supported.
|
|
1241 |
|
|
1242 |
Whitespace, ``:`` and ``,`` are ignored.
|
|
1243 |
|
|
1244 |
The following functions compose the value building API:
|
|
1245 |
|
|
1246 |
.. function:: json_t *json_pack(const char *fmt, ...)
|
|
1247 |
|
|
1248 |
.. refcounting:: new
|
|
1249 |
|
|
1250 |
Build a new JSON value according to the format string *fmt*. For
|
|
1251 |
each format specifier (except for ``{}[]n``), one or more arguments
|
|
1252 |
are consumed and used to build the corresponding value. Returns
|
|
1253 |
*NULL* on error.
|
|
1254 |
|
|
1255 |
.. function:: json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
|
|
1256 |
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap)
|
|
1257 |
|
|
1258 |
.. refcounting:: new
|
|
1259 |
|
|
1260 |
Like :func:`json_pack()`, but an in the case of an error, an error
|
|
1261 |
message is written to *error*, if it's not *NULL*. The *flags*
|
|
1262 |
parameter is currently unused and should be set to 0.
|
|
1263 |
|
|
1264 |
As only the errors in format string (and out-of-memory errors) can
|
|
1265 |
be caught by the packer, these two functions are most likely only
|
|
1266 |
useful for debugging format strings.
|
|
1267 |
|
|
1268 |
More examples::
|
|
1269 |
|
|
1270 |
/* Build an empty JSON object */
|
|
1271 |
json_pack("{}");
|
|
1272 |
|
|
1273 |
/* Build the JSON object {"foo": 42, "bar": 7} */
|
|
1274 |
json_pack("{sisi}", "foo", 42, "bar", 7);
|
|
1275 |
|
|
1276 |
/* Like above, ':', ',' and whitespace are ignored */
|
|
1277 |
json_pack("{s:i, s:i}", "foo", 42, "bar", 7);
|
|
1278 |
|
|
1279 |
/* Build the JSON array [[1, 2], {"cool": true}] */
|
|
1280 |
json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
|
|
1281 |
|
|
1282 |
/* Build a string from a non-NUL terminated buffer */
|
|
1283 |
char buffer[4] = {'t', 'e', 's', 't'};
|
|
1284 |
json_pack("s#", buffer, 4);
|
|
1285 |
|
|
1286 |
/* Concatentate strings together to build the JSON string "foobarbaz" */
|
|
1287 |
json_pack("s++", "foo", "bar", "baz");
|
|
1288 |
|
|
1289 |
|
|
1290 |
.. _apiref-unpack:
|
|
1291 |
|
|
1292 |
Parsing and Validating Values
|
|
1293 |
=============================
|
|
1294 |
|
|
1295 |
This section describes functions that help to validate complex values
|
|
1296 |
and extract, or *unpack*, data from them. Like :ref:`building values
|
|
1297 |
<apiref-pack>`, this is also based on format strings.
|
|
1298 |
|
|
1299 |
While a JSON value is unpacked, the type specified in the format
|
|
1300 |
string is checked to match that of the JSON value. This is the
|
|
1301 |
validation part of the process. In addition to this, the unpacking
|
|
1302 |
functions can also check that all items of arrays and objects are
|
|
1303 |
unpacked. This check be enabled with the format specifier ``!`` or by
|
|
1304 |
using the flag ``JSON_STRICT``. See below for details.
|
|
1305 |
|
|
1306 |
Here's the full list of format specifiers. The type in parentheses
|
|
1307 |
denotes the JSON type, and the type in brackets (if any) denotes the C
|
|
1308 |
type whose address should be passed.
|
|
1309 |
|
|
1310 |
``s`` (string) [const char \*]
|
|
1311 |
Convert a JSON string to a pointer to a NULL terminated UTF-8
|
|
1312 |
string. The resulting string is extracted by using
|
|
1313 |
:func:`json_string_value()` internally, so it exists as long as
|
|
1314 |
there are still references to the corresponding JSON string.
|
|
1315 |
|
|
1316 |
``s%`` (string) [const char \*, size_t \*]
|
|
1317 |
Convert a JSON string to a pointer to a NULL terminated UTF-8
|
|
1318 |
string and its length.
|
|
1319 |
|
|
1320 |
.. versionadded:: 2.6
|
|
1321 |
|
|
1322 |
``n`` (null)
|
|
1323 |
Expect a JSON null value. Nothing is extracted.
|
|
1324 |
|
|
1325 |
``b`` (boolean) [int]
|
|
1326 |
Convert a JSON boolean value to a C :type:`int`, so that ``true``
|
|
1327 |
is converted to 1 and ``false`` to 0.
|
|
1328 |
|
|
1329 |
``i`` (integer) [int]
|
|
1330 |
Convert a JSON integer to C :type:`int`.
|
|
1331 |
|
|
1332 |
``I`` (integer) [json_int_t]
|
|
1333 |
Convert a JSON integer to C :type:`json_int_t`.
|
|
1334 |
|
|
1335 |
``f`` (real) [double]
|
|
1336 |
Convert a JSON real to C :type:`double`.
|
|
1337 |
|
|
1338 |
``F`` (integer or real) [double]
|
|
1339 |
Convert a JSON number (integer or real) to C :type:`double`.
|
|
1340 |
|
|
1341 |
``o`` (any value) [json_t \*]
|
|
1342 |
Store a JSON value with no conversion to a :type:`json_t` pointer.
|
|
1343 |
|
|
1344 |
``O`` (any value) [json_t \*]
|
|
1345 |
Like ``O``, but the JSON value's reference count is incremented.
|
|
1346 |
|
|
1347 |
``[fmt]`` (array)
|
|
1348 |
Convert each item in the JSON array according to the inner format
|
|
1349 |
string. ``fmt`` may contain objects and arrays, i.e. recursive
|
|
1350 |
value extraction is supporetd.
|
|
1351 |
|
|
1352 |
``{fmt}`` (object)
|
|
1353 |
Convert each item in the JSON object according to the inner format
|
|
1354 |
string ``fmt``. The first, third, etc. format specifier represent
|
|
1355 |
a key, and must be ``s``. The corresponding argument to unpack
|
|
1356 |
functions is read as the object key. The second fourth, etc.
|
|
1357 |
format specifier represent a value and is written to the address
|
|
1358 |
given as the corresponding argument. **Note** that every other
|
|
1359 |
argument is read from and every other is written to.
|
|
1360 |
|
|
1361 |
``fmt`` may contain objects and arrays as values, i.e. recursive
|
|
1362 |
value extraction is supporetd.
|
|
1363 |
|
|
1364 |
.. versionadded:: 2.3
|
|
1365 |
Any ``s`` representing a key may be suffixed with a ``?`` to
|
|
1366 |
make the key optional. If the key is not found, nothing is
|
|
1367 |
extracted. See below for an example.
|
|
1368 |
|
|
1369 |
``!``
|
|
1370 |
This special format specifier is used to enable the check that
|
|
1371 |
all object and array items are accessed, on a per-value basis. It
|
|
1372 |
must appear inside an array or object as the last format specifier
|
|
1373 |
before the closing bracket or brace. To enable the check globally,
|
|
1374 |
use the ``JSON_STRICT`` unpacking flag.
|
|
1375 |
|
|
1376 |
``*``
|
|
1377 |
This special format specifier is the opposite of ``!``. If the
|
|
1378 |
``JSON_STRICT`` flag is used, ``*`` can be used to disable the
|
|
1379 |
strict check on a per-value basis. It must appear inside an array
|
|
1380 |
or object as the last format specifier before the closing bracket
|
|
1381 |
or brace.
|
|
1382 |
|
|
1383 |
Whitespace, ``:`` and ``,`` are ignored.
|
|
1384 |
|
|
1385 |
The following functions compose the parsing and validation API:
|
|
1386 |
|
|
1387 |
.. function:: int json_unpack(json_t *root, const char *fmt, ...)
|
|
1388 |
|
|
1389 |
Validate and unpack the JSON value *root* according to the format
|
|
1390 |
string *fmt*. Returns 0 on success and -1 on failure.
|
|
1391 |
|
|
1392 |
.. function:: int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
|
|
1393 |
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap)
|
|
1394 |
|
|
1395 |
Validate and unpack the JSON value *root* according to the format
|
|
1396 |
string *fmt*. If an error occurs and *error* is not *NULL*, write
|
|
1397 |
error information to *error*. *flags* can be used to control the
|
|
1398 |
behaviour of the unpacker, see below for the flags. Returns 0 on
|
|
1399 |
success and -1 on failure.
|
|
1400 |
|
|
1401 |
.. note::
|
|
1402 |
|
|
1403 |
The first argument of all unpack functions is ``json_t *root``
|
|
1404 |
instead of ``const json_t *root``, because the use of ``O`` format
|
|
1405 |
specifier causes the reference count of ``root``, or some value
|
|
1406 |
reachable from ``root``, to be increased. Furthermore, the ``o``
|
|
1407 |
format specifier may be used to extract a value as-is, which allows
|
|
1408 |
modifying the structure or contents of a value reachable from
|
|
1409 |
``root``.
|
|
1410 |
|
|
1411 |
If the ``O`` and ``o`` format specifiers are not used, it's
|
|
1412 |
perfectly safe to cast a ``const json_t *`` variable to plain
|
|
1413 |
``json_t *`` when used with these functions.
|
|
1414 |
|
|
1415 |
The following unpacking flags are available:
|
|
1416 |
|
|
1417 |
``JSON_STRICT``
|
|
1418 |
Enable the extra validation step checking that all object and
|
|
1419 |
array items are unpacked. This is equivalent to appending the
|
|
1420 |
format specifier ``!`` to the end of every array and object in the
|
|
1421 |
format string.
|
|
1422 |
|
|
1423 |
``JSON_VALIDATE_ONLY``
|
|
1424 |
Don't extract any data, just validate the JSON value against the
|
|
1425 |
given format string. Note that object keys must still be specified
|
|
1426 |
after the format string.
|
|
1427 |
|
|
1428 |
Examples::
|
|
1429 |
|
|
1430 |
/* root is the JSON integer 42 */
|
|
1431 |
int myint;
|
|
1432 |
json_unpack(root, "i", &myint);
|
|
1433 |
assert(myint == 42);
|
|
1434 |
|
|
1435 |
/* root is the JSON object {"foo": "bar", "quux": true} */
|
|
1436 |
const char *str;
|
|
1437 |
int boolean;
|
|
1438 |
json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
|
|
1439 |
assert(strcmp(str, "bar") == 0 && boolean == 1);
|
|
1440 |
|
|
1441 |
/* root is the JSON array [[1, 2], {"baz": null} */
|
|
1442 |
json_error_t error;
|
|
1443 |
json_unpack_ex(root, &error, JSON_VALIDATE_ONLY, "[[i,i], {s:n}]", "baz");
|
|
1444 |
/* returns 0 for validation success, nothing is extracted */
|
|
1445 |
|
|
1446 |
/* root is the JSON array [1, 2, 3, 4, 5] */
|
|
1447 |
int myint1, myint2;
|
|
1448 |
json_unpack(root, "[ii!]", &myint1, &myint2);
|
|
1449 |
/* returns -1 for failed validation */
|
|
1450 |
|
|
1451 |
/* root is an empty JSON object */
|
|
1452 |
int myint = 0, myint2 = 0;
|
|
1453 |
json_unpack(root, "{s?i, s?[ii]}",
|
|
1454 |
"foo", &myint1,
|
|
1455 |
"bar", &myint2, &myint3);
|
|
1456 |
/* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */
|
|
1457 |
|
|
1458 |
|
|
1459 |
Equality
|
|
1460 |
========
|
|
1461 |
|
|
1462 |
Testing for equality of two JSON values cannot, in general, be
|
|
1463 |
achieved using the ``==`` operator. Equality in the terms of the
|
|
1464 |
``==`` operator states that the two :type:`json_t` pointers point to
|
|
1465 |
exactly the same JSON value. However, two JSON values can be equal not
|
|
1466 |
only if they are exactly the same value, but also if they have equal
|
|
1467 |
"contents":
|
|
1468 |
|
|
1469 |
* Two integer or real values are equal if their contained numeric
|
|
1470 |
values are equal. An integer value is never equal to a real value,
|
|
1471 |
though.
|
|
1472 |
|
|
1473 |
* Two strings are equal if their contained UTF-8 strings are equal,
|
|
1474 |
byte by byte. Unicode comparison algorithms are not implemented.
|
|
1475 |
|
|
1476 |
* Two arrays are equal if they have the same number of elements and
|
|
1477 |
each element in the first array is equal to the corresponding
|
|
1478 |
element in the second array.
|
|
1479 |
|
|
1480 |
* Two objects are equal if they have exactly the same keys and the
|
|
1481 |
value for each key in the first object is equal to the value of the
|
|
1482 |
corresponding key in the second object.
|
|
1483 |
|
|
1484 |
* Two true, false or null values have no "contents", so they are equal
|
|
1485 |
if their types are equal. (Because these values are singletons,
|
|
1486 |
their equality can actually be tested with ``==``.)
|
|
1487 |
|
|
1488 |
The following function can be used to test whether two JSON values are
|
|
1489 |
equal.
|
|
1490 |
|
|
1491 |
.. function:: int json_equal(json_t *value1, json_t *value2)
|
|
1492 |
|
|
1493 |
Returns 1 if *value1* and *value2* are equal, as defined above.
|
|
1494 |
Returns 0 if they are inequal or one or both of the pointers are
|
|
1495 |
*NULL*.
|
|
1496 |
|
|
1497 |
|
|
1498 |
Copying
|
|
1499 |
=======
|
|
1500 |
|
|
1501 |
Because of reference counting, passing JSON values around doesn't
|
|
1502 |
require copying them. But sometimes a fresh copy of a JSON value is
|
|
1503 |
needed. For example, if you need to modify an array, but still want to
|
|
1504 |
use the original afterwards, you should take a copy of it first.
|
|
1505 |
|
|
1506 |
Jansson supports two kinds of copying: shallow and deep. There is a
|
|
1507 |
difference between these methods only for arrays and objects. Shallow
|
|
1508 |
copying only copies the first level value (array or object) and uses
|
|
1509 |
the same child values in the copied value. Deep copying makes a fresh
|
|
1510 |
copy of the child values, too. Moreover, all the child values are deep
|
|
1511 |
copied in a recursive fashion.
|
|
1512 |
|
|
1513 |
.. function:: json_t *json_copy(json_t *value)
|
|
1514 |
|
|
1515 |
.. refcounting:: new
|
|
1516 |
|
|
1517 |
Returns a shallow copy of *value*, or *NULL* on error.
|
|
1518 |
|
|
1519 |
.. function:: json_t *json_deep_copy(const json_t *value)
|
|
1520 |
|
|
1521 |
.. refcounting:: new
|
|
1522 |
|
|
1523 |
Returns a deep copy of *value*, or *NULL* on error.
|
|
1524 |
|
|
1525 |
|
|
1526 |
.. _apiref-custom-memory-allocation:
|
|
1527 |
|
|
1528 |
Custom Memory Allocation
|
|
1529 |
========================
|
|
1530 |
|
|
1531 |
By default, Jansson uses :func:`malloc()` and :func:`free()` for
|
|
1532 |
memory allocation. These functions can be overridden if custom
|
|
1533 |
behavior is needed.
|
|
1534 |
|
|
1535 |
.. type:: json_malloc_t
|
|
1536 |
|
|
1537 |
A typedef for a function pointer with :func:`malloc()`'s
|
|
1538 |
signature::
|
|
1539 |
|
|
1540 |
typedef void *(*json_malloc_t)(size_t);
|
|
1541 |
|
|
1542 |
.. type:: json_free_t
|
|
1543 |
|
|
1544 |
A typedef for a function pointer with :func:`free()`'s
|
|
1545 |
signature::
|
|
1546 |
|
|
1547 |
typedef void (*json_free_t)(void *);
|
|
1548 |
|
|
1549 |
.. function:: void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
|
|
1550 |
|
|
1551 |
Use *malloc_fn* instead of :func:`malloc()` and *free_fn* instead
|
|
1552 |
of :func:`free()`. This function has to be called before any other
|
|
1553 |
Jansson's API functions to ensure that all memory operations use
|
|
1554 |
the same functions.
|
|
1555 |
|
|
1556 |
**Examples:**
|
|
1557 |
|
|
1558 |
Circumvent problems with different CRT heaps on Windows by using
|
|
1559 |
application's :func:`malloc()` and :func:`free()`::
|
|
1560 |
|
|
1561 |
json_set_alloc_funcs(malloc, free);
|
|
1562 |
|
|
1563 |
Use the `Boehm's conservative garbage collector`_ for memory
|
|
1564 |
operations::
|
|
1565 |
|
|
1566 |
json_set_alloc_funcs(GC_malloc, GC_free);
|
|
1567 |
|
|
1568 |
.. _Boehm's conservative garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/
|
|
1569 |
|
|
1570 |
Allow storing sensitive data (e.g. passwords or encryption keys) in
|
|
1571 |
JSON structures by zeroing all memory when freed::
|
|
1572 |
|
|
1573 |
static void *secure_malloc(size_t size)
|
|
1574 |
{
|
|
1575 |
/* Store the memory area size in the beginning of the block */
|
|
1576 |
void *ptr = malloc(size + 8);
|
|
1577 |
*((size_t *)ptr) = size;
|
|
1578 |
return ptr + 8;
|
|
1579 |
}
|
|
1580 |
|
|
1581 |
static void secure_free(void *ptr)
|
|
1582 |
{
|
|
1583 |
size_t size;
|
|
1584 |
|
|
1585 |
ptr -= 8;
|
|
1586 |
size = *((size_t *)ptr);
|
|
1587 |
|
|
1588 |
guaranteed_memset(ptr, 0, size + 8);
|
|
1589 |
free(ptr);
|
|
1590 |
}
|
|
1591 |
|
|
1592 |
int main()
|
|
1593 |
{
|
|
1594 |
json_set_alloc_funcs(secure_malloc, secure_free);
|
|
1595 |
/* ... */
|
|
1596 |
}
|
|
1597 |
|
|
1598 |
For more information about the issues of storing sensitive data in
|
|
1599 |
memory, see
|
|
1600 |
http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html.
|
|
1601 |
The page also explains the :func:`guaranteed_memset()` function used
|
|
1602 |
in the example and gives a sample implementation for it.
|