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