The following preprocessor constants specify the current version of
the library:
-``JANSSON_VERSION_MAJOR``, ``JANSSON_VERSION_MINOR``, ``JANSSON_VERSION_MICRO``
+``JANSSON_MAJOR_VERSION``, ``JANSSON_MINOR_VERSION``, ``JANSSON_MICRO_VERSION``
Integers specifying the major, minor and micro versions,
respectively.
True, False and Null
====================
-These values are implemented as singletons, so each of these functions
-returns the same value each time.
+These three values are implemented as singletons, so the returned
+pointers won't change between invocations of these functions.
.. function:: json_t *json_true(void)
Returns the JSON false value.
+.. function:: json_t *json_boolean(val)
+
+ .. refcounting:: new
+
+ Returns JSON false if ``val`` is zero, and JSON true otherwise.
+ This is a macro, and equivalent to ``val ? json_true() :
+ json_false()``.
+
+ .. versionadded:: 2.4
+
+
.. function:: json_t *json_null(void)
.. refcounting:: new
.. versionadded:: 2.1
+``JSON_ESCAPE_SLASH``
+ Escape the ``/`` characters in strings with ``\/``.
+
+ .. versionadded:: 2.4
+
The following functions perform the actual JSON encoding. The result
is in UTF-8.
filled with information about the error. *flags* is described
above.
+.. type:: json_load_callback_t
+
+ A typedef for a function that's called by
+ :func:`json_load_callback()` to read a chunk of input data::
+
+ typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
+
+ *buffer* points to a buffer of *buflen* bytes, and *data* is the
+ corresponding :func:`json_load_callback()` argument passed through.
+
+ On error, the function should return ``(size_t)-1`` to abort the
+ decoding process. When there's no data left, it should return 0 to
+ report that the end of input has been reached.
+
+ .. versionadded:: 2.4
+
+.. function:: json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error)
+
+ .. refcounting:: new
+
+ Decodes the JSON text produced by repeated calls to *callback*, and
+ returns the array or object it contains, or *NULL* on error, in
+ which case *error* is filled with information about the error.
+ *data* is passed through to *callback* on each call. *flags* is
+ described above.
+
+ .. versionadded:: 2.4
+
.. _apiref-pack:
fourth, etc. format character represent a value. Any value may be
an object or array, i.e. recursive value building is supported.
+Whitespace, ``:`` and ``,`` are ignored.
+
The following functions compose the value building API:
.. function:: json_t *json_pack(const char *fmt, ...)
json_pack("{s:i, s:i}", "foo", 42, "bar", 7);
/* Build the JSON array [[1, 2], {"cool": true}] */
- json_pack("[[i,i],{s:b]]", 1, 2, "cool", 1);
+ json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
.. _apiref-unpack:
``fmt`` may contain objects and arrays as values, i.e. recursive
value extraction is supporetd.
+ .. versionadded:: 2.3
+ Any ``s`` representing a key may be suffixed with a ``?`` to
+ make the key optional. If the key is not found, nothing is
+ extracted. See below for an example.
+
``!``
This special format character is used to enable the check that
all object and array items are accessed, on a per-value basis. It
or object as the last format character before the closing bracket
or brace.
+Whitespace, ``:`` and ``,`` are ignored.
+
The following functions compose the parsing and validation API:
.. function:: int json_unpack(json_t *root, const char *fmt, ...)
modifying the structure or contents of a value reachable from
``root``.
- If the ``O`` and ``o`` format character are not used, it's
+ If the ``O`` and ``o`` format characters are not used, it's
perfectly safe to cast a ``const json_t *`` variable to plain
``json_t *`` when used with these functions.
json_unpack(root, "[ii!]", &myint1, &myint2);
/* returns -1 for failed validation */
+ /* root is an empty JSON object */
+ int myint = 0, myint2 = 0;
+ json_unpack(root, "{s?i, s?[ii]}",
+ "foo", &myint1,
+ "bar", &myint2, &myint3);
+ /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */
+
Equality
========