components/jansson/doc/html/_sources/apiref.txt
author Tomas Heran <tomas.heran@oracle.com>
Fri, 17 Apr 2015 01:30:52 -0700
branchs11-update
changeset 4141 c6a303a2f8c5
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:
4141
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     1
.. _apiref:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     2
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     3
*************
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     4
API Reference
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     5
*************
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     7
.. highlight:: c
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     8
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
     9
Preliminaries
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    10
=============
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    11
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    13
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    14
::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    15
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    16
   #include <jansson.h>
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    17
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    18
in each source file.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    19
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    20
All constants are prefixed with ``JSON_`` (except for those describing
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    21
the library version, prefixed with ``JANSSON_``). Other identifiers
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    24
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    25
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    26
Library Version
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    27
===============
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    28
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    32
version string is just *A.B*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    33
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    35
functionality, the micro version is incremented. When new features are
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    38
incompatible changes, the major version is incremented and others are
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    39
set to zero.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    40
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    41
The following preprocessor constants specify the current version of
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    42
the library:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    43
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    44
``JANSSON_MAJOR_VERSION``, ``JANSSON_MINOR_VERSION``, ``JANSSON_MICRO_VERSION``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    45
  Integers specifying the major, minor and micro versions,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    46
  respectively.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    47
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    48
``JANSSON_VERSION``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    50
  ``"1.3"``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    51
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    52
``JANSSON_VERSION_HEX``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    53
  A 3-byte hexadecimal representation of the version, e.g.
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    55
  This is useful in numeric comparisions, e.g.::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    56
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    57
      #if JANSSON_VERSION_HEX >= 0x010300
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    58
      /* Code specific to version 1.3 and above */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    59
      #endif
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    60
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    61
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    62
Value Representation
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    63
====================
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    64
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    65
The JSON specification (:rfc:`4627`) defines the following data types:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    66
*object*, *array*, *string*, *number*, *boolean*, and *null*. JSON
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    71
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    72
.. type:: json_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    73
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    77
  value.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    78
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    81
for constructing and manipulating values of different types.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    82
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    87
also cause errors.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    88
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    89
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    90
Type
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    91
----
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    92
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    94
functions:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    95
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    96
.. type:: enum json_type
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    97
c6a303a2f8c5 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:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
    99
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   100
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   101
   | ``JSON_OBJECT``    |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   102
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   103
   | ``JSON_ARRAY``     |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   104
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   105
   | ``JSON_STRING``    |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   106
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   107
   | ``JSON_INTEGER``   |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   108
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   109
   | ``JSON_REAL``      |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   110
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   111
   | ``JSON_TRUE``      |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   112
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   113
   | ``JSON_FALSE``     |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   114
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   115
   | ``JSON_NULL``      |
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   116
   +--------------------+
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   117
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   118
   These correspond to JSON object, array, string, number, boolean and
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   122
   value of the type ``JSON_FALSE``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   123
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   124
.. function:: int json_typeof(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   125
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   128
   implemented as a macro for speed.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   129
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   130
.. function:: json_is_object(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   131
               json_is_array(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   132
               json_is_string(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   133
               json_is_integer(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   134
               json_is_real(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   135
               json_is_true(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   136
               json_is_false(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   137
               json_is_null(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   138
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   139
   These functions (actually macros) return true (non-zero) for values
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   141
   for *NULL*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   142
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   143
.. function:: json_is_number(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   144
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   145
   Returns true for values of types ``JSON_INTEGER`` and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   146
   ``JSON_REAL``, and false for other types and for *NULL*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   147
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   148
.. function:: json_is_boolean(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   149
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   151
   for values of other types and for *NULL*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   152
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   153
.. function:: json_boolean_value(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   154
c6a303a2f8c5 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``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   156
   and 0 otherwise.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   157
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   158
   .. versionadded:: 2.7
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   159
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   160
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   161
.. _apiref-reference-count:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   162
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   163
Reference Count
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   164
---------------
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   165
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   170
no longer needed, the reference count is decremented. When the
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   172
value can be destroyed.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   173
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   174
The following functions are used to manipulate the reference count.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   175
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   176
.. function:: json_t *json_incref(json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   177
c6a303a2f8c5 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*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   179
   Returns *json*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   180
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   181
.. function:: void json_decref(json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   182
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   185
   is destroyed and it can no longer be used.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   186
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   188
functions are said to return a **new reference**. Other functions
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   189
returning (existing) JSON values do not normally increase the
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   190
reference count. These functions are said to return a **borrowed
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   194
to release the reference.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   195
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   200
the argument right after calling the function. These functions are
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   201
suffixed with ``_new`` or have ``_new_`` somewhere in their name.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   202
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   204
an integer to it::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   205
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   206
  json_t *array, *integer;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   207
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   208
  array = json_array();
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   209
  integer = json_integer(42);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   210
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   211
  json_array_append(array, integer);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   212
  json_decref(integer);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   213
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   215
by calling :func:`json_decref()`. By using a reference stealing
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   216
function :func:`json_array_append_new()` instead of
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   217
:func:`json_array_append()`, the code becomes much simpler::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   218
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   219
  json_t *array = json_array();
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   220
  json_array_append_new(array, json_integer(42));
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   221
c6a303a2f8c5 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
c6a303a2f8c5 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()`
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   224
steals the reference when appending the value to the array.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   225
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   228
argument.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   229
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   230
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   231
Circular References
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   232
-------------------
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   233
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   235
directly or indirectly, inserted inside itself. The direct case is
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   236
simple::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   237
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   238
  json_t *obj = json_object();
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   239
  json_object_set(obj, "foo", obj);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   240
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   243
an error status. The indirect case is the dangerous one::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   244
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   245
  json_t *arr1 = json_array(), *arr2 = json_array();
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   246
  json_array_append(arr1, arr2);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   247
  json_array_append(arr2, arr1);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   248
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   252
the user to avoid them.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   253
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   258
functions will fail. The encoder detects circular references and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   259
returns an error status.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   260
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   261
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   262
True, False and Null
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   263
====================
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   264
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   265
These three values are implemented as singletons, so the returned
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   266
pointers won't change between invocations of these functions.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   267
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   268
.. function:: json_t *json_true(void)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   269
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   270
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   271
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   272
   Returns the JSON true value.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   273
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   274
.. function:: json_t *json_false(void)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   275
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   276
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   277
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   278
   Returns the JSON false value.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   279
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   280
.. function:: json_t *json_boolean(val)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   281
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   282
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   283
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   285
   This is a macro, and equivalent to ``val ? json_true() :
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   286
   json_false()``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   287
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   288
   .. versionadded:: 2.4
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   289
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   290
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   291
.. function:: json_t *json_null(void)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   292
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   293
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   294
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   295
   Returns the JSON null value.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   296
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   297
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   298
String
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   299
======
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   300
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   305
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   306
.. function:: json_t *json_string(const char *value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   307
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   308
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   309
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   311
   valid null terminated UTF-8 encoded Unicode string.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   312
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   314
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   315
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   316
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   318
   contain null characters or not be null terminated.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   319
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   320
.. function:: json_t *json_string_nocheck(const char *value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   321
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   322
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   323
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   327
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   329
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   330
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   331
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   332
   Like :func:`json_string_nocheck`, but with explicit length, so
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   333
   *value* may contain null characters or not be null terminated.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   334
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   335
.. function:: const char *json_string_value(const json_t *string)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   336
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   339
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   342
   its reference count has not dropped to zero.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   343
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   344
.. function:: size_t json_string_length(const json_t *string)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   345
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   347
   if *string* is not a JSON string.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   348
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   350
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   353
   error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   354
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   356
c6a303a2f8c5 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*
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   358
   may contain null characters or not be null terminated.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   359
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   361
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   365
   means).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   366
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   368
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   369
   Like :func:`json_string_set_nocheck`, but with explicit length,
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   371
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   372
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   373
Number
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   374
======
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   375
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   377
programming language has distinct types for integer and floating-point
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   380
information, see :ref:`rfc-conformance`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   381
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   382
.. type:: json_int_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   383
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   385
   represents the widest integer type available on your system. In
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   387
   supports it, otherwise ``long``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   388
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   389
   Usually, you can safely use plain ``int`` in place of
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   392
   should use ``json_int_t`` explicitly.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   393
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   394
``JSON_INTEGER_IS_LONG_LONG``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   397
   can be used as follows::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   398
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   399
       #if JSON_INTEGER_IS_LONG_LONG
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   400
       /* Code specific for long long */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   401
       #else
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   402
       /* Code specific for long */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   403
       #endif
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   404
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   405
``JSON_INTEGER_FORMAT``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   407
   specifier that corresponds to :type:`json_int_t`, without the
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   408
   leading ``%`` sign, i.e. either ``"lld"`` or ``"ld"``. This macro
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   410
   either ``long`` or ``long long``, and :func:`printf()` reuiqres
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   411
   different length modifiers for the two.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   412
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   413
   Example::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   414
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   415
       json_int_t x = 123123123;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   416
       printf("x is %" JSON_INTEGER_FORMAT "\n", x);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   417
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   418
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   419
.. function:: json_t *json_integer(json_int_t value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   420
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   421
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   422
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   423
   Returns a new JSON integer, or *NULL* on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   424
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   426
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   428
   JSON integer.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   429
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   431
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   433
   success and -1 if *integer* is not a JSON integer.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   434
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   435
.. function:: json_t *json_real(double value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   436
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   437
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   438
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   439
   Returns a new JSON real, or *NULL* on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   440
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   441
.. function:: double json_real_value(const json_t *real)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   442
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   444
   JSON real.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   445
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   447
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   449
   success and -1 if *real* is not a JSON real.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   450
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   452
for integers and reals:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   453
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   454
.. function:: double json_number_value(const json_t *json)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   455
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   458
   neither JSON real nor JSON integer, 0.0 is returned.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   459
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   460
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   461
Array
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   462
=====
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   463
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   465
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   466
.. function:: json_t *json_array(void)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   467
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   468
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   469
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   471
   is empty.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   472
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   473
.. function:: size_t json_array_size(const json_t *array)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   474
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   476
   or not a JSON array.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   477
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   479
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   480
   .. refcounting:: borrow
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   481
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   483
   for *index* is from 0 to the return value of
c6a303a2f8c5 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,
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   486
   returned.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   487
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   489
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   490
   Replaces the element in *array* at position *index* with *value*.
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   493
   error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   494
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   496
c6a303a2f8c5 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*.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   499
   the call.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   500
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   502
c6a303a2f8c5 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*
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   504
   by 1. Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   505
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   507
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   508
   Like :func:`json_array_append()` but steals the reference to
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   510
   after the call.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   511
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   513
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   514
   Inserts *value* to *array* at position *index*, shifting the
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   516
   the array. Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   517
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   519
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   520
   Like :func:`json_array_insert()` but steals the reference to
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   522
   after the call.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   523
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   525
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   526
   Removes the element in *array* at position *index*, shifting the
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   529
   removed value is decremented.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   530
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   531
.. function:: int json_array_clear(json_t *array)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   532
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   534
   error. The reference count of all removed values are decremented.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   535
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   537
c6a303a2f8c5 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*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   539
   Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   540
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   542
in an array.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   543
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   544
.. function:: json_array_foreach(array, index, value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   545
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   546
   Iterate over every element of ``array``, running the block
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   548
   variables ``index`` and ``value``, of types :type:`size_t` and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   549
   :type:`json_t *` respectively. Example::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   550
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   551
       /* array is a JSON array */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   552
       size_t index;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   553
       json_t *value;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   554
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   555
       json_array_foreach(array, index, value) {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   556
           /* block of code that uses index and value */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   557
       }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   558
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   559
   The items are returned in increasing index order.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   560
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   561
   This macro expands to an ordinary ``for`` statement upon
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   562
   preprocessing, so its performance is equivalent to that of
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   563
   hand-written code using the array access functions.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   564
   The main advantage of this macro is that it abstracts
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   565
   away the complexity, and makes for shorter, more
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   566
   concise code.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   567
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   568
   .. versionadded:: 2.5
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   569
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   570
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   571
Object
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   572
======
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   573
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   575
Unicode string and the value is any JSON value.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   576
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   578
allowed in object keys.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   579
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   580
.. function:: json_t *json_object(void)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   581
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   582
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   583
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   585
   object is empty.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   586
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   587
.. function:: size_t json_object_size(const json_t *object)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   588
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   590
   a JSON object.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   591
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   593
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   594
   .. refcounting:: borrow
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   595
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   597
   *key* is not found and on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   598
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   600
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   602
   valid null terminated UTF-8 encoded Unicode string. If there
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   604
   Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   605
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   607
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   611
   means).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   612
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   614
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   615
   Like :func:`json_object_set()` but steals the reference to
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   617
   after the call.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   618
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   620
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   624
   means).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   625
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   627
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   630
   is decremented.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   631
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   632
.. function:: int json_object_clear(json_t *object)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   633
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   636
   values are decremented.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   637
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   639
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   640
   Update *object* with the key-value pairs from *other*, overwriting
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   641
   existing keys. Returns 0 on success or -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   642
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   644
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   647
   -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   648
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   649
   .. versionadded:: 2.3
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   650
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   652
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   655
   or -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   656
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   657
   .. versionadded:: 2.3
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   658
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   660
in an object.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   661
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   662
.. function:: json_object_foreach(object, key, value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   663
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   666
   variables ``key`` and ``value``, of types :type:`const char *` and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   667
   :type:`json_t *` respectively. Example::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   668
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   669
       /* obj is a JSON object */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   670
       const char *key;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   671
       json_t *value;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   672
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   673
       json_object_foreach(obj, key, value) {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   674
           /* block of code that uses key and value */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   675
       }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   676
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   677
   The items are not returned in any particular order.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   678
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   679
   This macro expands to an ordinary ``for`` statement upon
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   680
   preprocessing, so its performance is equivalent to that of
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   681
   hand-written iteration code using the object iteration protocol
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   683
   away the complexity behind iteration, and makes for shorter, more
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   684
   concise code.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   685
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   686
   .. versionadded:: 2.3
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   687
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   688
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   689
The following functions implement an iteration protocol for objects,
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   692
sorting due to the internal hashtable implementation.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   693
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   694
.. function:: void *json_object_iter(json_t *object)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   695
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   698
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   700
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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*
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   706
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   708
c6a303a2f8c5 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*
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   711
   through.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   712
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   713
.. function:: const char *json_object_iter_key(void *iter)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   714
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   715
   Extract the associated key from *iter*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   716
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   717
.. function:: json_t *json_object_iter_value(void *iter)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   718
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   719
   .. refcounting:: borrow
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   720
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   721
   Extract the associated value from *iter*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   722
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   724
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   726
   by *iter*, to *value*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   727
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   729
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   732
   after the call.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   733
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   734
.. function:: void *json_object_key_to_iter(const char *key)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   735
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   738
   will lead to segfaults. This function is used internally to
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   739
   implement :func:`json_object_foreach`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   740
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   741
   .. versionadded:: 2.3
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   742
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   743
The iteration protocol can be used for example as follows::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   744
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   745
   /* obj is a JSON object */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   746
   const char *key;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   747
   json_t *value;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   748
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   749
   void *iter = json_object_iter(obj);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   750
   while(iter)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   751
   {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   752
       key = json_object_iter_key(iter);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   753
       value = json_object_iter_value(iter);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   754
       /* use key and value ... */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   755
       iter = json_object_iter_next(obj, iter);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   756
   }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   757
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   758
.. function:: void json_object_seed(size_t seed)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   759
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   760
    Seed the hash function used in Jansson's hashtable implementation.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   762
    attacker cannot control its output.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   763
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   766
    entropy sources are available, falls back to using a combination
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   767
    of the current timestamp (with microsecond precision if possible)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   768
    and the process ID.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   769
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   771
    :func:`json_object()`, either explicit or implicit. If this
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   773
    :func:`json_object()` (either explicit or implicit) seeds the hash
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   774
    function. See :ref:`portability-thread-safety` for notes on thread
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   775
    safety.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   776
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   778
    function can be "unrandomized" by calling :func:`json_object_seed`
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   779
    with a constant value on program startup, e.g.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   780
    ``json_object_seed(1)``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   781
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   782
    .. versionadded:: 2.6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   783
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   784
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   785
Error reporting
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   786
===============
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   787
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   789
user. See sections :ref:`apiref-decoding`, :ref:`apiref-pack` and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   790
:ref:`apiref-unpack` for functions that pass error information using
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   791
this struct.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   792
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   793
.. type:: json_error_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   794
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   795
   .. member:: char text[]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   796
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   798
      not available.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   799
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   800
   .. member:: char source[]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   801
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   803
      special identifier in angle brackers (e.g. ``<string>``).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   804
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   805
   .. member:: int line
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   806
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   807
      The line number on which the error occurred.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   808
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   809
   .. member:: int column
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   810
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   813
      character counts as one column.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   814
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   815
   .. member:: size_t position
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   816
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   818
      useful for debugging Unicode encoding problems.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   819
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   821
and pass a pointer to a function. Example::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   822
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   823
   int main() {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   824
       json_t *json;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   825
       json_error_t error;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   826
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   827
       json = json_load_file("/path/to/file.json", 0, &error);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   828
       if(!json) {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   829
           /* the error variable contains error information */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   830
       }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   831
       ...
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   832
   }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   833
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   835
example), the contents of ``error`` are generally left unspecified.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   836
The decoding functions write to the ``position`` member also on
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   837
success. See :ref:`apiref-decoding` for more info.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   838
c6a303a2f8c5 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,
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   841
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   842
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   843
Encoding
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   844
========
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   845
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   850
below).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   851
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   854
altered by using the ``JSON_INDENT`` and ``JSON_COMPACT`` flags
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   856
JSON data.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   857
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   860
can be ORed together to obtain *flags*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   861
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   862
``JSON_INDENT(n)``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   863
   Pretty-print the result, using newlines between array and object
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   867
   inserted between array and object items.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   868
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   869
   The ``JSON_MAX_INDENT`` constant defines the maximum indentation
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   870
   that can be used, and its value is 31.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   871
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   872
   .. versionchanged:: 2.7
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   873
      Added ``JSON_MAX_INDENT``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   874
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   875
``JSON_COMPACT``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   877
   between array and object items to ``","`` and between object keys
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   878
   and values to ``":"``. Without this flag, the corresponding
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   879
   separators are ``", "`` and ``": "`` for more readable output.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   880
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   881
``JSON_ENSURE_ASCII``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   883
   ASCII characters. This is achived by escaping all Unicode
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   884
   characters outside the ASCII range.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   885
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   886
``JSON_SORT_KEYS``
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   889
   compared.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   890
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   891
``JSON_PRESERVE_ORDER``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   895
   preserves the order of object keys.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   896
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   897
``JSON_ENCODE_ANY``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   900
   *root* value to the encoding functions.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   901
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   903
   it's generally discouraged as it violates strict compatiblity with
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   905
   with other JSON systems.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   906
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   907
   .. versionadded:: 2.1
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   908
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   909
``JSON_ESCAPE_SLASH``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   910
   Escape the ``/`` characters in strings with ``\/``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   911
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   912
   .. versionadded:: 2.4
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   913
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   914
``JSON_REAL_PRECISION(n)``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   917
   values result in an undefined behavior.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   918
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   920
   all IEEE 754 double precision floating point numbers.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   921
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   922
   .. versionadded:: 2.7
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   923
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   924
The following functions perform the actual JSON encoding. The result
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   925
is in UTF-8.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   926
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   928
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   931
   by the caller using :func:`free()`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   932
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   934
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   935
   Write the JSON representation of *root* to the stream *output*.
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   939
   valid JSON.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   940
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   942
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   944
   *path* already exists, it is overwritten. *flags* is described
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   945
   above. Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   946
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   947
.. type:: json_dump_callback_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   948
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   949
   A typedef for a function that's called by
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   950
   :func:`json_dump_callback()`::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   951
c6a303a2f8c5 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);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   953
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   955
   the length of the buffer, and *data* is the corresponding
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   956
   :func:`json_dump_callback()` argument passed through.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   957
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   959
   process. On success, it should return 0.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   960
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   961
   .. versionadded:: 2.2
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   962
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   964
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   965
   Call *callback* repeatedly, passing a chunk of the JSON
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   966
   representation of *root* each time. *flags* is described above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   967
   Returns 0 on success and -1 on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   968
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   969
   .. versionadded:: 2.2
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   970
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   971
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   972
.. _apiref-decoding:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   973
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   974
Decoding
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   975
========
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   976
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   978
text to the Jansson representation of JSON data. The JSON
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   983
the ``JSON_DECODE_ANY`` flag (see below).
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   984
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   986
to the JSON specification. It explains many design decisions that
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   987
affect especially the behavior of the decoder.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   988
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   991
macros can be ORed together to obtain *flags*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   992
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   993
``JSON_REJECT_DUPLICATES``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   997
   checked byte-by-byte, without special Unicode comparison
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   998
   algorithms.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
   999
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1000
   .. versionadded:: 2.1
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1001
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1002
``JSON_DECODE_ANY``
c6a303a2f8c5 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.
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1005
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1007
   it's generally discouraged as it violates strict compatiblity with
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1009
   with other JSON systems.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1010
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1011
   .. versionadded:: 2.3
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1012
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1013
``JSON_DISABLE_EOF_CHECK``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1018
   extra data after the JSON text.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1019
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1020
   Normally, reading will stop when the last ``]`` or ``}`` in the
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1024
   ``4true`` correctly decodes the integer 4, but also reads the
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1025
   ``t``. For this reason, if reading multiple consecutive values that
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1027
   whitespace character.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1028
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1029
   .. versionadded:: 2.1
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1030
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1031
``JSON_DECODE_INT_AS_REAL``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1032
   JSON defines only one number type. Jansson distinguishes between
c6a303a2f8c5 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`.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1035
   values. Integers that do not have an exact double representation
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1037
   a double overflow will cause an error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1038
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1039
   .. versionadded:: 2.5
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1040
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1041
``JSON_ALLOW_NUL``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1042
   Allow ``\u0000`` escape inside string values. This is a safety
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1045
   bytes inside strings unless you explicitly create themselves by
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1047
   :func:`json_pack()`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1048
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1050
   used.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1051
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1052
   .. versionadded:: 2.6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1053
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1057
its ``position`` field. This is especially useful when using
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1058
``JSON_DISABLE_EOF_CHECK`` to read multiple consecutive JSON texts.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1059
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1060
.. versionadded:: 2.3
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1062
   of the :type:`json_error_t` structure.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1063
c6a303a2f8c5 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*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1065
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1066
The following functions perform the actual JSON decoding.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1067
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1069
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1070
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1071
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1074
   information about the error. *flags* is described above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1075
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1077
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1078
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1079
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1080
   Decodes the JSON string *buffer*, whose length is *buflen*, and
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1084
   need to be null-terminated. *flags* is described above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1085
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1086
   .. versionadded:: 2.1
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1087
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1089
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1090
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1091
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1094
   filled with information about the error. *flags* is described
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1095
   above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1096
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1097
   This function will start reading the input from whatever position
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1099
   occurs, the file position will be left indeterminate. On success,
c6a303a2f8c5 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``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1102
   character after the last ``]`` or ``}`` in the JSON input. This
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1103
   allows calling :func:`json_loadf()` on the same ``FILE`` object
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1104
   multiple times, if the input consists of consecutive JSON texts,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1105
   possibly separated by whitespace.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1106
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1108
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1109
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1110
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1113
   filled with information about the error. *flags* is described
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1114
   above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1115
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1116
.. type:: json_load_callback_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1117
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1118
   A typedef for a function that's called by
c6a303a2f8c5 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::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1120
c6a303a2f8c5 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);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1122
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1124
   corresponding :func:`json_load_callback()` argument passed through.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1125
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1129
   ``(size_t)-1`` to abort the decoding process.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1130
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1131
   .. versionadded:: 2.4
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1132
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1134
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1135
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1136
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1139
   which case *error* is filled with information about the error.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1141
   described above.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1142
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1143
   .. versionadded:: 2.4
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1144
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1145
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1146
.. _apiref-pack:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1147
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1148
Building Values
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1149
===============
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1150
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1151
This section describes functions that help to create, or *pack*,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1152
complex JSON values, especially nested objects and arrays. Value
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1154
functions about the expected arguments.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1155
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1156
For example, the format string ``"i"`` specifies a single integer
c6a303a2f8c5 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,
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1159
items::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1160
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1161
    /* Create the JSON integer 42 */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1162
    json_pack("i", 42);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1163
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1164
    /* Create the JSON array ["foo", "bar", true] */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1165
    json_pack("[ssb]", "foo", "bar", 1);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1166
c6a303a2f8c5 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
c6a303a2f8c5 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)
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1170
arguments.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1171
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1172
``s`` (string) [const char \*]
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1174
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1175
``s#`` (string) [const char \*, int]
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1177
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1178
    .. versionadded:: 2.5
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1179
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1180
``s%`` (string) [const char \*, size_t]
c6a303a2f8c5 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`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1182
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1183
    .. versionadded:: 2.6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1184
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1185
``+`` [const char \*]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1186
    Like ``s``, but concatenate to the previous string. Only valid
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1187
    after ``s``, ``s#``, ``+`` or ``+#``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1188
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1189
    .. versionadded:: 2.5
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1190
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1191
``+#`` [const char \*, int]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1192
    Like ``s#``, but concatenate to the previous string. Only valid
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1193
    after ``s``, ``s#``, ``+`` or ``+#``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1194
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1195
    .. versionadded:: 2.5
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1196
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1197
``+%`` (string) [const char \*, size_t]
c6a303a2f8c5 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`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1199
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1200
    .. versionadded:: 2.6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1201
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1202
``n`` (null)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1203
    Output a JSON null value. No argument is consumed.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1204
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1205
``b`` (boolean) [int]
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1207
    to ``false`` and non-zero to ``true``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1208
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1209
``i`` (integer) [int]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1210
    Convert a C :type:`int` to JSON integer.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1211
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1212
``I`` (integer) [json_int_t]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1213
    Convert a C :type:`json_int_t` to JSON integer.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1214
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1215
``f`` (real) [double]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1216
    Convert a C :type:`double` to JSON real.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1217
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1218
``o`` (any value) [json_t \*]
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1221
    stolen by the container.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1222
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1223
``O`` (any value) [json_t \*]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1224
    Like ``o``, but the argument's reference count is incremented.
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1227
    yourself.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1228
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1229
``[fmt]`` (array)
c6a303a2f8c5 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``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1232
    supported.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1233
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1234
``{fmt}`` (object)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1235
    Build an object with contents from the inner format string
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1236
    ``fmt``. The first, third, etc. format specifier represent a key,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1237
    and must be a string (see ``s``, ``s#``, ``+`` and ``+#`` above),
c6a303a2f8c5 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
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1240
    i.e. recursive value building is supported.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1241
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1242
Whitespace, ``:`` and ``,`` are ignored.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1243
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1244
The following functions compose the value building API:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1245
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1246
.. function:: json_t *json_pack(const char *fmt, ...)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1247
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1248
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1249
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1251
   each format specifier (except for ``{}[]n``), one or more arguments
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1252
   are consumed and used to build the corresponding value. Returns
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1253
   *NULL* on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1254
c6a303a2f8c5 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, ...)
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1257
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1258
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1259
c6a303a2f8c5 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
c6a303a2f8c5 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*
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1262
   parameter is currently unused and should be set to 0.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1263
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1266
   useful for debugging format strings.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1267
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1268
More examples::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1269
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1270
  /* Build an empty JSON object */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1271
  json_pack("{}");
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1272
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1273
  /* Build the JSON object {"foo": 42, "bar": 7} */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1274
  json_pack("{sisi}", "foo", 42, "bar", 7);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1275
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1276
  /* Like above, ':', ',' and whitespace are ignored */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1277
  json_pack("{s:i, s:i}", "foo", 42, "bar", 7);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1278
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1279
  /* Build the JSON array [[1, 2], {"cool": true}] */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1280
  json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1281
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1282
  /* Build a string from a non-NUL terminated buffer */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1283
  char buffer[4] = {'t', 'e', 's', 't'};
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1284
  json_pack("s#", buffer, 4);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1285
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1286
  /* Concatentate strings together to build the JSON string "foobarbaz" */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1287
  json_pack("s++", "foo", "bar", "baz");
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1288
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1289
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1290
.. _apiref-unpack:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1291
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1292
Parsing and Validating Values
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1293
=============================
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1294
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1295
This section describes functions that help to validate complex values
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1297
<apiref-pack>`, this is also based on format strings.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1298
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1304
using the flag ``JSON_STRICT``. See below for details.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1305
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1308
type whose address should be passed.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1309
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1310
``s`` (string) [const char \*]
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1312
    string. The resulting string is extracted by using
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1314
    there are still references to the corresponding JSON string.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1315
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1316
``s%`` (string) [const char \*, size_t \*]
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1318
    string and its length.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1319
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1320
    .. versionadded:: 2.6
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1321
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1322
``n`` (null)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1323
    Expect a JSON null value. Nothing is extracted.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1324
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1325
``b`` (boolean) [int]
c6a303a2f8c5 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``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1327
    is converted to 1 and ``false`` to 0.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1328
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1329
``i`` (integer) [int]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1330
    Convert a JSON integer to C :type:`int`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1331
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1332
``I`` (integer) [json_int_t]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1333
    Convert a JSON integer to C :type:`json_int_t`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1334
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1335
``f`` (real) [double]
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1336
    Convert a JSON real to C :type:`double`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1337
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1338
``F`` (integer or real) [double]
c6a303a2f8c5 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`.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1340
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1341
``o`` (any value) [json_t \*]
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1343
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1344
``O`` (any value) [json_t \*]
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1346
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1347
``[fmt]`` (array)
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1349
    string. ``fmt`` may contain objects and arrays, i.e. recursive
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1350
    value extraction is supporetd.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1351
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1352
``{fmt}`` (object)
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1354
    string ``fmt``. The first, third, etc. format specifier represent
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1358
    given as the corresponding argument. **Note** that every other
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1359
    argument is read from and every other is written to.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1360
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1362
    value extraction is supporetd.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1363
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1364
    .. versionadded:: 2.3
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1367
       extracted. See below for an example.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1368
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1369
``!``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1374
    use the ``JSON_STRICT`` unpacking flag.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1375
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1376
``*``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1377
    This special format specifier is the opposite of ``!``. If the
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1381
    or brace.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1382
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1383
Whitespace, ``:`` and ``,`` are ignored.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1384
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1385
The following functions compose the parsing and validation API:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1386
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1387
.. function:: int json_unpack(json_t *root, const char *fmt, ...)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1388
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1390
   string *fmt*. Returns 0 on success and -1 on failure.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1391
c6a303a2f8c5 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, ...)
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1394
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1399
   success and -1 on failure.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1400
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1401
.. note::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1402
c6a303a2f8c5 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``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1405
   specifier causes the reference count of ``root``, or some value
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1406
   reachable from ``root``, to be increased. Furthermore, the ``o``
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1408
   modifying the structure or contents of a value reachable from
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1409
   ``root``.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1410
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1413
   ``json_t *`` when used with these functions.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1414
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1415
The following unpacking flags are available:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1416
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1417
``JSON_STRICT``
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1418
    Enable the extra validation step checking that all object and
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1419
    array items are unpacked. This is equivalent to appending the
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1421
    format string.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1422
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1423
``JSON_VALIDATE_ONLY``
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1426
    after the format string.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1427
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1428
Examples::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1429
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1430
    /* root is the JSON integer 42 */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1431
    int myint;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1432
    json_unpack(root, "i", &myint);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1433
    assert(myint == 42);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1434
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1435
    /* root is the JSON object {"foo": "bar", "quux": true} */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1436
    const char *str;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1437
    int boolean;
c6a303a2f8c5 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);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1439
    assert(strcmp(str, "bar") == 0 && boolean == 1);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1440
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1441
    /* root is the JSON array [[1, 2], {"baz": null} */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1442
    json_error_t error;
c6a303a2f8c5 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");
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1444
    /* returns 0 for validation success, nothing is extracted */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1445
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1446
    /* root is the JSON array [1, 2, 3, 4, 5] */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1447
    int myint1, myint2;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1448
    json_unpack(root, "[ii!]", &myint1, &myint2);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1449
    /* returns -1 for failed validation */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1450
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1451
    /* root is an empty JSON object */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1452
    int myint = 0, myint2 = 0;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1453
    json_unpack(root, "{s?i, s?[ii]}",
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1454
                "foo", &myint1,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1455
                "bar", &myint2, &myint3);
c6a303a2f8c5 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 */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1457
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1458
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1459
Equality
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1460
========
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1461
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1463
achieved using the ``==`` operator. Equality in the terms of the
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1467
"contents":
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1468
c6a303a2f8c5 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
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1471
  though.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1472
c6a303a2f8c5 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,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1474
  byte by byte. Unicode comparison algorithms are not implemented.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1475
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1478
  element in the second array.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1479
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1482
  corresponding key in the second object.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1483
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1485
  if their types are equal. (Because these values are singletons,
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1486
  their equality can actually be tested with ``==``.)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1487
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1489
equal.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1490
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1491
.. function:: int json_equal(json_t *value1, json_t *value2)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1492
c6a303a2f8c5 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.
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1495
   *NULL*.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1496
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1497
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1498
Copying
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1499
=======
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1500
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1501
Because of reference counting, passing JSON values around doesn't
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1505
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1507
difference between these methods only for arrays and objects. Shallow
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1511
copied in a recursive fashion.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1512
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1513
.. function:: json_t *json_copy(json_t *value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1514
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1515
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1516
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1517
   Returns a shallow copy of *value*, or *NULL* on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1518
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1519
.. function:: json_t *json_deep_copy(const json_t *value)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1520
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1521
   .. refcounting:: new
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1522
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1523
   Returns a deep copy of *value*, or *NULL* on error.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1524
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1525
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1526
.. _apiref-custom-memory-allocation:
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1527
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1528
Custom Memory Allocation
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1529
========================
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1530
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1531
By default, Jansson uses :func:`malloc()` and :func:`free()` for
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1532
memory allocation. These functions can be overridden if custom
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1533
behavior is needed.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1534
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1535
.. type:: json_malloc_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1536
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1537
   A typedef for a function pointer with :func:`malloc()`'s
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1538
   signature::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1539
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1540
       typedef void *(*json_malloc_t)(size_t);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1541
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1542
.. type:: json_free_t
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1543
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1544
   A typedef for a function pointer with :func:`free()`'s
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1545
   signature::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1546
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1547
       typedef void (*json_free_t)(void *);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1548
c6a303a2f8c5 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)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1550
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1554
   the same functions.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1555
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1556
**Examples:**
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1557
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1558
Circumvent problems with different CRT heaps on Windows by using
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1559
application's :func:`malloc()` and :func:`free()`::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1560
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1561
    json_set_alloc_funcs(malloc, free);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1562
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1563
Use the `Boehm's conservative garbage collector`_ for memory
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1564
operations::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1565
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1566
    json_set_alloc_funcs(GC_malloc, GC_free);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1567
c6a303a2f8c5 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/
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1569
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1571
JSON structures by zeroing all memory when freed::
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1572
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1573
    static void *secure_malloc(size_t size)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1574
    {
c6a303a2f8c5 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 */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1576
        void *ptr = malloc(size + 8);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1577
        *((size_t *)ptr) = size;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1578
        return ptr + 8;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1579
    }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1580
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1581
    static void secure_free(void *ptr)
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1582
    {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1583
        size_t size;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1584
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1585
        ptr -= 8;
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1586
        size = *((size_t *)ptr);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1587
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1588
        guaranteed_memset(ptr, 0, size + 8);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1589
        free(ptr);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1590
    }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1591
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1592
    int main()
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1593
    {
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1594
        json_set_alloc_funcs(secure_malloc, secure_free);
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1595
        /* ... */
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1596
    }
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1597
c6a303a2f8c5 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
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1599
memory, see
c6a303a2f8c5 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.
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1601
The page also explains the :func:`guaranteed_memset()` function used
c6a303a2f8c5 PSARC/2014/362 Jansson
Tomas Heran <tomas.heran@oracle.com>
parents:
diff changeset
  1602
in the example and gives a sample implementation for it.