3421N/A<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3421N/A "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3421N/A
3421N/A
3421N/A<html xmlns="http://www.w3.org/1999/xhtml">
3421N/A <head>
3421N/A <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3421N/A
3421N/A <title>API Reference &mdash; Jansson 2.7 documentation</title>
3421N/A
3421N/A <link rel="stylesheet" href="_static/default.css" type="text/css" />
3421N/A <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
3421N/A
3421N/A <script type="text/javascript">
3421N/A var DOCUMENTATION_OPTIONS = {
3421N/A URL_ROOT: './',
3421N/A VERSION: '2.7',
3421N/A COLLAPSE_INDEX: false,
3421N/A FILE_SUFFIX: '.html',
3421N/A HAS_SOURCE: true
3421N/A };
3421N/A </script>
3421N/A <script type="text/javascript" src="_static/jquery.js"></script>
3421N/A <script type="text/javascript" src="_static/underscore.js"></script>
3421N/A <script type="text/javascript" src="_static/doctools.js"></script>
3421N/A <link rel="top" title="Jansson 2.7 documentation" href="index.html" />
3421N/A <link rel="next" title="Changes in Jansson" href="changes.html" />
3421N/A <link rel="prev" title="Portability" href="portability.html" />
3421N/A </head>
3421N/A <body>
3421N/A <div class="related">
3421N/A <h3>Navigation</h3>
3421N/A <ul>
3421N/A <li class="right" style="margin-right: 10px">
3421N/A <a href="genindex.html" title="General Index"
3421N/A accesskey="I">index</a></li>
3421N/A <li class="right" >
3421N/A <a href="changes.html" title="Changes in Jansson"
3421N/A accesskey="N">next</a> |</li>
3421N/A <li class="right" >
3421N/A <a href="portability.html" title="Portability"
3421N/A accesskey="P">previous</a> |</li>
3421N/A <li><a href="index.html">Jansson 2.7 documentation</a> &raquo;</li>
3421N/A </ul>
3421N/A </div>
3421N/A
3421N/A <div class="document">
3421N/A <div class="documentwrapper">
3421N/A <div class="bodywrapper">
3421N/A <div class="body">
3421N/A
3421N/A <div class="section" id="api-reference">
3421N/A<span id="apiref"></span><h1>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h1>
3421N/A<div class="section" id="preliminaries">
3421N/A<h2>Preliminaries<a class="headerlink" href="#preliminaries" title="Permalink to this headline">¶</a></h2>
3421N/A<p>All declarations are in <tt class="file docutils literal"><span class="pre">jansson.h</span></tt>, so it&#8217;s enough to</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include &lt;jansson.h&gt;</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>in each source file.</p>
3421N/A<p>All constants are prefixed with <tt class="docutils literal"><span class="pre">JSON_</span></tt> (except for those describing
3421N/Athe library version, prefixed with <tt class="docutils literal"><span class="pre">JANSSON_</span></tt>). Other identifiers
3421N/Aare prefixed with <tt class="docutils literal"><span class="pre">json_</span></tt>. Type names are suffixed with <tt class="docutils literal"><span class="pre">_t</span></tt> and
3421N/A<tt class="docutils literal"><span class="pre">typedef</span></tt>&#8216;d so that the <tt class="docutils literal"><span class="pre">struct</span></tt> keyword need not be used.</p>
3421N/A</div>
3421N/A<div class="section" id="library-version">
3421N/A<h2>Library Version<a class="headerlink" href="#library-version" title="Permalink to this headline">¶</a></h2>
3421N/A<p>The Jansson version is of the form <em>A.B.C</em>, where <em>A</em> is the major
3421N/Aversion, <em>B</em> is the minor version and <em>C</em> is the micro version. If the
3421N/Amicro version is zero, it&#8217;s omitted from the version string, i.e. the
3421N/Aversion string is just <em>A.B</em>.</p>
3421N/A<p>When a new release only fixes bugs and doesn&#8217;t add new features or
3421N/Afunctionality, the micro version is incremented. When new features are
3421N/Aadded in a backwards compatible way, the minor version is incremented
3421N/Aand the micro version is set to zero. When there are backwards
3421N/Aincompatible changes, the major version is incremented and others are
3421N/Aset to zero.</p>
3421N/A<p>The following preprocessor constants specify the current version of
3421N/Athe library:</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">JANSSON_MAJOR_VERSION</span></tt>, <tt class="docutils literal"><span class="pre">JANSSON_MINOR_VERSION</span></tt>, <tt class="docutils literal"><span class="pre">JANSSON_MICRO_VERSION</span></tt></dt>
3421N/A<dd>Integers specifying the major, minor and micro versions,
3421N/Arespectively.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JANSSON_VERSION</span></tt></dt>
3421N/A<dd>A string representation of the current version, e.g. <tt class="docutils literal"><span class="pre">&quot;1.2.1&quot;</span></tt> or
3421N/A<tt class="docutils literal"><span class="pre">&quot;1.3&quot;</span></tt>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JANSSON_VERSION_HEX</span></tt></dt>
3421N/A<dd><p class="first">A 3-byte hexadecimal representation of the version, e.g.
3421N/A<tt class="docutils literal"><span class="pre">0x010201</span></tt> for version 1.2.1 and <tt class="docutils literal"><span class="pre">0x010300</span></tt> for version 1.3.
3421N/AThis is useful in numeric comparisions, e.g.:</p>
3421N/A<div class="last highlight-c"><div class="highlight"><pre><span class="cp">#if JANSSON_VERSION_HEX &gt;= 0x010300</span>
3421N/A<span class="cm">/* Code specific to version 1.3 and above */</span>
3421N/A<span class="cp">#endif</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</dd>
3421N/A</dl>
3421N/A</div>
3421N/A<div class="section" id="value-representation">
3421N/A<h2>Value Representation<a class="headerlink" href="#value-representation" title="Permalink to this headline">¶</a></h2>
3421N/A<p>The JSON specification (<span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>) defines the following data types:
3421N/A<em>object</em>, <em>array</em>, <em>string</em>, <em>number</em>, <em>boolean</em>, and <em>null</em>. JSON
3421N/Atypes are used dynamically; arrays and objects can hold any other data
3421N/Atype, including themselves. For this reason, Jansson&#8217;s type system is
3421N/Aalso dynamic in nature. There&#8217;s one C type to represent all JSON
3421N/Avalues, and this structure knows the type of the JSON value it holds.</p>
3421N/A<dl class="type">
3421N/A<dt id="c.json_t">
3421N/A<tt class="descname">json_t</tt><a class="headerlink" href="#c.json_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>This data structure is used throughout the library to represent all
3421N/AJSON values. It always contains the type of the JSON value it holds
3421N/Aand the value&#8217;s reference count. The rest depends on the type of the
3421N/Avalue.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p>Objects of <a class="reference internal" href="#c.json_t" title="json_t"><tt class="xref c c-type docutils literal"><span class="pre">json_t</span></tt></a> are always used through a pointer. There
3421N/Aare APIs for querying the type, manipulating the reference count, and
3421N/Afor constructing and manipulating values of different types.</p>
3421N/A<p>Unless noted otherwise, all API functions return an error value if an
3421N/Aerror occurs. Depending on the function&#8217;s signature, the error value
3421N/Ais either <em>NULL</em> or -1. Invalid arguments or invalid input are
3421N/Aapparent sources for errors. Memory allocation and I/O operations may
3421N/Aalso cause errors.</p>
3421N/A<div class="section" id="type">
3421N/A<h3>Type<a class="headerlink" href="#type" title="Permalink to this headline">¶</a></h3>
3421N/A<p>The type of a JSON value is queried and tested using the following
3421N/Afunctions:</p>
3421N/A<dl class="type">
3421N/A<dt id="c.json_type">
3421N/Aenum <tt class="descname">json_type</tt><a class="headerlink" href="#c.json_type" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>The type of a JSON value. The following members are defined:</p>
3421N/A<table border="1" class="docutils">
3421N/A<colgroup>
3421N/A<col width="100%" />
3421N/A</colgroup>
3421N/A<tbody valign="top">
3421N/A<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">JSON_OBJECT</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-even"><td><tt class="docutils literal"><span class="pre">JSON_ARRAY</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">JSON_STRING</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-even"><td><tt class="docutils literal"><span class="pre">JSON_INTEGER</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">JSON_REAL</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-even"><td><tt class="docutils literal"><span class="pre">JSON_TRUE</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">JSON_FALSE</span></tt></td>
3421N/A</tr>
3421N/A<tr class="row-even"><td><tt class="docutils literal"><span class="pre">JSON_NULL</span></tt></td>
3421N/A</tr>
3421N/A</tbody>
3421N/A</table>
3421N/A<p>These correspond to JSON object, array, string, number, boolean and
3421N/Anull. A number is represented by either a value of the type
3421N/A<tt class="docutils literal"><span class="pre">JSON_INTEGER</span></tt> or of the type <tt class="docutils literal"><span class="pre">JSON_REAL</span></tt>. A true boolean value
3421N/Ais represented by a value of the type <tt class="docutils literal"><span class="pre">JSON_TRUE</span></tt> and false by a
3421N/Avalue of the type <tt class="docutils literal"><span class="pre">JSON_FALSE</span></tt>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_typeof">
3421N/Aint <tt class="descname">json_typeof</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_typeof" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Return the type of the JSON value (a <a class="reference internal" href="#c.json_type" title="json_type"><tt class="xref c c-type docutils literal"><span class="pre">json_type</span></tt></a> cast to
3421N/A<tt class="xref c c-type docutils literal"><span class="pre">int</span></tt>). <em>json</em> MUST NOT be <em>NULL</em>. This function is actually
3421N/Aimplemented as a macro for speed.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_is_object">
3421N/A<tt class="descname">json_is_object</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_object" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_array">
3421N/A<tt class="descname">json_is_array</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_array" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_string">
3421N/A<tt class="descname">json_is_string</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_string" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_integer">
3421N/A<tt class="descname">json_is_integer</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_integer" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_real">
3421N/A<tt class="descname">json_is_real</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_real" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_true">
3421N/A<tt class="descname">json_is_true</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_true" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_false">
3421N/A<tt class="descname">json_is_false</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_false" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_is_null">
3421N/A<tt class="descname">json_is_null</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_null" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>These functions (actually macros) return true (non-zero) for values
3421N/Aof the given type, and false (zero) for values of other types and
3421N/Afor <em>NULL</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_is_number">
3421N/A<tt class="descname">json_is_number</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_number" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns true for values of types <tt class="docutils literal"><span class="pre">JSON_INTEGER</span></tt> and
3421N/A<tt class="docutils literal"><span class="pre">JSON_REAL</span></tt>, and false for other types and for <em>NULL</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_is_boolean">
3421N/A<tt class="descname">json_is_boolean</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_is_boolean" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns true for types <tt class="docutils literal"><span class="pre">JSON_TRUE</span></tt> and <tt class="docutils literal"><span class="pre">JSON_FALSE</span></tt>, and false
3421N/Afor values of other types and for <em>NULL</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_boolean_value">
3421N/A<tt class="descname">json_boolean_value</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_boolean_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Alias of <a class="reference internal" href="#c.json_is_true" title="json_is_true"><tt class="xref c c-func docutils literal"><span class="pre">json_is_true()</span></tt></a>, i.e. returns 1 for <tt class="docutils literal"><span class="pre">JSON_TRUE</span></tt>
3421N/Aand 0 otherwise.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.7.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="reference-count">
3421N/A<span id="apiref-reference-count"></span><h3>Reference Count<a class="headerlink" href="#reference-count" title="Permalink to this headline">¶</a></h3>
3421N/A<p>The reference count is used to track whether a value is still in use
3421N/Aor not. When a value is created, it&#8217;s reference count is set to 1. If
3421N/Aa reference to a value is kept (e.g. a value is stored somewhere for
3421N/Alater use), its reference count is incremented, and when the value is
3421N/Ano longer needed, the reference count is decremented. When the
3421N/Areference count drops to zero, there are no references left, and the
3421N/Avalue can be destroyed.</p>
3421N/A<p>The following functions are used to manipulate the reference count.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_incref">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_incref</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_incref" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Increment the reference count of <em>json</em> if it&#8217;s not <em>NULL</em>.
3421N/AReturns <em>json</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_decref">
3421N/Avoid <tt class="descname">json_decref</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_decref" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Decrement the reference count of <em>json</em>. As soon as a call to
3421N/A<a class="reference internal" href="#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a> drops the reference count to zero, the value
3421N/Ais destroyed and it can no longer be used.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p>Functions creating new JSON values set the reference count to 1. These
3421N/Afunctions are said to return a <strong>new reference</strong>. Other functions
3421N/Areturning (existing) JSON values do not normally increase the
3421N/Areference count. These functions are said to return a <strong>borrowed
3421N/Areference</strong>. So, if the user will hold a reference to a value returned
3421N/Aas a borrowed reference, he must call <a class="reference internal" href="#c.json_incref" title="json_incref"><tt class="xref c c-func docutils literal"><span class="pre">json_incref()</span></tt></a>. As soon as
3421N/Athe value is no longer needed, <a class="reference internal" href="#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a> should be called
3421N/Ato release the reference.</p>
3421N/A<p>Normally, all functions accepting a JSON value as an argument will
3421N/Amanage the reference, i.e. increase and decrease the reference count
3421N/Aas needed. However, some functions <strong>steal</strong> the reference, i.e. they
3421N/Ahave the same result as if the user called <a class="reference internal" href="#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a> on
3421N/Athe argument right after calling the function. These functions are
3421N/Asuffixed with <tt class="docutils literal"><span class="pre">_new</span></tt> or have <tt class="docutils literal"><span class="pre">_new_</span></tt> somewhere in their name.</p>
3421N/A<p>For example, the following code creates a new JSON array and appends
3421N/Aan integer to it:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">json_t</span> <span class="o">*</span><span class="n">array</span><span class="p">,</span> <span class="o">*</span><span class="n">integer</span><span class="p">;</span>
3421N/A
3421N/A<span class="n">array</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
3421N/A<span class="n">integer</span> <span class="o">=</span> <span class="n">json_integer</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span>
3421N/A
3421N/A<span class="n">json_array_append</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">integer</span><span class="p">);</span>
3421N/A<span class="n">json_decref</span><span class="p">(</span><span class="n">integer</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Note how the caller has to release the reference to the integer value
3421N/Aby calling <a class="reference internal" href="#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a>. By using a reference stealing
3421N/Afunction <a class="reference internal" href="#c.json_array_append_new" title="json_array_append_new"><tt class="xref c c-func docutils literal"><span class="pre">json_array_append_new()</span></tt></a> instead of
3421N/A<a class="reference internal" href="#c.json_array_append" title="json_array_append"><tt class="xref c c-func docutils literal"><span class="pre">json_array_append()</span></tt></a>, the code becomes much simpler:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">json_t</span> <span class="o">*</span><span class="n">array</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
3421N/A<span class="n">json_array_append_new</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">json_integer</span><span class="p">(</span><span class="mi">42</span><span class="p">));</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>In this case, the user doesn&#8217;t have to explicitly release the
3421N/Areference to the integer value, as <a class="reference internal" href="#c.json_array_append_new" title="json_array_append_new"><tt class="xref c c-func docutils literal"><span class="pre">json_array_append_new()</span></tt></a>
3421N/Asteals the reference when appending the value to the array.</p>
3421N/A<p>In the following sections it is clearly documented whether a function
3421N/Awill return a new or borrowed reference or steal a reference to its
3421N/Aargument.</p>
3421N/A</div>
3421N/A<div class="section" id="circular-references">
3421N/A<h3>Circular References<a class="headerlink" href="#circular-references" title="Permalink to this headline">¶</a></h3>
3421N/A<p>A circular reference is created when an object or an array is,
3421N/Adirectly or indirectly, inserted inside itself. The direct case is
3421N/Asimple:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">json_t</span> <span class="o">*</span><span class="n">obj</span> <span class="o">=</span> <span class="n">json_object</span><span class="p">();</span>
3421N/A<span class="n">json_object_set</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="n">obj</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Jansson will refuse to do this, and <a class="reference internal" href="#c.json_object_set" title="json_object_set"><tt class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></tt></a> (and
3421N/Aall the other such functions for objects and arrays) will return with
3421N/Aan error status. The indirect case is the dangerous one:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">json_t</span> <span class="o">*</span><span class="n">arr1</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">(),</span> <span class="o">*</span><span class="n">arr2</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
3421N/A<span class="n">json_array_append</span><span class="p">(</span><span class="n">arr1</span><span class="p">,</span> <span class="n">arr2</span><span class="p">);</span>
3421N/A<span class="n">json_array_append</span><span class="p">(</span><span class="n">arr2</span><span class="p">,</span> <span class="n">arr1</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>In this example, the array <tt class="docutils literal"><span class="pre">arr2</span></tt> is contained in the array
3421N/A<tt class="docutils literal"><span class="pre">arr1</span></tt>, and vice versa. Jansson cannot check for this kind of
3421N/Aindirect circular references without a performance hit, so it&#8217;s up to
3421N/Athe user to avoid them.</p>
3421N/A<p>If a circular reference is created, the memory consumed by the values
3421N/Acannot be freed by <a class="reference internal" href="#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a>. The reference counts never
3421N/Adrops to zero because the values are keeping the references to each
3421N/Aother. Moreover, trying to encode the values with any of the encoding
3421N/Afunctions will fail. The encoder detects circular references and
3421N/Areturns an error status.</p>
3421N/A</div>
3421N/A</div>
3421N/A<div class="section" id="true-false-and-null">
3421N/A<h2>True, False and Null<a class="headerlink" href="#true-false-and-null" title="Permalink to this headline">¶</a></h2>
3421N/A<p>These three values are implemented as singletons, so the returned
3421N/Apointers won&#8217;t change between invocations of these functions.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_true">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_true</tt><big>(</big>void<big>)</big><a class="headerlink" href="#c.json_true" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON true value.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_false">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_false</tt><big>(</big>void<big>)</big><a class="headerlink" href="#c.json_false" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON false value.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_boolean">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_boolean</tt><big>(</big>val<big>)</big><a class="headerlink" href="#c.json_boolean" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns JSON false if <tt class="docutils literal"><span class="pre">val</span></tt> is zero, and JSON true otherwise.
3421N/AThis is a macro, and equivalent to <tt class="docutils literal"><span class="pre">val</span> <span class="pre">?</span> <span class="pre">json_true()</span> <span class="pre">:</span>
3421N/A<span class="pre">json_false()</span></tt>.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.4.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_null">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_null</tt><big>(</big>void<big>)</big><a class="headerlink" href="#c.json_null" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON null value.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="string">
3421N/A<h2>String<a class="headerlink" href="#string" title="Permalink to this headline">¶</a></h2>
3421N/A<p>Jansson uses UTF-8 as the character encoding. All JSON strings must be
3421N/Avalid UTF-8 (or ASCII, as it&#8217;s a subset of UTF-8). All Unicode
3421N/Acodepoints U+0000 through U+10FFFF are allowed, but you must use
3421N/Alength-aware functions if you wish to embed NUL bytes in strings.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_string">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_string</tt><big>(</big>const char<em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_string" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON string, or <em>NULL</em> on error. <em>value</em> must be a
3421N/Avalid null terminated UTF-8 encoded Unicode string.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_stringn">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_stringn</tt><big>(</big>const char<em>&nbsp;*value</em>, size_t<em>&nbsp;len</em><big>)</big><a class="headerlink" href="#c.json_stringn" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string" title="json_string"><tt class="xref c c-func docutils literal"><span class="pre">json_string()</span></tt></a>, but with explicit length, so <em>value</em> may
3421N/Acontain null characters or not be null terminated.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_nocheck">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_string_nocheck</tt><big>(</big>const char<em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_string_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string" title="json_string"><tt class="xref c c-func docutils literal"><span class="pre">json_string()</span></tt></a>, but doesn&#8217;t check that <em>value</em> is valid
3421N/AUTF-8. Use this function only if you are certain that this really
3421N/Ais the case (e.g. you have already checked it by other means).</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_stringn_nocheck">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_stringn_nocheck</tt><big>(</big>const char<em>&nbsp;*value</em>, size_t<em>&nbsp;len</em><big>)</big><a class="headerlink" href="#c.json_stringn_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string_nocheck" title="json_string_nocheck"><tt class="xref c c-func docutils literal"><span class="pre">json_string_nocheck()</span></tt></a>, but with explicit length, so
3421N/A<em>value</em> may contain null characters or not be null terminated.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_value">
3421N/Aconst char *<tt class="descname">json_string_value</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em><big>)</big><a class="headerlink" href="#c.json_string_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the associated value of <em>string</em> as a null terminated UTF-8
3421N/Aencoded string, or <em>NULL</em> if <em>string</em> is not a JSON string.</p>
3421N/A<p>The retuned value is read-only and must not be modified or freed by
3421N/Athe user. It is valid as long as <em>string</em> exists, i.e. as long as
3421N/Aits reference count has not dropped to zero.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_length">
3421N/Asize_t <tt class="descname">json_string_length</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em><big>)</big><a class="headerlink" href="#c.json_string_length" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the length of <em>string</em> in its UTF-8 presentation, or zero
3421N/Aif <em>string</em> is not a JSON string.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_set">
3421N/Aint <tt class="descname">json_string_set</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em>, const char<em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_string_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Sets the associated value of <em>string</em> to <em>value</em>. <em>value</em> must be a
3421N/Avalid UTF-8 encoded Unicode string. Returns 0 on success and -1 on
3421N/Aerror.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_setn">
3421N/Aint <tt class="descname">json_string_setn</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em>, const char<em>&nbsp;*value</em>, size_t<em>&nbsp;len</em><big>)</big><a class="headerlink" href="#c.json_string_setn" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_string_set" title="json_string_set"><tt class="xref c c-func docutils literal"><span class="pre">json_string_set()</span></tt></a>, but with explicit length, so <em>value</em>
3421N/Amay contain null characters or not be null terminated.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_set_nocheck">
3421N/Aint <tt class="descname">json_string_set_nocheck</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em>, const char<em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_string_set_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_string_set" title="json_string_set"><tt class="xref c c-func docutils literal"><span class="pre">json_string_set()</span></tt></a>, but doesn&#8217;t check that <em>value</em> is
3421N/Avalid UTF-8. Use this function only if you are certain that this
3421N/Areally is the case (e.g. you have already checked it by other
3421N/Ameans).</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_string_setn_nocheck">
3421N/Aint <tt class="descname">json_string_setn_nocheck</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*string</em>, const char<em>&nbsp;*value</em>, size_t<em>&nbsp;len</em><big>)</big><a class="headerlink" href="#c.json_string_setn_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_string_set_nocheck" title="json_string_set_nocheck"><tt class="xref c c-func docutils literal"><span class="pre">json_string_set_nocheck()</span></tt></a>, but with explicit length,
3421N/Aso <em>value</em> may contain null characters or not be null terminated.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="number">
3421N/A<h2>Number<a class="headerlink" href="#number" title="Permalink to this headline">¶</a></h2>
3421N/A<p>The JSON specification only contains one numeric type, &#8220;number&#8221;. The C
3421N/Aprogramming language has distinct types for integer and floating-point
3421N/Anumbers, so for practical reasons Jansson also has distinct types for
3421N/Athe two. They are called &#8220;integer&#8221; and &#8220;real&#8221;, respectively. For more
3421N/Ainformation, see <a class="reference internal" href="conformance.html#rfc-conformance"><em>RFC Conformance</em></a>.</p>
3421N/A<dl class="type">
3421N/A<dt id="c.json_int_t">
3421N/A<tt class="descname">json_int_t</tt><a class="headerlink" href="#c.json_int_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>This is the C type that is used to store JSON integer values. It
3421N/Arepresents the widest integer type available on your system. In
3421N/Apractice it&#8217;s just a typedef of <tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt> if your compiler
3421N/Asupports it, otherwise <tt class="docutils literal"><span class="pre">long</span></tt>.</p>
3421N/A<p>Usually, you can safely use plain <tt class="docutils literal"><span class="pre">int</span></tt> in place of
3421N/A<tt class="docutils literal"><span class="pre">json_int_t</span></tt>, and the implicit C integer conversion handles the
3421N/Arest. Only when you know that you need the full 64-bit range, you
3421N/Ashould use <tt class="docutils literal"><span class="pre">json_int_t</span></tt> explicitly.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_INTEGER_IS_LONG_LONG</span></tt></dt>
3421N/A<dd><p class="first">This is a preprocessor variable that holds the value 1 if
3421N/A<a class="reference internal" href="#c.json_int_t" title="json_int_t"><tt class="xref c c-type docutils literal"><span class="pre">json_int_t</span></tt></a> is <tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt>, and 0 if it&#8217;s <tt class="docutils literal"><span class="pre">long</span></tt>. It
3421N/Acan be used as follows:</p>
3421N/A<div class="last highlight-c"><div class="highlight"><pre><span class="cp">#if JSON_INTEGER_IS_LONG_LONG</span>
3421N/A<span class="cm">/* Code specific for long long */</span>
3421N/A<span class="cp">#else</span>
3421N/A<span class="cm">/* Code specific for long */</span>
3421N/A<span class="cp">#endif</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_INTEGER_FORMAT</span></tt></dt>
3421N/A<dd><p class="first">This is a macro that expands to a <tt class="xref c c-func docutils literal"><span class="pre">printf()</span></tt> conversion
3421N/Aspecifier that corresponds to <a class="reference internal" href="#c.json_int_t" title="json_int_t"><tt class="xref c c-type docutils literal"><span class="pre">json_int_t</span></tt></a>, without the
3421N/Aleading <tt class="docutils literal"><span class="pre">%</span></tt> sign, i.e. either <tt class="docutils literal"><span class="pre">&quot;lld&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;ld&quot;</span></tt>. This macro
3421N/Ais required because the actual type of <a class="reference internal" href="#c.json_int_t" title="json_int_t"><tt class="xref c c-type docutils literal"><span class="pre">json_int_t</span></tt></a> can be
3421N/Aeither <tt class="docutils literal"><span class="pre">long</span></tt> or <tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt>, and <tt class="xref c c-func docutils literal"><span class="pre">printf()</span></tt> reuiqres
3421N/Adifferent length modifiers for the two.</p>
3421N/A<p>Example:</p>
3421N/A<div class="last highlight-c"><div class="highlight"><pre><span class="kt">json_int_t</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">123123123</span><span class="p">;</span>
3421N/A<span class="n">printf</span><span class="p">(</span><span class="s">&quot;x is %&quot;</span> <span class="n">JSON_INTEGER_FORMAT</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</dd>
3421N/A</dl>
3421N/A<dl class="function">
3421N/A<dt id="c.json_integer">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_integer</tt><big>(</big><a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a><em>&nbsp;value</em><big>)</big><a class="headerlink" href="#c.json_integer" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON integer, or <em>NULL</em> on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_integer_value">
3421N/A<a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a> <tt class="descname">json_integer_value</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*integer</em><big>)</big><a class="headerlink" href="#c.json_integer_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the associated value of <em>integer</em>, or 0 if <em>json</em> is not a
3421N/AJSON integer.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_integer_set">
3421N/Aint <tt class="descname">json_integer_set</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*integer</em>, <a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a><em>&nbsp;value</em><big>)</big><a class="headerlink" href="#c.json_integer_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Sets the associated value of <em>integer</em> to <em>value</em>. Returns 0 on
3421N/Asuccess and -1 if <em>integer</em> is not a JSON integer.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_real">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_real</tt><big>(</big>double<em>&nbsp;value</em><big>)</big><a class="headerlink" href="#c.json_real" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON real, or <em>NULL</em> on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_real_value">
3421N/Adouble <tt class="descname">json_real_value</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*real</em><big>)</big><a class="headerlink" href="#c.json_real_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the associated value of <em>real</em>, or 0.0 if <em>real</em> is not a
3421N/AJSON real.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_real_set">
3421N/Aint <tt class="descname">json_real_set</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*real</em>, double<em>&nbsp;value</em><big>)</big><a class="headerlink" href="#c.json_real_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Sets the associated value of <em>real</em> to <em>value</em>. Returns 0 on
3421N/Asuccess and -1 if <em>real</em> is not a JSON real.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p>In addition to the functions above, there&#8217;s a common query function
3421N/Afor integers and reals:</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_number_value">
3421N/Adouble <tt class="descname">json_number_value</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em><big>)</big><a class="headerlink" href="#c.json_number_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the associated value of the JSON integer or JSON real
3421N/A<em>json</em>, cast to double regardless of the actual type. If <em>json</em> is
3421N/Aneither JSON real nor JSON integer, 0.0 is returned.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="array">
3421N/A<h2>Array<a class="headerlink" href="#array" title="Permalink to this headline">¶</a></h2>
3421N/A<p>A JSON array is an ordered collection of other JSON values.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_array">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_array</tt><big>(</big>void<big>)</big><a class="headerlink" href="#c.json_array" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON array, or <em>NULL</em> on error. Initially, the array
3421N/Ais empty.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_size">
3421N/Asize_t <tt class="descname">json_array_size</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em><big>)</big><a class="headerlink" href="#c.json_array_size" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the number of elements in <em>array</em>, or 0 if <em>array</em> is NULL
3421N/Aor not a JSON array.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_get">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_array_get</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em><big>)</big><a class="headerlink" href="#c.json_array_get" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: Borrowed reference.</em><p>Returns the element in <em>array</em> at position <em>index</em>. The valid range
3421N/Afor <em>index</em> is from 0 to the return value of
3421N/A<a class="reference internal" href="#c.json_array_size" title="json_array_size"><tt class="xref c c-func docutils literal"><span class="pre">json_array_size()</span></tt></a> minus 1. If <em>array</em> is not a JSON array,
3421N/Aif <em>array</em> is <em>NULL</em>, or if <em>index</em> is out of range, <em>NULL</em> is
3421N/Areturned.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_set">
3421N/Aint <tt class="descname">json_array_set</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Replaces the element in <em>array</em> at position <em>index</em> with <em>value</em>.
3421N/AThe valid range for <em>index</em> is from 0 to the return value of
3421N/A<a class="reference internal" href="#c.json_array_size" title="json_array_size"><tt class="xref c c-func docutils literal"><span class="pre">json_array_size()</span></tt></a> minus 1. Returns 0 on success and -1 on
3421N/Aerror.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_set_new">
3421N/Aint <tt class="descname">json_array_set_new</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_set_new" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_array_set" title="json_array_set"><tt class="xref c c-func docutils literal"><span class="pre">json_array_set()</span></tt></a> but steals the reference to <em>value</em>.
3421N/AThis is useful when <em>value</em> is newly created and not used after
3421N/Athe call.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_append">
3421N/Aint <tt class="descname">json_array_append</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_append" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Appends <em>value</em> to the end of <em>array</em>, growing the size of <em>array</em>
3421N/Aby 1. Returns 0 on success and -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_append_new">
3421N/Aint <tt class="descname">json_array_append_new</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_append_new" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_array_append" title="json_array_append"><tt class="xref c c-func docutils literal"><span class="pre">json_array_append()</span></tt></a> but steals the reference to
3421N/A<em>value</em>. This is useful when <em>value</em> is newly created and not used
3421N/Aafter the call.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_insert">
3421N/Aint <tt class="descname">json_array_insert</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_insert" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Inserts <em>value</em> to <em>array</em> at position <em>index</em>, shifting the
3421N/Aelements at <em>index</em> and after it one position towards the end of
3421N/Athe array. Returns 0 on success and -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_insert_new">
3421N/Aint <tt class="descname">json_array_insert_new</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_array_insert_new" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_array_insert" title="json_array_insert"><tt class="xref c c-func docutils literal"><span class="pre">json_array_insert()</span></tt></a> but steals the reference to
3421N/A<em>value</em>. This is useful when <em>value</em> is newly created and not used
3421N/Aafter the call.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_remove">
3421N/Aint <tt class="descname">json_array_remove</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, size_t<em>&nbsp;index</em><big>)</big><a class="headerlink" href="#c.json_array_remove" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Removes the element in <em>array</em> at position <em>index</em>, shifting the
3421N/Aelements after <em>index</em> one position towards the start of the array.
3421N/AReturns 0 on success and -1 on error. The reference count of the
3421N/Aremoved value is decremented.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_clear">
3421N/Aint <tt class="descname">json_array_clear</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em><big>)</big><a class="headerlink" href="#c.json_array_clear" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Removes all elements from <em>array</em>. Returns 0 on sucess and -1 on
3421N/Aerror. The reference count of all removed values are decremented.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_extend">
3421N/Aint <tt class="descname">json_array_extend</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*other_array</em><big>)</big><a class="headerlink" href="#c.json_array_extend" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Appends all elements in <em>other_array</em> to the end of <em>array</em>.
3421N/AReturns 0 on success and -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p>The following macro can be used to iterate through all elements
3421N/Ain an array.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_array_foreach">
3421N/A<tt class="descname">json_array_foreach</tt><big>(</big>array, index, value<big>)</big><a class="headerlink" href="#c.json_array_foreach" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Iterate over every element of <tt class="docutils literal"><span class="pre">array</span></tt>, running the block
3421N/Aof code that follows each time with the proper values set to
3421N/Avariables <tt class="docutils literal"><span class="pre">index</span></tt> and <tt class="docutils literal"><span class="pre">value</span></tt>, of types <tt class="xref c c-type docutils literal"><span class="pre">size_t</span></tt> and
3421N/A<a class="reference internal" href="#c.json_t" title="json_t"><tt class="xref c c-type docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></tt></a> respectively. Example:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* array is a JSON array */</span>
3421N/A<span class="kt">size_t</span> <span class="n">index</span><span class="p">;</span>
3421N/A<span class="kt">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
3421N/A
3421N/A<span class="n">json_array_foreach</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
3421N/A <span class="cm">/* block of code that uses index and value */</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>The items are returned in increasing index order.</p>
3421N/A<p>This macro expands to an ordinary <tt class="docutils literal"><span class="pre">for</span></tt> statement upon
3421N/Apreprocessing, so its performance is equivalent to that of
3421N/Ahand-written code using the array access functions.
3421N/AThe main advantage of this macro is that it abstracts
3421N/Aaway the complexity, and makes for shorter, more
3421N/Aconcise code.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.5.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="object">
3421N/A<h2>Object<a class="headerlink" href="#object" title="Permalink to this headline">¶</a></h2>
3421N/A<p>A JSON object is a dictionary of key-value pairs, where the key is a
3421N/AUnicode string and the value is any JSON value.</p>
3421N/A<p>Even though NUL bytes are allowed in string values, they are not
3421N/Aallowed in object keys.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_object">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_object</tt><big>(</big>void<big>)</big><a class="headerlink" href="#c.json_object" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON object, or <em>NULL</em> on error. Initially, the
3421N/Aobject is empty.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_size">
3421N/Asize_t <tt class="descname">json_object_size</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em><big>)</big><a class="headerlink" href="#c.json_object_size" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the number of elements in <em>object</em>, or 0 if <em>object</em> is not
3421N/Aa JSON object.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_get">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_object_get</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em><big>)</big><a class="headerlink" href="#c.json_object_get" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: Borrowed reference.</em><p>Get a value corresponding to <em>key</em> from <em>object</em>. Returns <em>NULL</em> if
3421N/A<em>key</em> is not found and on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_set">
3421N/Aint <tt class="descname">json_object_set</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Set the value of <em>key</em> to <em>value</em> in <em>object</em>. <em>key</em> must be a
3421N/Avalid null terminated UTF-8 encoded Unicode string. If there
3421N/Aalready is a value for <em>key</em>, it is replaced by the new value.
3421N/AReturns 0 on success and -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_set_nocheck">
3421N/Aint <tt class="descname">json_object_set_nocheck</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_set_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_set" title="json_object_set"><tt class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></tt></a>, but doesn&#8217;t check that <em>key</em> is
3421N/Avalid UTF-8. Use this function only if you are certain that this
3421N/Areally is the case (e.g. you have already checked it by other
3421N/Ameans).</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_set_new">
3421N/Aint <tt class="descname">json_object_set_new</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_set_new" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_set" title="json_object_set"><tt class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></tt></a> but steals the reference to
3421N/A<em>value</em>. This is useful when <em>value</em> is newly created and not used
3421N/Aafter the call.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_set_new_nocheck">
3421N/Aint <tt class="descname">json_object_set_new_nocheck</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_set_new_nocheck" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_set_new" title="json_object_set_new"><tt class="xref c c-func docutils literal"><span class="pre">json_object_set_new()</span></tt></a>, but doesn&#8217;t check that <em>key</em> is
3421N/Avalid UTF-8. Use this function only if you are certain that this
3421N/Areally is the case (e.g. you have already checked it by other
3421N/Ameans).</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_del">
3421N/Aint <tt class="descname">json_object_del</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em><big>)</big><a class="headerlink" href="#c.json_object_del" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Delete <em>key</em> from <em>object</em> if it exists. Returns 0 on success, or
3421N/A-1 if <em>key</em> was not found. The reference count of the removed value
3421N/Ais decremented.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_clear">
3421N/Aint <tt class="descname">json_object_clear</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em><big>)</big><a class="headerlink" href="#c.json_object_clear" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Remove all elements from <em>object</em>. Returns 0 on success and -1 if
3421N/A<em>object</em> is not a JSON object. The reference count of all removed
3421N/Avalues are decremented.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_update">
3421N/Aint <tt class="descname">json_object_update</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*other</em><big>)</big><a class="headerlink" href="#c.json_object_update" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Update <em>object</em> with the key-value pairs from <em>other</em>, overwriting
3421N/Aexisting keys. Returns 0 on success or -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_update_existing">
3421N/Aint <tt class="descname">json_object_update_existing</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*other</em><big>)</big><a class="headerlink" href="#c.json_object_update_existing" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_update" title="json_object_update"><tt class="xref c c-func docutils literal"><span class="pre">json_object_update()</span></tt></a>, but only the values of existing
3421N/Akeys are updated. No new keys are created. Returns 0 on success or
3421N/A-1 on error.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_update_missing">
3421N/Aint <tt class="descname">json_object_update_missing</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*other</em><big>)</big><a class="headerlink" href="#c.json_object_update_missing" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_update" title="json_object_update"><tt class="xref c c-func docutils literal"><span class="pre">json_object_update()</span></tt></a>, but only new keys are created.
3421N/AThe value of any existing key is not changed. Returns 0 on success
3421N/Aor -1 on error.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<p>The following macro can be used to iterate through all key-value pairs
3421N/Ain an object.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_foreach">
3421N/A<tt class="descname">json_object_foreach</tt><big>(</big>object, key, value<big>)</big><a class="headerlink" href="#c.json_object_foreach" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Iterate over every key-value pair of <tt class="docutils literal"><span class="pre">object</span></tt>, running the block
3421N/Aof code that follows each time with the proper values set to
3421N/Avariables <tt class="docutils literal"><span class="pre">key</span></tt> and <tt class="docutils literal"><span class="pre">value</span></tt>, of types <tt class="xref c c-type docutils literal"><span class="pre">const</span> <span class="pre">char</span> <span class="pre">*</span></tt> and
3421N/A<a class="reference internal" href="#c.json_t" title="json_t"><tt class="xref c c-type docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></tt></a> respectively. Example:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* obj is a JSON object */</span>
3421N/A<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="p">;</span>
3421N/A<span class="kt">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
3421N/A
3421N/A<span class="n">json_object_foreach</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
3421N/A <span class="cm">/* block of code that uses key and value */</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>The items are not returned in any particular order.</p>
3421N/A<p>This macro expands to an ordinary <tt class="docutils literal"><span class="pre">for</span></tt> statement upon
3421N/Apreprocessing, so its performance is equivalent to that of
3421N/Ahand-written iteration code using the object iteration protocol
3421N/A(see below). The main advantage of this macro is that it abstracts
3421N/Aaway the complexity behind iteration, and makes for shorter, more
3421N/Aconcise code.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<p>The following functions implement an iteration protocol for objects,
3421N/Aallowing to iterate through all key-value pairs in an object. The
3421N/Aitems are not returned in any particular order, as this would require
3421N/Asorting due to the internal hashtable implementation.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter">
3421N/Avoid *<tt class="descname">json_object_iter</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em><big>)</big><a class="headerlink" href="#c.json_object_iter" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns an opaque iterator which can be used to iterate over all
3421N/Akey-value pairs in <em>object</em>, or <em>NULL</em> if <em>object</em> is empty.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_at">
3421N/Avoid *<tt class="descname">json_object_iter_at</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, const char<em>&nbsp;*key</em><big>)</big><a class="headerlink" href="#c.json_object_iter_at" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_iter" title="json_object_iter"><tt class="xref c c-func docutils literal"><span class="pre">json_object_iter()</span></tt></a>, but returns an iterator to the
3421N/Akey-value pair in <em>object</em> whose key is equal to <em>key</em>, or NULL if
3421N/A<em>key</em> is not found in <em>object</em>. Iterating forward to the end of
3421N/A<em>object</em> only yields all key-value pairs of the object if <em>key</em>
3421N/Ahappens to be the first key in the underlying hash table.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_next">
3421N/Avoid *<tt class="descname">json_object_iter_next</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, void<em>&nbsp;*iter</em><big>)</big><a class="headerlink" href="#c.json_object_iter_next" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns an iterator pointing to the next key-value pair in <em>object</em>
3421N/Aafter <em>iter</em>, or <em>NULL</em> if the whole object has been iterated
3421N/Athrough.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_key">
3421N/Aconst char *<tt class="descname">json_object_iter_key</tt><big>(</big>void<em>&nbsp;*iter</em><big>)</big><a class="headerlink" href="#c.json_object_iter_key" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Extract the associated key from <em>iter</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_value">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_object_iter_value</tt><big>(</big>void<em>&nbsp;*iter</em><big>)</big><a class="headerlink" href="#c.json_object_iter_value" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: Borrowed reference.</em><p>Extract the associated value from <em>iter</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_set">
3421N/Aint <tt class="descname">json_object_iter_set</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, void<em>&nbsp;*iter</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_iter_set" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Set the value of the key-value pair in <em>object</em>, that is pointed to
3421N/Aby <em>iter</em>, to <em>value</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_iter_set_new">
3421N/Aint <tt class="descname">json_object_iter_set_new</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*object</em>, void<em>&nbsp;*iter</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_object_iter_set_new" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_iter_set" title="json_object_iter_set"><tt class="xref c c-func docutils literal"><span class="pre">json_object_iter_set()</span></tt></a>, but steals the reference to
3421N/A<em>value</em>. This is useful when <em>value</em> is newly created and not used
3421N/Aafter the call.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_key_to_iter">
3421N/Avoid *<tt class="descname">json_object_key_to_iter</tt><big>(</big>const char<em>&nbsp;*key</em><big>)</big><a class="headerlink" href="#c.json_object_key_to_iter" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Like <a class="reference internal" href="#c.json_object_iter_at" title="json_object_iter_at"><tt class="xref c c-func docutils literal"><span class="pre">json_object_iter_at()</span></tt></a>, but much faster. Only works for
3421N/Avalues returned by <a class="reference internal" href="#c.json_object_iter_key" title="json_object_iter_key"><tt class="xref c c-func docutils literal"><span class="pre">json_object_iter_key()</span></tt></a>. Using other keys
3421N/Awill lead to segfaults. This function is used internally to
3421N/Aimplement <a class="reference internal" href="#c.json_object_foreach" title="json_object_foreach"><tt class="xref c c-func docutils literal"><span class="pre">json_object_foreach()</span></tt></a>.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<p>The iteration protocol can be used for example as follows:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* obj is a JSON object */</span>
3421N/A<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="p">;</span>
3421N/A<span class="kt">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
3421N/A
3421N/A<span class="kt">void</span> <span class="o">*</span><span class="n">iter</span> <span class="o">=</span> <span class="n">json_object_iter</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
3421N/A<span class="k">while</span><span class="p">(</span><span class="n">iter</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="n">key</span> <span class="o">=</span> <span class="n">json_object_iter_key</span><span class="p">(</span><span class="n">iter</span><span class="p">);</span>
3421N/A <span class="n">value</span> <span class="o">=</span> <span class="n">json_object_iter_value</span><span class="p">(</span><span class="n">iter</span><span class="p">);</span>
3421N/A <span class="cm">/* use key and value ... */</span>
3421N/A <span class="n">iter</span> <span class="o">=</span> <span class="n">json_object_iter_next</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">iter</span><span class="p">);</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<dl class="function">
3421N/A<dt id="c.json_object_seed">
3421N/Avoid <tt class="descname">json_object_seed</tt><big>(</big>size_t<em>&nbsp;seed</em><big>)</big><a class="headerlink" href="#c.json_object_seed" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Seed the hash function used in Jansson&#8217;s hashtable implementation.
3421N/AThe seed is used to randomize the hash function so that an
3421N/Aattacker cannot control its output.</p>
3421N/A<p>If <em>seed</em> is 0, Jansson generates the seed itselfy by reading
3421N/Arandom data from the operating system&#8217;s entropy sources. If no
3421N/Aentropy sources are available, falls back to using a combination
3421N/Aof the current timestamp (with microsecond precision if possible)
3421N/Aand the process ID.</p>
3421N/A<p>If called at all, this function must be called before any calls to
3421N/A<a class="reference internal" href="#c.json_object" title="json_object"><tt class="xref c c-func docutils literal"><span class="pre">json_object()</span></tt></a>, either explicit or implicit. If this
3421N/Afunction is not called by the user, the first call to
3421N/A<a class="reference internal" href="#c.json_object" title="json_object"><tt class="xref c c-func docutils literal"><span class="pre">json_object()</span></tt></a> (either explicit or implicit) seeds the hash
3421N/Afunction. See <a class="reference internal" href="portability.html#portability-thread-safety"><em>Thread safety</em></a> for notes on thread
3421N/Asafety.</p>
3421N/A<p>If repeatable results are required, for e.g. unit tests, the hash
3421N/Afunction can be &#8220;unrandomized&#8221; by calling <a class="reference internal" href="#c.json_object_seed" title="json_object_seed"><tt class="xref c c-func docutils literal"><span class="pre">json_object_seed()</span></tt></a>
3421N/Awith a constant value on program startup, e.g.
3421N/A<tt class="docutils literal"><span class="pre">json_object_seed(1)</span></tt>.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.6.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="error-reporting">
3421N/A<h2>Error reporting<a class="headerlink" href="#error-reporting" title="Permalink to this headline">¶</a></h2>
3421N/A<p>Jansson uses a single struct type to pass error information to the
3421N/Auser. See sections <a class="reference internal" href="#apiref-decoding"><em>Decoding</em></a>, <a class="reference internal" href="#apiref-pack"><em>Building Values</em></a> and
3421N/A<a class="reference internal" href="#apiref-unpack"><em>Parsing and Validating Values</em></a> for functions that pass error information using
3421N/Athis struct.</p>
3421N/A<dl class="type">
3421N/A<dt id="c.json_error_t">
3421N/A<tt class="descname">json_error_t</tt><a class="headerlink" href="#c.json_error_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><dl class="member">
3421N/A<dt>
3421N/A<tt class="descname">char text[]</tt></dt>
3421N/A<dd><p>The error message (in UTF-8), or an empty string if a message is
3421N/Anot available.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="member">
3421N/A<dt>
3421N/A<tt class="descname">char source[]</tt></dt>
3421N/A<dd><p>Source of the error. This can be (a part of) the file name or a
3421N/Aspecial identifier in angle brackers (e.g. <tt class="docutils literal"><span class="pre">&lt;string&gt;</span></tt>).</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="member">
3421N/A<dt id="c.json_error_t.line">
3421N/Aint <tt class="descname">line</tt><a class="headerlink" href="#c.json_error_t.line" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>The line number on which the error occurred.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="member">
3421N/A<dt id="c.json_error_t.column">
3421N/Aint <tt class="descname">column</tt><a class="headerlink" href="#c.json_error_t.column" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>The column on which the error occurred. Note that this is the
3421N/A<em>character column</em>, not the byte column, i.e. a multibyte UTF-8
3421N/Acharacter counts as one column.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="member">
3421N/A<dt id="c.json_error_t.position">
3421N/Asize_t <tt class="descname">position</tt><a class="headerlink" href="#c.json_error_t.position" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>The position in bytes from the start of the input. This is
3421N/Auseful for debugging Unicode encoding problems.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</dd></dl>
3421N/A
3421N/A<p>The normal use of <a class="reference internal" href="#c.json_error_t" title="json_error_t"><tt class="xref c c-type docutils literal"><span class="pre">json_error_t</span></tt></a> is to allocate it on the stack,
3421N/Aand pass a pointer to a function. Example:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
3421N/A <span class="kt">json_t</span> <span class="o">*</span><span class="n">json</span><span class="p">;</span>
3421N/A <span class="kt">json_error_t</span> <span class="n">error</span><span class="p">;</span>
3421N/A
3421N/A <span class="n">json</span> <span class="o">=</span> <span class="n">json_load_file</span><span class="p">(</span><span class="s">&quot;/path/to/file.json&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">error</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json</span><span class="p">)</span> <span class="p">{</span>
3421N/A <span class="cm">/* the error variable contains error information */</span>
3421N/A <span class="p">}</span>
3421N/A <span class="p">...</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Also note that if the call succeeded (<tt class="docutils literal"><span class="pre">json</span> <span class="pre">!=</span> <span class="pre">NULL</span></tt> in the above
3421N/Aexample), the contents of <tt class="docutils literal"><span class="pre">error</span></tt> are generally left unspecified.
3421N/AThe decoding functions write to the <tt class="docutils literal"><span class="pre">position</span></tt> member also on
3421N/Asuccess. See <a class="reference internal" href="#apiref-decoding"><em>Decoding</em></a> for more info.</p>
3421N/A<p>All functions also accept <em>NULL</em> as the <a class="reference internal" href="#c.json_error_t" title="json_error_t"><tt class="xref c c-type docutils literal"><span class="pre">json_error_t</span></tt></a> pointer,
3421N/Ain which case no error information is returned to the caller.</p>
3421N/A</div>
3421N/A<div class="section" id="encoding">
3421N/A<h2>Encoding<a class="headerlink" href="#encoding" title="Permalink to this headline">¶</a></h2>
3421N/A<p>This sections describes the functions that can be used to encode
3421N/Avalues to JSON. By default, only objects and arrays can be encoded
3421N/Adirectly, since they are the only valid <em>root</em> values of a JSON text.
3421N/ATo encode any JSON value, use the <tt class="docutils literal"><span class="pre">JSON_ENCODE_ANY</span></tt> flag (see
3421N/Abelow).</p>
3421N/A<p>By default, the output has no newlines, and spaces are used between
3421N/Aarray and object elements for a readable output. This behavior can be
3421N/Aaltered by using the <tt class="docutils literal"><span class="pre">JSON_INDENT</span></tt> and <tt class="docutils literal"><span class="pre">JSON_COMPACT</span></tt> flags
3421N/Adescribed below. A newline is never appended to the end of the encoded
3421N/AJSON data.</p>
3421N/A<p>Each function takes a <em>flags</em> parameter that controls some aspects of
3421N/Ahow the data is encoded. Its default value is 0. The following macros
3421N/Acan be ORed together to obtain <em>flags</em>.</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_INDENT(n)</span></tt></dt>
3421N/A<dd><p class="first">Pretty-print the result, using newlines between array and object
3421N/Aitems, and indenting with <em>n</em> spaces. The valid range for <em>n</em> is
3421N/Abetween 0 and 31 (inclusive), other values result in an undefined
3421N/Aoutput. If <tt class="docutils literal"><span class="pre">JSON_INDENT</span></tt> is not used or <em>n</em> is 0, no newlines are
3421N/Ainserted between array and object items.</p>
3421N/A<p>The <tt class="docutils literal"><span class="pre">JSON_MAX_INDENT</span></tt> constant defines the maximum indentation
3421N/Athat can be used, and its value is 31.</p>
3421N/A<div class="last versionchanged">
3421N/A<p><span class="versionmodified">Changed in version 2.7: </span>Added <tt class="docutils literal"><span class="pre">JSON_MAX_INDENT</span></tt>.</p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_COMPACT</span></tt></dt>
3421N/A<dd>This flag enables a compact representation, i.e. sets the separator
3421N/Abetween array and object items to <tt class="docutils literal"><span class="pre">&quot;,&quot;</span></tt> and between object keys
3421N/Aand values to <tt class="docutils literal"><span class="pre">&quot;:&quot;</span></tt>. Without this flag, the corresponding
3421N/Aseparators are <tt class="docutils literal"><span class="pre">&quot;,</span> <span class="pre">&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;:</span> <span class="pre">&quot;</span></tt> for more readable output.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_ENSURE_ASCII</span></tt></dt>
3421N/A<dd>If this flag is used, the output is guaranteed to consist only of
3421N/AASCII characters. This is achived by escaping all Unicode
3421N/Acharacters outside the ASCII range.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_SORT_KEYS</span></tt></dt>
3421N/A<dd>If this flag is used, all the objects in output are sorted by key.
3421N/AThis is useful e.g. if two JSON texts are diffed or visually
3421N/Acompared.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_PRESERVE_ORDER</span></tt></dt>
3421N/A<dd>If this flag is used, object keys in the output are sorted into the
3421N/Asame order in which they were first inserted to the object. For
3421N/Aexample, decoding a JSON text and then encoding with this flag
3421N/Apreserves the order of object keys.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_ENCODE_ANY</span></tt></dt>
3421N/A<dd><p class="first">Specifying this flag makes it possible to encode any JSON value on
3421N/Aits own. Without it, only objects and arrays can be passed as the
3421N/A<em>root</em> value to the encoding functions.</p>
3421N/A<p><strong>Note:</strong> Encoding any value may be useful in some scenarios, but
3421N/Ait&#8217;s generally discouraged as it violates strict compatiblity with
3421N/A<span class="target" id="index-1"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>. If you use this flag, don&#8217;t expect interoperatibility
3421N/Awith other JSON systems.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.1.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_ESCAPE_SLASH</span></tt></dt>
3421N/A<dd><p class="first">Escape the <tt class="docutils literal"><span class="pre">/</span></tt> characters in strings with <tt class="docutils literal"><span class="pre">\/</span></tt>.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.4.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_REAL_PRECISION(n)</span></tt></dt>
3421N/A<dd><p class="first">Output all real numbers with at most <em>n</em> digits of precision. The
3421N/Avalid range for <em>n</em> is between 0 and 31 (inclusive), and other
3421N/Avalues result in an undefined behavior.</p>
3421N/A<p>By default, the precision is 17, to correctly and losslessly encode
3421N/Aall IEEE 754 double precision floating point numbers.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.7.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A</dl>
3421N/A<p>The following functions perform the actual JSON encoding. The result
3421N/Ais in UTF-8.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_dumps">
3421N/Achar *<tt class="descname">json_dumps</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*root</em>, size_t<em>&nbsp;flags</em><big>)</big><a class="headerlink" href="#c.json_dumps" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns the JSON representation of <em>root</em> as a string, or <em>NULL</em> on
3421N/Aerror. <em>flags</em> is described above. The return value must be freed
3421N/Aby the caller using <tt class="xref c c-func docutils literal"><span class="pre">free()</span></tt>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_dumpf">
3421N/Aint <tt class="descname">json_dumpf</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*root</em>, FILE<em>&nbsp;*output</em>, size_t<em>&nbsp;flags</em><big>)</big><a class="headerlink" href="#c.json_dumpf" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Write the JSON representation of <em>root</em> to the stream <em>output</em>.
3421N/A<em>flags</em> is described above. Returns 0 on success and -1 on error.
3421N/AIf an error occurs, something may have already been written to
3421N/A<em>output</em>. In this case, the output is undefined and most likely not
3421N/Avalid JSON.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_dump_file">
3421N/Aint <tt class="descname">json_dump_file</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em>, const char<em>&nbsp;*path</em>, size_t<em>&nbsp;flags</em><big>)</big><a class="headerlink" href="#c.json_dump_file" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Write the JSON representation of <em>root</em> to the file <em>path</em>. If
3421N/A<em>path</em> already exists, it is overwritten. <em>flags</em> is described
3421N/Aabove. Returns 0 on success and -1 on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="type">
3421N/A<dt id="c.json_dump_callback_t">
3421N/A<tt class="descname">json_dump_callback_t</tt><a class="headerlink" href="#c.json_dump_callback_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>A typedef for a function that&#8217;s called by
3421N/A<a class="reference internal" href="#c.json_dump_callback" title="json_dump_callback"><tt class="xref c c-func docutils literal"><span class="pre">json_dump_callback()</span></tt></a>:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="nf">int</span> <span class="p">(</span><span class="o">*</span><span class="kt">json_dump_callback_t</span><span class="p">)(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">size</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p><em>buffer</em> points to a buffer containing a chunk of output, <em>size</em> is
3421N/Athe length of the buffer, and <em>data</em> is the corresponding
3421N/A<a class="reference internal" href="#c.json_dump_callback" title="json_dump_callback"><tt class="xref c c-func docutils literal"><span class="pre">json_dump_callback()</span></tt></a> argument passed through.</p>
3421N/A<p>On error, the function should return -1 to stop the encoding
3421N/Aprocess. On success, it should return 0.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.2.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_dump_callback">
3421N/Aint <tt class="descname">json_dump_callback</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*json</em>, <a class="reference internal" href="#c.json_dump_callback_t" title="json_dump_callback_t">json_dump_callback_t</a><em>&nbsp;callback</em>, void<em>&nbsp;*data</em>, size_t<em>&nbsp;flags</em><big>)</big><a class="headerlink" href="#c.json_dump_callback" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Call <em>callback</em> repeatedly, passing a chunk of the JSON
3421N/Arepresentation of <em>root</em> each time. <em>flags</em> is described above.
3421N/AReturns 0 on success and -1 on error.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.2.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="decoding">
3421N/A<span id="apiref-decoding"></span><h2>Decoding<a class="headerlink" href="#decoding" title="Permalink to this headline">¶</a></h2>
3421N/A<p>This sections describes the functions that can be used to decode JSON
3421N/Atext to the Jansson representation of JSON data. The JSON
3421N/Aspecification requires that a JSON text is either a serialized array
3421N/Aor object, and this requirement is also enforced with the following
3421N/Afunctions. In other words, the top level value in the JSON text being
3421N/Adecoded must be either array or object. To decode any JSON value, use
3421N/Athe <tt class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></tt> flag (see below).</p>
3421N/A<p>See <a class="reference internal" href="conformance.html#rfc-conformance"><em>RFC Conformance</em></a> for a discussion on Jansson&#8217;s conformance
3421N/Ato the JSON specification. It explains many design decisions that
3421N/Aaffect especially the behavior of the decoder.</p>
3421N/A<p>Each function takes a <em>flags</em> parameter that can be used to control
3421N/Athe behavior of the decoder. Its default value is 0. The following
3421N/Amacros can be ORed together to obtain <em>flags</em>.</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_REJECT_DUPLICATES</span></tt></dt>
3421N/A<dd><p class="first">Issue a decoding error if any JSON object in the input text
3421N/Acontains duplicate keys. Without this flag, the value of the last
3421N/Aoccurence of each key ends up in the result. Key equivalence is
3421N/Achecked byte-by-byte, without special Unicode comparison
3421N/Aalgorithms.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.1.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></tt></dt>
3421N/A<dd><p class="first">By default, the decoder expects an array or object as the input.
3421N/AWith this flag enabled, the decoder accepts any valid JSON value.</p>
3421N/A<p><strong>Note:</strong> Decoding any value may be useful in some scenarios, but
3421N/Ait&#8217;s generally discouraged as it violates strict compatiblity with
3421N/A<span class="target" id="index-2"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>. If you use this flag, don&#8217;t expect interoperatibility
3421N/Awith other JSON systems.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></tt></dt>
3421N/A<dd><p class="first">By default, the decoder expects that its whole input constitutes a
3421N/Avalid JSON text, and issues an error if there&#8217;s extra data after
3421N/Athe otherwise valid JSON input. With this flag enabled, the decoder
3421N/Astops after decoding a valid JSON array or object, and thus allows
3421N/Aextra data after the JSON text.</p>
3421N/A<p>Normally, reading will stop when the last <tt class="docutils literal"><span class="pre">]</span></tt> or <tt class="docutils literal"><span class="pre">}</span></tt> in the
3421N/AJSON input is encountered. If both <tt class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></tt> and
3421N/A<tt class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></tt> flags are used, the decoder may read one extra
3421N/AUTF-8 code unit (up to 4 bytes of input). For example, decoding
3421N/A<tt class="docutils literal"><span class="pre">4true</span></tt> correctly decodes the integer 4, but also reads the
3421N/A<tt class="docutils literal"><span class="pre">t</span></tt>. For this reason, if reading multiple consecutive values that
3421N/Aare not arrays or objects, they should be separated by at least one
3421N/Awhitespace character.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.1.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_DECODE_INT_AS_REAL</span></tt></dt>
3421N/A<dd><p class="first">JSON defines only one number type. Jansson distinguishes between
3421N/Aints and reals. For more information see <a class="reference internal" href="conformance.html#real-vs-integer"><em>Real vs. Integer</em></a>.
3421N/AWith this flag enabled the decoder interprets all numbers as real
3421N/Avalues. Integers that do not have an exact double representation
3421N/Awill silently result in a loss of precision. Integers that cause
3421N/Aa double overflow will cause an error.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.5.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_ALLOW_NUL</span></tt></dt>
3421N/A<dd><p class="first">Allow <tt class="docutils literal"><span class="pre">\u0000</span></tt> escape inside string values. This is a safety
3421N/Ameasure; If you know your input can contain NUL bytes, use this
3421N/Aflag. If you don&#8217;t use this flag, you don&#8217;t have to worry about NUL
3421N/Abytes inside strings unless you explicitly create themselves by
3421N/Ausing e.g. <a class="reference internal" href="#c.json_stringn" title="json_stringn"><tt class="xref c c-func docutils literal"><span class="pre">json_stringn()</span></tt></a> or <tt class="docutils literal"><span class="pre">s#</span></tt> format specifier for
3421N/A<a class="reference internal" href="#c.json_pack" title="json_pack"><tt class="xref c c-func docutils literal"><span class="pre">json_pack()</span></tt></a>.</p>
3421N/A<p>Object keys cannot have embedded NUL bytes even if this flag is
3421N/Aused.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.6.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A</dl>
3421N/A<p>Each function also takes an optional <a class="reference internal" href="#c.json_error_t" title="json_error_t"><tt class="xref c c-type docutils literal"><span class="pre">json_error_t</span></tt></a> parameter
3421N/Athat is filled with error information if decoding fails. It&#8217;s also
3421N/Aupdated on success; the number of bytes of input read is written to
3421N/Aits <tt class="docutils literal"><span class="pre">position</span></tt> field. This is especially useful when using
3421N/A<tt class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></tt> to read multiple consecutive JSON texts.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3: </span>Number of bytes of input read is written to the <tt class="docutils literal"><span class="pre">position</span></tt> field
3421N/Aof the <a class="reference internal" href="#c.json_error_t" title="json_error_t"><tt class="xref c c-type docutils literal"><span class="pre">json_error_t</span></tt></a> structure.</p>
3421N/A</div>
3421N/A<p>If no error or position information is needed, you can pass <em>NULL</em>.</p>
3421N/A<p>The following functions perform the actual JSON decoding.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_loads">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_loads</tt><big>(</big>const char<em>&nbsp;*input</em>, size_t<em>&nbsp;flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em><big>)</big><a class="headerlink" href="#c.json_loads" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON string <em>input</em> and returns the array or object it
3421N/Acontains, or <em>NULL</em> on error, in which case <em>error</em> is filled with
3421N/Ainformation about the error. <em>flags</em> is described above.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_loadb">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_loadb</tt><big>(</big>const char<em>&nbsp;*buffer</em>, size_t<em>&nbsp;buflen</em>, size_t<em>&nbsp;flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em><big>)</big><a class="headerlink" href="#c.json_loadb" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON string <em>buffer</em>, whose length is <em>buflen</em>, and
3421N/Areturns the array or object it contains, or <em>NULL</em> on error, in
3421N/Awhich case <em>error</em> is filled with information about the error. This
3421N/Ais similar to <a class="reference internal" href="#c.json_loads" title="json_loads"><tt class="xref c c-func docutils literal"><span class="pre">json_loads()</span></tt></a> except that the string doesn&#8217;t
3421N/Aneed to be null-terminated. <em>flags</em> is described above.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.1.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_loadf">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_loadf</tt><big>(</big>FILE<em>&nbsp;*input</em>, size_t<em>&nbsp;flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em><big>)</big><a class="headerlink" href="#c.json_loadf" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text in stream <em>input</em> and returns the array or
3421N/Aobject it contains, or <em>NULL</em> on error, in which case <em>error</em> is
3421N/Afilled with information about the error. <em>flags</em> is described
3421N/Aabove.</p>
3421N/A<p>This function will start reading the input from whatever position
3421N/Athe input file was, without attempting to seek first. If an error
3421N/Aoccurs, the file position will be left indeterminate. On success,
3421N/Athe file position will be at EOF, unless <tt class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></tt>
3421N/Aflag was used. In this case, the file position will be at the first
3421N/Acharacter after the last <tt class="docutils literal"><span class="pre">]</span></tt> or <tt class="docutils literal"><span class="pre">}</span></tt> in the JSON input. This
3421N/Aallows calling <a class="reference internal" href="#c.json_loadf" title="json_loadf"><tt class="xref c c-func docutils literal"><span class="pre">json_loadf()</span></tt></a> on the same <tt class="docutils literal"><span class="pre">FILE</span></tt> object
3421N/Amultiple times, if the input consists of consecutive JSON texts,
3421N/Apossibly separated by whitespace.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_load_file">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_load_file</tt><big>(</big>const char<em>&nbsp;*path</em>, size_t<em>&nbsp;flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em><big>)</big><a class="headerlink" href="#c.json_load_file" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text in file <em>path</em> and returns the array or
3421N/Aobject it contains, or <em>NULL</em> on error, in which case <em>error</em> is
3421N/Afilled with information about the error. <em>flags</em> is described
3421N/Aabove.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="type">
3421N/A<dt id="c.json_load_callback_t">
3421N/A<tt class="descname">json_load_callback_t</tt><a class="headerlink" href="#c.json_load_callback_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>A typedef for a function that&#8217;s called by
3421N/A<a class="reference internal" href="#c.json_load_callback" title="json_load_callback"><tt class="xref c c-func docutils literal"><span class="pre">json_load_callback()</span></tt></a> to read a chunk of input data:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="nf">size_t</span> <span class="p">(</span><span class="o">*</span><span class="kt">json_load_callback_t</span><span class="p">)(</span><span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">buflen</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p><em>buffer</em> points to a buffer of <em>buflen</em> bytes, and <em>data</em> is the
3421N/Acorresponding <a class="reference internal" href="#c.json_load_callback" title="json_load_callback"><tt class="xref c c-func docutils literal"><span class="pre">json_load_callback()</span></tt></a> argument passed through.</p>
3421N/A<p>On success, the function should return the number of bytes read; a
3421N/Areturned value of 0 indicates that no data was read and that the
3421N/Aend of file has been reached. On error, the function should return
3421N/A<tt class="docutils literal"><span class="pre">(size_t)-1</span></tt> to abort the decoding process.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.4.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_load_callback">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_load_callback</tt><big>(</big><a class="reference internal" href="#c.json_load_callback_t" title="json_load_callback_t">json_load_callback_t</a><em>&nbsp;callback</em>, void<em>&nbsp;*data</em>, size_t<em>&nbsp;flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em><big>)</big><a class="headerlink" href="#c.json_load_callback" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text produced by repeated calls to <em>callback</em>, and
3421N/Areturns the array or object it contains, or <em>NULL</em> on error, in
3421N/Awhich case <em>error</em> is filled with information about the error.
3421N/A<em>data</em> is passed through to <em>callback</em> on each call. <em>flags</em> is
3421N/Adescribed above.</p>
3421N/A<div class="versionadded">
3421N/A<p><span class="versionmodified">New in version 2.4.</span></p>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="building-values">
3421N/A<span id="apiref-pack"></span><h2>Building Values<a class="headerlink" href="#building-values" title="Permalink to this headline">¶</a></h2>
3421N/A<p>This section describes functions that help to create, or <em>pack</em>,
3421N/Acomplex JSON values, especially nested objects and arrays. Value
3421N/Abuilding is based on a <em>format string</em> that is used to tell the
3421N/Afunctions about the expected arguments.</p>
3421N/A<p>For example, the format string <tt class="docutils literal"><span class="pre">&quot;i&quot;</span></tt> specifies a single integer
3421N/Avalue, while the format string <tt class="docutils literal"><span class="pre">&quot;[ssb]&quot;</span></tt> or the equivalent <tt class="docutils literal"><span class="pre">&quot;[s,</span> <span class="pre">s,</span>
3421N/A<span class="pre">b]&quot;</span></tt> specifies an array value with two strings and a boolean as its
3421N/Aitems:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Create the JSON integer 42 */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;i&quot;</span><span class="p">,</span> <span class="mi">42</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Create the JSON array [&quot;foo&quot;, &quot;bar&quot;, true] */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;[ssb]&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Here&#8217;s the full list of format specifiers. The type in parentheses
3421N/Adenotes the resulting JSON type, and the type in brackets (if any)
3421N/Adenotes the C type that is expected as the corresponding argument or
3421N/Aarguments.</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">s</span></tt> (string) [const char *]</dt>
3421N/A<dd>Convert a NULL terminated UTF-8 string to a JSON string.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">s#</span></tt> (string) [const char *, int]</dt>
3421N/A<dd><p class="first">Convert a UTF-8 buffer of a given length to a JSON string.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.5.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">s%</span></tt> (string) [const char *, size_t]</dt>
3421N/A<dd><p class="first">Like <tt class="docutils literal"><span class="pre">s#</span></tt> but the length argument is of type <tt class="xref c c-type docutils literal"><span class="pre">size_t</span></tt>.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.6.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">+</span></tt> [const char *]</dt>
3421N/A<dd><p class="first">Like <tt class="docutils literal"><span class="pre">s</span></tt>, but concatenate to the previous string. Only valid
3421N/Aafter <tt class="docutils literal"><span class="pre">s</span></tt>, <tt class="docutils literal"><span class="pre">s#</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">+#</span></tt>.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.5.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">+#</span></tt> [const char *, int]</dt>
3421N/A<dd><p class="first">Like <tt class="docutils literal"><span class="pre">s#</span></tt>, but concatenate to the previous string. Only valid
3421N/Aafter <tt class="docutils literal"><span class="pre">s</span></tt>, <tt class="docutils literal"><span class="pre">s#</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">+#</span></tt>.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.5.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">+%</span></tt> (string) [const char *, size_t]</dt>
3421N/A<dd><p class="first">Like <tt class="docutils literal"><span class="pre">+#</span></tt> but the length argument is of type <tt class="xref c c-type docutils literal"><span class="pre">size_t</span></tt>.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.6.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">n</span></tt> (null)</dt>
3421N/A<dd>Output a JSON null value. No argument is consumed.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">b</span></tt> (boolean) [int]</dt>
3421N/A<dd>Convert a C <tt class="xref c c-type docutils literal"><span class="pre">int</span></tt> to JSON boolean value. Zero is converted
3421N/Ato <tt class="docutils literal"><span class="pre">false</span></tt> and non-zero to <tt class="docutils literal"><span class="pre">true</span></tt>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">i</span></tt> (integer) [int]</dt>
3421N/A<dd>Convert a C <tt class="xref c c-type docutils literal"><span class="pre">int</span></tt> to JSON integer.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">I</span></tt> (integer) [json_int_t]</dt>
3421N/A<dd>Convert a C <a class="reference internal" href="#c.json_int_t" title="json_int_t"><tt class="xref c c-type docutils literal"><span class="pre">json_int_t</span></tt></a> to JSON integer.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">f</span></tt> (real) [double]</dt>
3421N/A<dd>Convert a C <tt class="xref c c-type docutils literal"><span class="pre">double</span></tt> to JSON real.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">o</span></tt> (any value) [json_t *]</dt>
3421N/A<dd>Output any given JSON value as-is. If the value is added to an
3421N/Aarray or object, the reference to the value passed to <tt class="docutils literal"><span class="pre">o</span></tt> is
3421N/Astolen by the container.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">O</span></tt> (any value) [json_t *]</dt>
3421N/A<dd>Like <tt class="docutils literal"><span class="pre">o</span></tt>, but the argument&#8217;s reference count is incremented.
3421N/AThis is useful if you pack into an array or object and want to
3421N/Akeep the reference for the JSON value consumed by <tt class="docutils literal"><span class="pre">O</span></tt> to
3421N/Ayourself.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">[fmt]</span></tt> (array)</dt>
3421N/A<dd>Build an array with contents from the inner format string. <tt class="docutils literal"><span class="pre">fmt</span></tt>
3421N/Amay contain objects and arrays, i.e. recursive value building is
3421N/Asupported.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">{fmt}</span></tt> (object)</dt>
3421N/A<dd>Build an object with contents from the inner format string
3421N/A<tt class="docutils literal"><span class="pre">fmt</span></tt>. The first, third, etc. format specifier represent a key,
3421N/Aand must be a string (see <tt class="docutils literal"><span class="pre">s</span></tt>, <tt class="docutils literal"><span class="pre">s#</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt> and <tt class="docutils literal"><span class="pre">+#</span></tt> above),
3421N/Aas object keys are always strings. The second, fourth, etc. format
3421N/Aspecifier represent a value. Any value may be an object or array,
3421N/Ai.e. recursive value building is supported.</dd>
3421N/A</dl>
3421N/A<p>Whitespace, <tt class="docutils literal"><span class="pre">:</span></tt> and <tt class="docutils literal"><span class="pre">,</span></tt> are ignored.</p>
3421N/A<p>The following functions compose the value building API:</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_pack">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_pack</tt><big>(</big>const char<em>&nbsp;*fmt</em>, ...<big>)</big><a class="headerlink" href="#c.json_pack" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Build a new JSON value according to the format string <em>fmt</em>. For
3421N/Aeach format specifier (except for <tt class="docutils literal"><span class="pre">{}[]n</span></tt>), one or more arguments
3421N/Aare consumed and used to build the corresponding value. Returns
3421N/A<em>NULL</em> on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_pack_ex">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_pack_ex</tt><big>(</big><a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em>, size_t<em>&nbsp;flags</em>, const char<em>&nbsp;*fmt</em>, ...<big>)</big><a class="headerlink" href="#c.json_pack_ex" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_vpack_ex">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_vpack_ex</tt><big>(</big><a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em>, size_t<em>&nbsp;flags</em>, const char<em>&nbsp;*fmt</em>, va_list<em>&nbsp;ap</em><big>)</big><a class="headerlink" href="#c.json_vpack_ex" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_pack" title="json_pack"><tt class="xref c c-func docutils literal"><span class="pre">json_pack()</span></tt></a>, but an in the case of an error, an error
3421N/Amessage is written to <em>error</em>, if it&#8217;s not <em>NULL</em>. The <em>flags</em>
3421N/Aparameter is currently unused and should be set to 0.</p>
3421N/A<p>As only the errors in format string (and out-of-memory errors) can
3421N/Abe caught by the packer, these two functions are most likely only
3421N/Auseful for debugging format strings.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p>More examples:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Build an empty JSON object */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Build the JSON object {&quot;foo&quot;: 42, &quot;bar&quot;: 7} */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;{sisi}&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="mi">7</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Like above, &#39;:&#39;, &#39;,&#39; and whitespace are ignored */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;{s:i, s:i}&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="mi">7</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Build the JSON array [[1, 2], {&quot;cool&quot;: true}] */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;[[i,i],{s:b}]&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">&quot;cool&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Build a string from a non-NUL terminated buffer */</span>
3421N/A<span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="sc">&#39;t&#39;</span><span class="p">,</span> <span class="sc">&#39;e&#39;</span><span class="p">,</span> <span class="sc">&#39;s&#39;</span><span class="p">,</span> <span class="sc">&#39;t&#39;</span><span class="p">};</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;s#&quot;</span><span class="p">,</span> <span class="n">buffer</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* Concatentate strings together to build the JSON string &quot;foobarbaz&quot; */</span>
3421N/A<span class="n">json_pack</span><span class="p">(</span><span class="s">&quot;s++&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;baz&quot;</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</div>
3421N/A<div class="section" id="parsing-and-validating-values">
3421N/A<span id="apiref-unpack"></span><h2>Parsing and Validating Values<a class="headerlink" href="#parsing-and-validating-values" title="Permalink to this headline">¶</a></h2>
3421N/A<p>This section describes functions that help to validate complex values
3421N/Aand extract, or <em>unpack</em>, data from them. Like <a class="reference internal" href="#apiref-pack"><em>building values</em></a>, this is also based on format strings.</p>
3421N/A<p>While a JSON value is unpacked, the type specified in the format
3421N/Astring is checked to match that of the JSON value. This is the
3421N/Avalidation part of the process. In addition to this, the unpacking
3421N/Afunctions can also check that all items of arrays and objects are
3421N/Aunpacked. This check be enabled with the format specifier <tt class="docutils literal"><span class="pre">!</span></tt> or by
3421N/Ausing the flag <tt class="docutils literal"><span class="pre">JSON_STRICT</span></tt>. See below for details.</p>
3421N/A<p>Here&#8217;s the full list of format specifiers. The type in parentheses
3421N/Adenotes the JSON type, and the type in brackets (if any) denotes the C
3421N/Atype whose address should be passed.</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">s</span></tt> (string) [const char *]</dt>
3421N/A<dd>Convert a JSON string to a pointer to a NULL terminated UTF-8
3421N/Astring. The resulting string is extracted by using
3421N/A<a class="reference internal" href="#c.json_string_value" title="json_string_value"><tt class="xref c c-func docutils literal"><span class="pre">json_string_value()</span></tt></a> internally, so it exists as long as
3421N/Athere are still references to the corresponding JSON string.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">s%</span></tt> (string) [const char *, size_t *]</dt>
3421N/A<dd><p class="first">Convert a JSON string to a pointer to a NULL terminated UTF-8
3421N/Astring and its length.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.6.</span></p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">n</span></tt> (null)</dt>
3421N/A<dd>Expect a JSON null value. Nothing is extracted.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">b</span></tt> (boolean) [int]</dt>
3421N/A<dd>Convert a JSON boolean value to a C <tt class="xref c c-type docutils literal"><span class="pre">int</span></tt>, so that <tt class="docutils literal"><span class="pre">true</span></tt>
3421N/Ais converted to 1 and <tt class="docutils literal"><span class="pre">false</span></tt> to 0.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">i</span></tt> (integer) [int]</dt>
3421N/A<dd>Convert a JSON integer to C <tt class="xref c c-type docutils literal"><span class="pre">int</span></tt>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">I</span></tt> (integer) [json_int_t]</dt>
3421N/A<dd>Convert a JSON integer to C <a class="reference internal" href="#c.json_int_t" title="json_int_t"><tt class="xref c c-type docutils literal"><span class="pre">json_int_t</span></tt></a>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">f</span></tt> (real) [double]</dt>
3421N/A<dd>Convert a JSON real to C <tt class="xref c c-type docutils literal"><span class="pre">double</span></tt>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">F</span></tt> (integer or real) [double]</dt>
3421N/A<dd>Convert a JSON number (integer or real) to C <tt class="xref c c-type docutils literal"><span class="pre">double</span></tt>.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">o</span></tt> (any value) [json_t *]</dt>
3421N/A<dd>Store a JSON value with no conversion to a <a class="reference internal" href="#c.json_t" title="json_t"><tt class="xref c c-type docutils literal"><span class="pre">json_t</span></tt></a> pointer.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">O</span></tt> (any value) [json_t *]</dt>
3421N/A<dd>Like <tt class="docutils literal"><span class="pre">O</span></tt>, but the JSON value&#8217;s reference count is incremented.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">[fmt]</span></tt> (array)</dt>
3421N/A<dd>Convert each item in the JSON array according to the inner format
3421N/Astring. <tt class="docutils literal"><span class="pre">fmt</span></tt> may contain objects and arrays, i.e. recursive
3421N/Avalue extraction is supporetd.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">{fmt}</span></tt> (object)</dt>
3421N/A<dd><p class="first">Convert each item in the JSON object according to the inner format
3421N/Astring <tt class="docutils literal"><span class="pre">fmt</span></tt>. The first, third, etc. format specifier represent
3421N/Aa key, and must be <tt class="docutils literal"><span class="pre">s</span></tt>. The corresponding argument to unpack
3421N/Afunctions is read as the object key. The second fourth, etc.
3421N/Aformat specifier represent a value and is written to the address
3421N/Agiven as the corresponding argument. <strong>Note</strong> that every other
3421N/Aargument is read from and every other is written to.</p>
3421N/A<p><tt class="docutils literal"><span class="pre">fmt</span></tt> may contain objects and arrays as values, i.e. recursive
3421N/Avalue extraction is supporetd.</p>
3421N/A<div class="last versionadded">
3421N/A<p><span class="versionmodified">New in version 2.3: </span>Any <tt class="docutils literal"><span class="pre">s</span></tt> representing a key may be suffixed with a <tt class="docutils literal"><span class="pre">?</span></tt> to
3421N/Amake the key optional. If the key is not found, nothing is
3421N/Aextracted. See below for an example.</p>
3421N/A</div>
3421N/A</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">!</span></tt></dt>
3421N/A<dd>This special format specifier is used to enable the check that
3421N/Aall object and array items are accessed, on a per-value basis. It
3421N/Amust appear inside an array or object as the last format specifier
3421N/Abefore the closing bracket or brace. To enable the check globally,
3421N/Ause the <tt class="docutils literal"><span class="pre">JSON_STRICT</span></tt> unpacking flag.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">*</span></tt></dt>
3421N/A<dd>This special format specifier is the opposite of <tt class="docutils literal"><span class="pre">!</span></tt>. If the
3421N/A<tt class="docutils literal"><span class="pre">JSON_STRICT</span></tt> flag is used, <tt class="docutils literal"><span class="pre">*</span></tt> can be used to disable the
3421N/Astrict check on a per-value basis. It must appear inside an array
3421N/Aor object as the last format specifier before the closing bracket
3421N/Aor brace.</dd>
3421N/A</dl>
3421N/A<p>Whitespace, <tt class="docutils literal"><span class="pre">:</span></tt> and <tt class="docutils literal"><span class="pre">,</span></tt> are ignored.</p>
3421N/A<p>The following functions compose the parsing and validation API:</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_unpack">
3421N/Aint <tt class="descname">json_unpack</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*root</em>, const char<em>&nbsp;*fmt</em>, ...<big>)</big><a class="headerlink" href="#c.json_unpack" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Validate and unpack the JSON value <em>root</em> according to the format
3421N/Astring <em>fmt</em>. Returns 0 on success and -1 on failure.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_unpack_ex">
3421N/Aint <tt class="descname">json_unpack_ex</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*root</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em>, size_t<em>&nbsp;flags</em>, const char<em>&nbsp;*fmt</em>, ...<big>)</big><a class="headerlink" href="#c.json_unpack_ex" title="Permalink to this definition">¶</a></dt>
3421N/A<dt id="c.json_vunpack_ex">
3421N/Aint <tt class="descname">json_vunpack_ex</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*root</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em>&nbsp;*error</em>, size_t<em>&nbsp;flags</em>, const char<em>&nbsp;*fmt</em>, va_list<em>&nbsp;ap</em><big>)</big><a class="headerlink" href="#c.json_vunpack_ex" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Validate and unpack the JSON value <em>root</em> according to the format
3421N/Astring <em>fmt</em>. If an error occurs and <em>error</em> is not <em>NULL</em>, write
3421N/Aerror information to <em>error</em>. <em>flags</em> can be used to control the
3421N/Abehaviour of the unpacker, see below for the flags. Returns 0 on
3421N/Asuccess and -1 on failure.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<div class="admonition note">
3421N/A<p class="first admonition-title">Note</p>
3421N/A<p>The first argument of all unpack functions is <tt class="docutils literal"><span class="pre">json_t</span> <span class="pre">*root</span></tt>
3421N/Ainstead of <tt class="docutils literal"><span class="pre">const</span> <span class="pre">json_t</span> <span class="pre">*root</span></tt>, because the use of <tt class="docutils literal"><span class="pre">O</span></tt> format
3421N/Aspecifier causes the reference count of <tt class="docutils literal"><span class="pre">root</span></tt>, or some value
3421N/Areachable from <tt class="docutils literal"><span class="pre">root</span></tt>, to be increased. Furthermore, the <tt class="docutils literal"><span class="pre">o</span></tt>
3421N/Aformat specifier may be used to extract a value as-is, which allows
3421N/Amodifying the structure or contents of a value reachable from
3421N/A<tt class="docutils literal"><span class="pre">root</span></tt>.</p>
3421N/A<p class="last">If the <tt class="docutils literal"><span class="pre">O</span></tt> and <tt class="docutils literal"><span class="pre">o</span></tt> format specifiers are not used, it&#8217;s
3421N/Aperfectly safe to cast a <tt class="docutils literal"><span class="pre">const</span> <span class="pre">json_t</span> <span class="pre">*</span></tt> variable to plain
3421N/A<tt class="docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></tt> when used with these functions.</p>
3421N/A</div>
3421N/A<p>The following unpacking flags are available:</p>
3421N/A<dl class="docutils">
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_STRICT</span></tt></dt>
3421N/A<dd>Enable the extra validation step checking that all object and
3421N/Aarray items are unpacked. This is equivalent to appending the
3421N/Aformat specifier <tt class="docutils literal"><span class="pre">!</span></tt> to the end of every array and object in the
3421N/Aformat string.</dd>
3421N/A<dt><tt class="docutils literal"><span class="pre">JSON_VALIDATE_ONLY</span></tt></dt>
3421N/A<dd>Don&#8217;t extract any data, just validate the JSON value against the
3421N/Agiven format string. Note that object keys must still be specified
3421N/Aafter the format string.</dd>
3421N/A</dl>
3421N/A<p>Examples:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* root is the JSON integer 42 */</span>
3421N/A<span class="kt">int</span> <span class="n">myint</span><span class="p">;</span>
3421N/A<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">&quot;i&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint</span><span class="p">);</span>
3421N/A<span class="n">assert</span><span class="p">(</span><span class="n">myint</span> <span class="o">==</span> <span class="mi">42</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* root is the JSON object {&quot;foo&quot;: &quot;bar&quot;, &quot;quux&quot;: true} */</span>
3421N/A<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">;</span>
3421N/A<span class="kt">int</span> <span class="n">boolean</span><span class="p">;</span>
3421N/A<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">&quot;{s:s, s:b}&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">str</span><span class="p">,</span> <span class="s">&quot;quux&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">boolean</span><span class="p">);</span>
3421N/A<span class="n">assert</span><span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">str</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">boolean</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span>
3421N/A
3421N/A<span class="cm">/* root is the JSON array [[1, 2], {&quot;baz&quot;: null} */</span>
3421N/A<span class="kt">json_error_t</span> <span class="n">error</span><span class="p">;</span>
3421N/A<span class="n">json_unpack_ex</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">error</span><span class="p">,</span> <span class="n">JSON_VALIDATE_ONLY</span><span class="p">,</span> <span class="s">&quot;[[i,i], {s:n}]&quot;</span><span class="p">,</span> <span class="s">&quot;baz&quot;</span><span class="p">);</span>
3421N/A<span class="cm">/* returns 0 for validation success, nothing is extracted */</span>
3421N/A
3421N/A<span class="cm">/* root is the JSON array [1, 2, 3, 4, 5] */</span>
3421N/A<span class="kt">int</span> <span class="n">myint1</span><span class="p">,</span> <span class="n">myint2</span><span class="p">;</span>
3421N/A<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">&quot;[ii!]&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint1</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint2</span><span class="p">);</span>
3421N/A<span class="cm">/* returns -1 for failed validation */</span>
3421N/A
3421N/A<span class="cm">/* root is an empty JSON object */</span>
3421N/A<span class="kt">int</span> <span class="n">myint</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">myint2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
3421N/A<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">&quot;{s?i, s?[ii]}&quot;</span><span class="p">,</span>
3421N/A <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint1</span><span class="p">,</span>
3421N/A <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint2</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myint3</span><span class="p">);</span>
3421N/A<span class="cm">/* myint1, myint2 or myint3 is no touched as &quot;foo&quot; and &quot;bar&quot; don&#39;t exist */</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</div>
3421N/A<div class="section" id="equality">
3421N/A<h2>Equality<a class="headerlink" href="#equality" title="Permalink to this headline">¶</a></h2>
3421N/A<p>Testing for equality of two JSON values cannot, in general, be
3421N/Aachieved using the <tt class="docutils literal"><span class="pre">==</span></tt> operator. Equality in the terms of the
3421N/A<tt class="docutils literal"><span class="pre">==</span></tt> operator states that the two <a class="reference internal" href="#c.json_t" title="json_t"><tt class="xref c c-type docutils literal"><span class="pre">json_t</span></tt></a> pointers point to
3421N/Aexactly the same JSON value. However, two JSON values can be equal not
3421N/Aonly if they are exactly the same value, but also if they have equal
3421N/A&#8220;contents&#8221;:</p>
3421N/A<ul class="simple">
3421N/A<li>Two integer or real values are equal if their contained numeric
3421N/Avalues are equal. An integer value is never equal to a real value,
3421N/Athough.</li>
3421N/A<li>Two strings are equal if their contained UTF-8 strings are equal,
3421N/Abyte by byte. Unicode comparison algorithms are not implemented.</li>
3421N/A<li>Two arrays are equal if they have the same number of elements and
3421N/Aeach element in the first array is equal to the corresponding
3421N/Aelement in the second array.</li>
3421N/A<li>Two objects are equal if they have exactly the same keys and the
3421N/Avalue for each key in the first object is equal to the value of the
3421N/Acorresponding key in the second object.</li>
3421N/A<li>Two true, false or null values have no &#8220;contents&#8221;, so they are equal
3421N/Aif their types are equal. (Because these values are singletons,
3421N/Atheir equality can actually be tested with <tt class="docutils literal"><span class="pre">==</span></tt>.)</li>
3421N/A</ul>
3421N/A<p>The following function can be used to test whether two JSON values are
3421N/Aequal.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_equal">
3421N/Aint <tt class="descname">json_equal</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value1</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value2</em><big>)</big><a class="headerlink" href="#c.json_equal" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Returns 1 if <em>value1</em> and <em>value2</em> are equal, as defined above.
3421N/AReturns 0 if they are inequal or one or both of the pointers are
3421N/A<em>NULL</em>.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="copying">
3421N/A<h2>Copying<a class="headerlink" href="#copying" title="Permalink to this headline">¶</a></h2>
3421N/A<p>Because of reference counting, passing JSON values around doesn&#8217;t
3421N/Arequire copying them. But sometimes a fresh copy of a JSON value is
3421N/Aneeded. For example, if you need to modify an array, but still want to
3421N/Ause the original afterwards, you should take a copy of it first.</p>
3421N/A<p>Jansson supports two kinds of copying: shallow and deep. There is a
3421N/Adifference between these methods only for arrays and objects. Shallow
3421N/Acopying only copies the first level value (array or object) and uses
3421N/Athe same child values in the copied value. Deep copying makes a fresh
3421N/Acopy of the child values, too. Moreover, all the child values are deep
3421N/Acopied in a recursive fashion.</p>
3421N/A<dl class="function">
3421N/A<dt id="c.json_copy">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_copy</tt><big>(</big><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_copy" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a shallow copy of <em>value</em>, or <em>NULL</em> on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_deep_copy">
3421N/A<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<tt class="descname">json_deep_copy</tt><big>(</big>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#c.json_deep_copy" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><em class="refcount">Return value: New reference.</em><p>Returns a deep copy of <em>value</em>, or <em>NULL</em> on error.</p>
3421N/A</dd></dl>
3421N/A
3421N/A</div>
3421N/A<div class="section" id="custom-memory-allocation">
3421N/A<span id="apiref-custom-memory-allocation"></span><h2>Custom Memory Allocation<a class="headerlink" href="#custom-memory-allocation" title="Permalink to this headline">¶</a></h2>
3421N/A<p>By default, Jansson uses <tt class="xref c c-func docutils literal"><span class="pre">malloc()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">free()</span></tt> for
3421N/Amemory allocation. These functions can be overridden if custom
3421N/Abehavior is needed.</p>
3421N/A<dl class="type">
3421N/A<dt id="c.json_malloc_t">
3421N/A<tt class="descname">json_malloc_t</tt><a class="headerlink" href="#c.json_malloc_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>A typedef for a function pointer with <tt class="xref c c-func docutils literal"><span class="pre">malloc()</span></tt>&#8216;s
3421N/Asignature:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="kt">void</span> <span class="o">*</span><span class="p">(</span><span class="o">*</span><span class="kt">json_malloc_t</span><span class="p">)(</span><span class="kt">size_t</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="type">
3421N/A<dt id="c.json_free_t">
3421N/A<tt class="descname">json_free_t</tt><a class="headerlink" href="#c.json_free_t" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>A typedef for a function pointer with <tt class="xref c c-func docutils literal"><span class="pre">free()</span></tt>&#8216;s
3421N/Asignature:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="nf">void</span> <span class="p">(</span><span class="o">*</span><span class="kt">json_free_t</span><span class="p">)(</span><span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A</dd></dl>
3421N/A
3421N/A<dl class="function">
3421N/A<dt id="c.json_set_alloc_funcs">
3421N/Avoid <tt class="descname">json_set_alloc_funcs</tt><big>(</big><a class="reference internal" href="#c.json_malloc_t" title="json_malloc_t">json_malloc_t</a><em>&nbsp;malloc_fn</em>, <a class="reference internal" href="#c.json_free_t" title="json_free_t">json_free_t</a><em>&nbsp;free_fn</em><big>)</big><a class="headerlink" href="#c.json_set_alloc_funcs" title="Permalink to this definition">¶</a></dt>
3421N/A<dd><p>Use <em>malloc_fn</em> instead of <tt class="xref c c-func docutils literal"><span class="pre">malloc()</span></tt> and <em>free_fn</em> instead
3421N/Aof <tt class="xref c c-func docutils literal"><span class="pre">free()</span></tt>. This function has to be called before any other
3421N/AJansson&#8217;s API functions to ensure that all memory operations use
3421N/Athe same functions.</p>
3421N/A</dd></dl>
3421N/A
3421N/A<p><strong>Examples:</strong></p>
3421N/A<p>Circumvent problems with different CRT heaps on Windows by using
3421N/Aapplication&#8217;s <tt class="xref c c-func docutils literal"><span class="pre">malloc()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">free()</span></tt>:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">malloc</span><span class="p">,</span> <span class="n">free</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Use the <a class="reference external" href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/">Boehm&#8217;s conservative garbage collector</a> for memory
3421N/Aoperations:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">GC_malloc</span><span class="p">,</span> <span class="n">GC_free</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Allow storing sensitive data (e.g. passwords or encryption keys) in
3421N/AJSON structures by zeroing all memory when freed:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span> <span class="o">*</span><span class="nf">secure_malloc</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="cm">/* Store the memory area size in the beginning of the block */</span>
3421N/A <span class="kt">void</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="n">size</span> <span class="o">+</span> <span class="mi">8</span><span class="p">);</span>
3421N/A <span class="o">*</span><span class="p">((</span><span class="kt">size_t</span> <span class="o">*</span><span class="p">)</span><span class="n">ptr</span><span class="p">)</span> <span class="o">=</span> <span class="n">size</span><span class="p">;</span>
3421N/A <span class="k">return</span> <span class="n">ptr</span> <span class="o">+</span> <span class="mi">8</span><span class="p">;</span>
3421N/A<span class="p">}</span>
3421N/A
3421N/A<span class="k">static</span> <span class="kt">void</span> <span class="nf">secure_free</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">ptr</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="kt">size_t</span> <span class="n">size</span><span class="p">;</span>
3421N/A
3421N/A <span class="n">ptr</span> <span class="o">-=</span> <span class="mi">8</span><span class="p">;</span>
3421N/A <span class="n">size</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">size_t</span> <span class="o">*</span><span class="p">)</span><span class="n">ptr</span><span class="p">);</span>
3421N/A
3421N/A <span class="n">guaranteed_memset</span><span class="p">(</span><span class="n">ptr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span> <span class="o">+</span> <span class="mi">8</span><span class="p">);</span>
3421N/A <span class="n">free</span><span class="p">(</span><span class="n">ptr</span><span class="p">);</span>
3421N/A<span class="p">}</span>
3421N/A
3421N/A<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
3421N/A<span class="p">{</span>
3421N/A <span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">secure_malloc</span><span class="p">,</span> <span class="n">secure_free</span><span class="p">);</span>
3421N/A <span class="cm">/* ... */</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>For more information about the issues of storing sensitive data in
3421N/Amemory, see
3421N/A<a class="reference external" href="http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html">http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html</a>.
3421N/AThe page also explains the <tt class="xref c c-func docutils literal"><span class="pre">guaranteed_memset()</span></tt> function used
3421N/Ain the example and gives a sample implementation for it.</p>
3421N/A</div>
3421N/A</div>
3421N/A
3421N/A
3421N/A </div>
3421N/A </div>
3421N/A </div>
3421N/A <div class="sphinxsidebar">
3421N/A <div class="sphinxsidebarwrapper">
3421N/A <h3><a href="index.html">Table Of Contents</a></h3>
3421N/A <ul>
3421N/A<li><a class="reference internal" href="#">API Reference</a><ul>
3421N/A<li><a class="reference internal" href="#preliminaries">Preliminaries</a></li>
3421N/A<li><a class="reference internal" href="#library-version">Library Version</a></li>
3421N/A<li><a class="reference internal" href="#value-representation">Value Representation</a><ul>
3421N/A<li><a class="reference internal" href="#type">Type</a></li>
3421N/A<li><a class="reference internal" href="#reference-count">Reference Count</a></li>
3421N/A<li><a class="reference internal" href="#circular-references">Circular References</a></li>
3421N/A</ul>
3421N/A</li>
3421N/A<li><a class="reference internal" href="#true-false-and-null">True, False and Null</a></li>
3421N/A<li><a class="reference internal" href="#string">String</a></li>
3421N/A<li><a class="reference internal" href="#number">Number</a></li>
3421N/A<li><a class="reference internal" href="#array">Array</a></li>
3421N/A<li><a class="reference internal" href="#object">Object</a></li>
3421N/A<li><a class="reference internal" href="#error-reporting">Error reporting</a></li>
3421N/A<li><a class="reference internal" href="#encoding">Encoding</a></li>
3421N/A<li><a class="reference internal" href="#decoding">Decoding</a></li>
3421N/A<li><a class="reference internal" href="#building-values">Building Values</a></li>
3421N/A<li><a class="reference internal" href="#parsing-and-validating-values">Parsing and Validating Values</a></li>
3421N/A<li><a class="reference internal" href="#equality">Equality</a></li>
3421N/A<li><a class="reference internal" href="#copying">Copying</a></li>
3421N/A<li><a class="reference internal" href="#custom-memory-allocation">Custom Memory Allocation</a></li>
3421N/A</ul>
3421N/A</li>
3421N/A</ul>
3421N/A
3421N/A <h4>Previous topic</h4>
3421N/A <p class="topless"><a href="portability.html"
3421N/A title="previous chapter">Portability</a></p>
3421N/A <h4>Next topic</h4>
3421N/A <p class="topless"><a href="changes.html"
3421N/A title="next chapter">Changes in Jansson</a></p>
3421N/A <h3>This Page</h3>
3421N/A <ul class="this-page-menu">
3421N/A <li><a href="_sources/apiref.txt"
3421N/A rel="nofollow">Show Source</a></li>
3421N/A </ul>
3421N/A<div id="searchbox" style="display: none">
3421N/A <h3>Quick search</h3>
3421N/A <form class="search" action="search.html" method="get">
3421N/A <input type="text" name="q" />
3421N/A <input type="submit" value="Go" />
3421N/A <input type="hidden" name="check_keywords" value="yes" />
3421N/A <input type="hidden" name="area" value="default" />
3421N/A </form>
3421N/A <p class="searchtip" style="font-size: 90%">
3421N/A Enter search terms or a module, class or function name.
3421N/A </p>
3421N/A</div>
3421N/A<script type="text/javascript">$('#searchbox').show(0);</script>
3421N/A </div>
3421N/A </div>
3421N/A <div class="clearer"></div>
3421N/A </div>
3421N/A <div class="related">
3421N/A <h3>Navigation</h3>
3421N/A <ul>
3421N/A <li class="right" style="margin-right: 10px">
3421N/A <a href="genindex.html" title="General Index"
3421N/A >index</a></li>
3421N/A <li class="right" >
3421N/A <a href="changes.html" title="Changes in Jansson"
3421N/A >next</a> |</li>
3421N/A <li class="right" >
3421N/A <a href="portability.html" title="Portability"
3421N/A >previous</a> |</li>
3421N/A <li><a href="index.html">Jansson 2.7 documentation</a> &raquo;</li>
3421N/A </ul>
3421N/A </div>
3421N/A <div class="footer">
3421N/A &copy; Copyright 2009-2014, Petri Lehtinen.
3421N/A Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
3421N/A </div>
3421N/A </body>
3421N/A</html>