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