3421N/A<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3421N/A "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3421N/A
3421N/A
3421N/A<html xmlns="http://www.w3.org/1999/xhtml">
3421N/A <head>
3421N/A <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3421N/A
3421N/A <title>Tutorial &mdash; Jansson 2.7 documentation</title>
3421N/A
3421N/A <link rel="stylesheet" href="_static/default.css" type="text/css" />
3421N/A <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
3421N/A
3421N/A <script type="text/javascript">
3421N/A var DOCUMENTATION_OPTIONS = {
3421N/A URL_ROOT: './',
3421N/A VERSION: '2.7',
3421N/A COLLAPSE_INDEX: false,
3421N/A FILE_SUFFIX: '.html',
3421N/A HAS_SOURCE: true
3421N/A };
3421N/A </script>
3421N/A <script type="text/javascript" src="_static/jquery.js"></script>
3421N/A <script type="text/javascript" src="_static/underscore.js"></script>
3421N/A <script type="text/javascript" src="_static/doctools.js"></script>
3421N/A <link rel="top" title="Jansson 2.7 documentation" href="index.html" />
3421N/A <link rel="next" title="RFC Conformance" href="conformance.html" />
3421N/A <link rel="prev" title="Upgrading from 1.x" href="upgrading.html" />
3421N/A </head>
3421N/A <body>
3421N/A <div class="related">
3421N/A <h3>Navigation</h3>
3421N/A <ul>
3421N/A <li class="right" style="margin-right: 10px">
3421N/A <a href="genindex.html" title="General Index"
3421N/A accesskey="I">index</a></li>
3421N/A <li class="right" >
3421N/A <a href="conformance.html" title="RFC Conformance"
3421N/A accesskey="N">next</a> |</li>
3421N/A <li class="right" >
3421N/A <a href="upgrading.html" title="Upgrading from 1.x"
3421N/A accesskey="P">previous</a> |</li>
3421N/A <li><a href="index.html">Jansson 2.7 documentation</a> &raquo;</li>
3421N/A </ul>
3421N/A </div>
3421N/A
3421N/A <div class="document">
3421N/A <div class="documentwrapper">
3421N/A <div class="bodywrapper">
3421N/A <div class="body">
3421N/A
3421N/A <div class="section" id="tutorial">
3421N/A<span id="id1"></span><h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1>
3421N/A<p>In this tutorial, we create a program that fetches the latest commits
3421N/Aof a repository in <a class="reference external" href="https://github.com/">GitHub</a> over the web. <a class="reference external" href="http://developer.github.com/">GitHub API</a> uses JSON, so
3421N/Athe result can be parsed using Jansson.</p>
3421N/A<p>To stick to the the scope of this tutorial, we will only cover the the
3421N/Aparts of the program related to handling JSON data. For the best user
3421N/Aexperience, the full source code is available:
3421N/A<a class="reference download internal" href="_downloads/github_commits.c"><tt class="xref download docutils literal"><span class="pre">github_commits.c</span></tt></a>. To compile it (on Unix-like systems with
3421N/Agcc), use the following command:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">gcc</span> <span class="o">-</span><span class="n">o</span> <span class="n">github_commits</span> <span class="n">github_commits</span><span class="p">.</span><span class="n">c</span> <span class="o">-</span><span class="n">ljansson</span> <span class="o">-</span><span class="n">lcurl</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p><a class="reference external" href="http://curl.haxx.se/">libcurl</a> is used to communicate over the web, so it is required to
3421N/Acompile the program.</p>
3421N/A<p>The command line syntax is:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">github_commits</span> <span class="n">USER</span> <span class="n">REPOSITORY</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p><tt class="docutils literal"><span class="pre">USER</span></tt> is a GitHub user ID and <tt class="docutils literal"><span class="pre">REPOSITORY</span></tt> is the repository
3421N/Aname. Please note that the GitHub API is rate limited, so if you run
3421N/Athe program too many times within a short period of time, the sever
3421N/Astarts to respond with an error.</p>
3421N/A<div class="section" id="the-github-repo-commits-api">
3421N/A<span id="tutorial-github-commits-api"></span><h2>The GitHub Repo Commits API<a class="headerlink" href="#the-github-repo-commits-api" title="Permalink to this headline">¶</a></h2>
3421N/A<p>The <a class="reference external" href="http://developer.github.com/v3/repos/commits/">GitHub Repo Commits API</a> is used by sending HTTP requests to
3421N/AURLs like <tt class="docutils literal"><span class="pre">https://api.github.com/repos/USER/REPOSITORY/commits</span></tt>,
3421N/Awhere <tt class="docutils literal"><span class="pre">USER</span></tt> and <tt class="docutils literal"><span class="pre">REPOSITORY</span></tt> are the GitHub user ID and the name
3421N/Aof the repository whose commits are to be listed, respectively.</p>
3421N/A<p>GitHub responds with a JSON array of the following form:</p>
3421N/A<div class="highlight-none"><div class="highlight"><pre>[
3421N/A {
3421N/A &quot;sha&quot;: &quot;&lt;the commit ID&gt;&quot;,
3421N/A &quot;commit&quot;: {
3421N/A &quot;message&quot;: &quot;&lt;the commit message&gt;&quot;,
3421N/A &lt;more fields, not important to this tutorial...&gt;
3421N/A },
3421N/A &lt;more fields...&gt;
3421N/A },
3421N/A {
3421N/A &quot;sha&quot;: &quot;&lt;the commit ID&gt;&quot;,
3421N/A &quot;commit&quot;: {
3421N/A &quot;message&quot;: &quot;&lt;the commit message&gt;&quot;,
3421N/A &lt;more fields...&gt;
3421N/A },
3421N/A &lt;more fields...&gt;
3421N/A },
3421N/A &lt;more commits...&gt;
3421N/A]
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>In our program, the HTTP request is sent using the following
3421N/Afunction:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="nf">request</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">url</span><span class="p">);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>It takes the URL as a parameter, preforms a HTTP GET request, and
3421N/Areturns a newly allocated string that contains the response body. If
3421N/Athe request fails, an error message is printed to stderr and the
3421N/Areturn value is <em>NULL</em>. For full details, refer to <a class="reference download internal" href="_downloads/github_commits.c"><tt class="xref download docutils literal"><span class="pre">the</span> <span class="pre">code</span></tt></a>, as the actual implementation is not important
3421N/Ahere.</p>
3421N/A</div>
3421N/A<div class="section" id="the-program">
3421N/A<span id="tutorial-the-program"></span><h2>The Program<a class="headerlink" href="#the-program" title="Permalink to this headline">¶</a></h2>
3421N/A<p>First the includes:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include &lt;string.h&gt;</span>
3421N/A<span class="cp">#include &lt;jansson.h&gt;</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Like all the programs using Jansson, we need to include
3421N/A<tt class="file docutils literal"><span class="pre">jansson.h</span></tt>.</p>
3421N/A<p>The following definitions are used to build the GitHub API request
3421N/AURL:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cp">#define URL_FORMAT &quot;https:</span><span class="c1">//api.github.com/repos/%s/%s/commits&quot;</span>
3421N/A<span class="cp">#define URL_SIZE 256</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>The following function is used when formatting the result to find the
3421N/Afirst newline in the commit message:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Return the offset of the first newline in text or the length of</span>
3421N/A<span class="cm"> text if there&#39;s no newline */</span>
3421N/A<span class="k">static</span> <span class="kt">int</span> <span class="nf">newline_offset</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">newline</span> <span class="o">=</span> <span class="n">strchr</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="sc">&#39;\n&#39;</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">newline</span><span class="p">)</span>
3421N/A <span class="k">return</span> <span class="n">strlen</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
3421N/A <span class="k">else</span>
3421N/A <span class="k">return</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">newline</span> <span class="o">-</span> <span class="n">text</span><span class="p">);</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>The main function follows. In the beginning, we first declare a bunch
3421N/Aof variables and check the command line parameters:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
3421N/A<span class="p">{</span>
3421N/A <span class="kt">size_t</span> <span class="n">i</span><span class="p">;</span>
3421N/A <span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
3421N/A <span class="kt">char</span> <span class="n">url</span><span class="p">[</span><span class="n">URL_SIZE</span><span class="p">];</span>
3421N/A
3421N/A <span class="kt">json_t</span> <span class="o">*</span><span class="n">root</span><span class="p">;</span>
3421N/A <span class="kt">json_error_t</span> <span class="n">error</span><span class="p">;</span>
3421N/A
3421N/A <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">)</span>
3421N/A <span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;usage: %s USER REPOSITORY</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;List commits at USER&#39;s REPOSITORY.</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">2</span><span class="p">;</span>
3421N/A <span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Then we build the request URL using the user and repository names
3421N/Agiven as command line parameters:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">snprintf</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">URL_SIZE</span><span class="p">,</span> <span class="n">URL_FORMAT</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]);</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>This uses the <tt class="docutils literal"><span class="pre">URL_SIZE</span></tt> and <tt class="docutils literal"><span class="pre">URL_FORMAT</span></tt> constants defined above.
3421N/ANow we&#8217;re ready to actually request the JSON data over the web:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">text</span> <span class="o">=</span> <span class="n">request</span><span class="p">(</span><span class="n">url</span><span class="p">);</span>
3421N/A<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">text</span><span class="p">)</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>If an error occurs, our function <tt class="docutils literal"><span class="pre">request</span></tt> prints the error and
3421N/Areturns <em>NULL</em>, so it&#8217;s enough to just return 1 from the main
3421N/Afunction.</p>
3421N/A<p>Next we&#8217;ll call <a class="reference internal" href="apiref.html#c.json_loads" title="json_loads"><tt class="xref c c-func docutils literal"><span class="pre">json_loads()</span></tt></a> to decode the JSON text we got
3421N/Aas a response:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">root</span> <span class="o">=</span> <span class="n">json_loads</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">error</span><span class="p">);</span>
3421N/A<span class="n">free</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
3421N/A
3421N/A<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">root</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: on line %d: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">error</span><span class="p">.</span><span class="n">line</span><span class="p">,</span> <span class="n">error</span><span class="p">.</span><span class="n">text</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>We don&#8217;t need the JSON text anymore, so we can free the <tt class="docutils literal"><span class="pre">text</span></tt>
3421N/Avariable right after decoding it. If <a class="reference internal" href="apiref.html#c.json_loads" title="json_loads"><tt class="xref c c-func docutils literal"><span class="pre">json_loads()</span></tt></a> fails, it
3421N/Areturns <em>NULL</em> and sets error information to the <a class="reference internal" href="apiref.html#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>
3421N/Astructure given as the second parameter. In this case, our program
3421N/Aprints the error information out and returns 1 from the main function.</p>
3421N/A<p>Now we&#8217;re ready to extract the data out of the decoded JSON response.
3421N/AThe structure of the response JSON was explained in section
3421N/A<a class="reference internal" href="#tutorial-github-commits-api"><em>The GitHub Repo Commits API</em></a>.</p>
3421N/A<p>We check that the returned value really is an array:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json_is_array</span><span class="p">(</span><span class="n">root</span><span class="p">))</span>
3421N/A<span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: root is not an array</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
3421N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>Then we proceed to loop over all the commits in the array:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="k">for</span><span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">json_array_size</span><span class="p">(</span><span class="n">root</span><span class="p">);</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
3421N/A<span class="p">{</span>
3421N/A <span class="kt">json_t</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="o">*</span><span class="n">sha</span><span class="p">,</span> <span class="o">*</span><span class="n">commit</span><span class="p">,</span> <span class="o">*</span><span class="n">message</span><span class="p">;</span>
3421N/A <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">message_text</span><span class="p">;</span>
3421N/A
3421N/A <span class="n">data</span> <span class="o">=</span> <span class="n">json_array_get</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json_is_object</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
3421N/A <span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: commit data %d is not an object</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
3421N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A <span class="p">}</span>
3421N/A<span class="p">...</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>The function <a class="reference internal" href="apiref.html#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> returns the size of a JSON
3421N/Aarray. First, we again declare some variables and then extract the
3421N/Ai&#8217;th element of the <tt class="docutils literal"><span class="pre">root</span></tt> array using <a class="reference internal" href="apiref.html#c.json_array_get" title="json_array_get"><tt class="xref c c-func docutils literal"><span class="pre">json_array_get()</span></tt></a>.
3421N/AWe also check that the resulting value is a JSON object.</p>
3421N/A<p>Next we&#8217;ll extract the commit ID (a hexadecimal SHA-1 sum),
3421N/Aintermediate commit info object, and the commit message from that
3421N/Aobject. We also do proper type checks:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre> <span class="n">sha</span> <span class="o">=</span> <span class="n">json_object_get</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s">&quot;sha&quot;</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json_is_string</span><span class="p">(</span><span class="n">sha</span><span class="p">))</span>
3421N/A <span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: commit %d: sha is not a string</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
3421N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A <span class="p">}</span>
3421N/A
3421N/A <span class="n">commit</span> <span class="o">=</span> <span class="n">json_object_get</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s">&quot;commit&quot;</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json_is_object</span><span class="p">(</span><span class="n">commit</span><span class="p">))</span>
3421N/A <span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: commit %d: commit is not an object</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
3421N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A <span class="p">}</span>
3421N/A
3421N/A <span class="n">message</span> <span class="o">=</span> <span class="n">json_object_get</span><span class="p">(</span><span class="n">commit</span><span class="p">,</span> <span class="s">&quot;message&quot;</span><span class="p">);</span>
3421N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json_is_string</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
3421N/A <span class="p">{</span>
3421N/A <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;error: commit %d: message is not a string</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
3421N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
3421N/A <span class="p">}</span>
3421N/A<span class="p">...</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>And finally, we&#8217;ll print the first 8 characters of the commit ID and
3421N/Athe first line of the commit message. A C-style string is extracted
3421N/Afrom a JSON string using <a class="reference internal" href="apiref.html#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>:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre> <span class="n">message_text</span> <span class="o">=</span> <span class="n">json_string_value</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
3421N/A <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%.8s %.*s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
3421N/A <span class="n">json_string_value</span><span class="p">(</span><span class="n">id</span><span class="p">),</span>
3421N/A <span class="n">newline_offset</span><span class="p">(</span><span class="n">message_text</span><span class="p">),</span>
3421N/A <span class="n">message_text</span><span class="p">);</span>
3421N/A<span class="p">}</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>After sending the HTTP request, we decoded the JSON text using
3421N/A<a class="reference internal" href="apiref.html#c.json_loads" title="json_loads"><tt class="xref c c-func docutils literal"><span class="pre">json_loads()</span></tt></a>, remember? It returns a <em>new reference</em> to the
3421N/AJSON value it decodes. When we&#8217;re finished with the value, we&#8217;ll need
3421N/Ato decrease the reference count using <a class="reference internal" href="apiref.html#c.json_decref" title="json_decref"><tt class="xref c c-func docutils literal"><span class="pre">json_decref()</span></tt></a>. This way
3421N/AJansson can release the resources:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre><span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
3421N/A<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
3421N/A</pre></div>
3421N/A</div>
3421N/A<p>For a detailed explanation of reference counting in Jansson, see
3421N/A<a class="reference internal" href="apiref.html#apiref-reference-count"><em>Reference Count</em></a> in <a class="reference internal" href="apiref.html#apiref"><em>API Reference</em></a>.</p>
3421N/A<p>The program&#8217;s ready, let&#8217;s test it and view the latest commits in
3421N/AJansson&#8217;s repository:</p>
3421N/A<div class="highlight-c"><div class="highlight"><pre>$ /github_commits akheron jansson
3421N/A1581f26a Merge branch &#39;2.3&#39;
3421N/Aaabfd493 load: Change buffer_pos to be a size_t
3421N/Abd72efbd load: Avoid unexpected behaviour in macro expansion
3421N/Ae8fd3e30 Document and tweak json_load_callback()
3421N/A873eddaf Merge pull request #60 from rogerz/contrib
3421N/Abd2c0c73 Ignore the binary test_load_callback
3421N/A17a51a4b Merge branch &#39;2.3&#39;
3421N/A09c39adc Add json_load_callback to the list of exported symbols
3421N/Acbb80baf Merge pull request #57 from rogerz/contrib
3421N/A040bd7b0 Add json_load_callback()
3421N/A2637faa4 Make test stripping locale independent
3421N/A&lt;...&gt;
3421N/A</pre></div>
3421N/A</div>
3421N/A</div>
3421N/A<div class="section" id="conclusion">
3421N/A<h2>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">¶</a></h2>
3421N/A<p>In this tutorial, we implemented a program that fetches the latest
3421N/Acommits of a GitHub repository using the GitHub Repo Commits API.
3421N/AJansson was used to decode the JSON response and to extract the commit
3421N/Adata.</p>
3421N/A<p>This tutorial only covered a small part of Jansson. For example, we
3421N/Adid not create or manipulate JSON values at all. Proceed to
3421N/A<a class="reference internal" href="apiref.html#apiref"><em>API Reference</em></a> to explore all features of Jansson.</p>
3421N/A</div>
3421N/A</div>
3421N/A
3421N/A
3421N/A </div>
3421N/A </div>
3421N/A </div>
3421N/A <div class="sphinxsidebar">
3421N/A <div class="sphinxsidebarwrapper">
3421N/A <h3><a href="index.html">Table Of Contents</a></h3>
3421N/A <ul>
3421N/A<li><a class="reference internal" href="#">Tutorial</a><ul>
3421N/A<li><a class="reference internal" href="#the-github-repo-commits-api">The GitHub Repo Commits API</a></li>
3421N/A<li><a class="reference internal" href="#the-program">The Program</a></li>
3421N/A<li><a class="reference internal" href="#conclusion">Conclusion</a></li>
3421N/A</ul>
3421N/A</li>
3421N/A</ul>
3421N/A
3421N/A <h4>Previous topic</h4>
3421N/A <p class="topless"><a href="upgrading.html"
3421N/A title="previous chapter">Upgrading from 1.x</a></p>
3421N/A <h4>Next topic</h4>
3421N/A <p class="topless"><a href="conformance.html"
3421N/A title="next chapter">RFC Conformance</a></p>
3421N/A <h3>This Page</h3>
3421N/A <ul class="this-page-menu">
3421N/A <li><a href="_sources/tutorial.txt"
3421N/A rel="nofollow">Show Source</a></li>
3421N/A </ul>
3421N/A<div id="searchbox" style="display: none">
3421N/A <h3>Quick search</h3>
3421N/A <form class="search" action="search.html" method="get">
3421N/A <input type="text" name="q" />
3421N/A <input type="submit" value="Go" />
3421N/A <input type="hidden" name="check_keywords" value="yes" />
3421N/A <input type="hidden" name="area" value="default" />
3421N/A </form>
3421N/A <p class="searchtip" style="font-size: 90%">
3421N/A Enter search terms or a module, class or function name.
3421N/A </p>
3421N/A</div>
3421N/A<script type="text/javascript">$('#searchbox').show(0);</script>
3421N/A </div>
3421N/A </div>
3421N/A <div class="clearer"></div>
3421N/A </div>
3421N/A <div class="related">
3421N/A <h3>Navigation</h3>
3421N/A <ul>
3421N/A <li class="right" style="margin-right: 10px">
3421N/A <a href="genindex.html" title="General Index"
3421N/A >index</a></li>
3421N/A <li class="right" >
3421N/A <a href="conformance.html" title="RFC Conformance"
3421N/A >next</a> |</li>
3421N/A <li class="right" >
3421N/A <a href="upgrading.html" title="Upgrading from 1.x"
3421N/A >previous</a> |</li>
3421N/A <li><a href="index.html">Jansson 2.7 documentation</a> &raquo;</li>
3421N/A </ul>
3421N/A </div>
3421N/A <div class="footer">
3421N/A &copy; Copyright 2009-2014, Petri Lehtinen.
3421N/A Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
3421N/A </div>
3421N/A </body>
3421N/A</html>