tutorial.html revision 4139
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>Tutorial &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="RFC Conformance" href="conformance.html" />
4139N/A <link rel="prev" title="Upgrading from 1.x" href="upgrading.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="conformance.html" title="RFC Conformance"
4139N/A accesskey="N">next</a> |</li>
4139N/A <li class="right" >
4139N/A <a href="upgrading.html" title="Upgrading from 1.x"
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="tutorial">
4139N/A<span id="id1"></span><h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1>
4139N/A<p>In this tutorial, we create a program that fetches the latest commits
4139N/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
4139N/Athe result can be parsed using Jansson.</p>
4139N/A<p>To stick to the the scope of this tutorial, we will only cover the the
4139N/Aparts of the program related to handling JSON data. For the best user
4139N/Aexperience, the full source code is available:
4139N/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
4139N/Agcc), use the following command:</p>
4139N/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>
4139N/A</pre></div>
4139N/A</div>
4139N/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
4139N/Acompile the program.</p>
4139N/A<p>The command line syntax is:</p>
4139N/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>
4139N/A</pre></div>
4139N/A</div>
4139N/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
4139N/Aname. Please note that the GitHub API is rate limited, so if you run
4139N/Athe program too many times within a short period of time, the sever
4139N/Astarts to respond with an error.</p>
4139N/A<div class="section" id="the-github-repo-commits-api">
4139N/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>
4139N/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
4139N/AURLs like <tt class="docutils literal"><span class="pre">https://api.github.com/repos/USER/REPOSITORY/commits</span></tt>,
4139N/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
4139N/Aof the repository whose commits are to be listed, respectively.</p>
4139N/A<p>GitHub responds with a JSON array of the following form:</p>
4139N/A<div class="highlight-none"><div class="highlight"><pre>[
4139N/A {
4139N/A &quot;sha&quot;: &quot;&lt;the commit ID&gt;&quot;,
4139N/A &quot;commit&quot;: {
4139N/A &quot;message&quot;: &quot;&lt;the commit message&gt;&quot;,
4139N/A &lt;more fields, not important to this tutorial...&gt;
4139N/A },
4139N/A &lt;more fields...&gt;
4139N/A },
4139N/A {
4139N/A &quot;sha&quot;: &quot;&lt;the commit ID&gt;&quot;,
4139N/A &quot;commit&quot;: {
4139N/A &quot;message&quot;: &quot;&lt;the commit message&gt;&quot;,
4139N/A &lt;more fields...&gt;
4139N/A },
4139N/A &lt;more fields...&gt;
4139N/A },
4139N/A &lt;more commits...&gt;
4139N/A]
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>In our program, the HTTP request is sent using the following
4139N/Afunction:</p>
4139N/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>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>It takes the URL as a parameter, preforms a HTTP GET request, and
4139N/Areturns a newly allocated string that contains the response body. If
4139N/Athe request fails, an error message is printed to stderr and the
4139N/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
4139N/Ahere.</p>
4139N/A</div>
4139N/A<div class="section" id="the-program">
4139N/A<span id="tutorial-the-program"></span><h2>The Program<a class="headerlink" href="#the-program" title="Permalink to this headline">¶</a></h2>
4139N/A<p>First the includes:</p>
4139N/A<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include &lt;string.h&gt;</span>
4139N/A<span class="cp">#include &lt;jansson.h&gt;</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>Like all the programs using Jansson, we need to include
4139N/A<tt class="file docutils literal"><span class="pre">jansson.h</span></tt>.</p>
4139N/A<p>The following definitions are used to build the GitHub API request
4139N/AURL:</p>
4139N/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>
4139N/A<span class="cp">#define URL_SIZE 256</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>The following function is used when formatting the result to find the
4139N/Afirst newline in the commit message:</p>
4139N/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>
4139N/A<span class="cm"> text if there&#39;s no newline */</span>
4139N/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>
4139N/A<span class="p">{</span>
4139N/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>
4139N/A <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">newline</span><span class="p">)</span>
4139N/A <span class="k">return</span> <span class="n">strlen</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
4139N/A <span class="k">else</span>
4139N/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>
4139N/A<span class="p">}</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>The main function follows. In the beginning, we first declare a bunch
4139N/Aof variables and check the command line parameters:</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="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>
4139N/A<span class="p">{</span>
4139N/A <span class="kt">size_t</span> <span class="n">i</span><span class="p">;</span>
4139N/A <span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
4139N/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>
4139N/A
4139N/A <span class="kt">json_t</span> <span class="o">*</span><span class="n">root</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="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>
4139N/A <span class="p">{</span>
4139N/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>
4139N/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>
4139N/A <span class="k">return</span> <span class="mi">2</span><span class="p">;</span>
4139N/A <span class="p">}</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>Then we build the request URL using the user and repository names
4139N/Agiven as command line parameters:</p>
4139N/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>
4139N/A</pre></div>
4139N/A</div>
4139N/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.
4139N/ANow we&#8217;re ready to actually request the JSON data over the web:</p>
4139N/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>
4139N/A<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">text</span><span class="p">)</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>If an error occurs, our function <tt class="docutils literal"><span class="pre">request</span></tt> prints the error and
4139N/Areturns <em>NULL</em>, so it&#8217;s enough to just return 1 from the main
4139N/Afunction.</p>
4139N/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
4139N/Aas a response:</p>
4139N/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>
4139N/A<span class="n">free</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
4139N/A
4139N/A<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">root</span><span class="p">)</span>
4139N/A<span class="p">{</span>
4139N/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>
4139N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
4139N/A<span class="p">}</span>
4139N/A</pre></div>
4139N/A</div>
4139N/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>
4139N/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
4139N/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>
4139N/Astructure given as the second parameter. In this case, our program
4139N/Aprints the error information out and returns 1 from the main function.</p>
4139N/A<p>Now we&#8217;re ready to extract the data out of the decoded JSON response.
4139N/AThe structure of the response JSON was explained in section
4139N/A<a class="reference internal" href="#tutorial-github-commits-api"><em>The GitHub Repo Commits API</em></a>.</p>
4139N/A<p>We check that the returned value really is an array:</p>
4139N/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>
4139N/A<span class="p">{</span>
4139N/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>
4139N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
4139N/A<span class="p">}</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>Then we proceed to loop over all the commits in the array:</p>
4139N/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>
4139N/A<span class="p">{</span>
4139N/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>
4139N/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>
4139N/A
4139N/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>
4139N/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>
4139N/A <span class="p">{</span>
4139N/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>
4139N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><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>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
4139N/Aarray. First, we again declare some variables and then extract the
4139N/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>.
4139N/AWe also check that the resulting value is a JSON object.</p>
4139N/A<p>Next we&#8217;ll extract the commit ID (a hexadecimal SHA-1 sum),
4139N/Aintermediate commit info object, and the commit message from that
4139N/Aobject. We also do proper type checks:</p>
4139N/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>
4139N/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>
4139N/A <span class="p">{</span>
4139N/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>
4139N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
4139N/A <span class="p">}</span>
4139N/A
4139N/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>
4139N/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>
4139N/A <span class="p">{</span>
4139N/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>
4139N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
4139N/A <span class="p">}</span>
4139N/A
4139N/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>
4139N/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>
4139N/A <span class="p">{</span>
4139N/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>
4139N/A <span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
4139N/A <span class="k">return</span> <span class="mi">1</span><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>And finally, we&#8217;ll print the first 8 characters of the commit ID and
4139N/Athe first line of the commit message. A C-style string is extracted
4139N/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>
4139N/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>
4139N/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>
4139N/A <span class="n">json_string_value</span><span class="p">(</span><span class="n">id</span><span class="p">),</span>
4139N/A <span class="n">newline_offset</span><span class="p">(</span><span class="n">message_text</span><span class="p">),</span>
4139N/A <span class="n">message_text</span><span class="p">);</span>
4139N/A<span class="p">}</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>After sending the HTTP request, we decoded the JSON text using
4139N/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
4139N/AJSON value it decodes. When we&#8217;re finished with the value, we&#8217;ll need
4139N/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
4139N/AJansson can release the resources:</p>
4139N/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>
4139N/A<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
4139N/A</pre></div>
4139N/A</div>
4139N/A<p>For a detailed explanation of reference counting in Jansson, see
4139N/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>
4139N/A<p>The program&#8217;s ready, let&#8217;s test it and view the latest commits in
4139N/AJansson&#8217;s repository:</p>
4139N/A<div class="highlight-c"><div class="highlight"><pre>$ /github_commits akheron jansson
4139N/A1581f26a Merge branch &#39;2.3&#39;
4139N/Aaabfd493 load: Change buffer_pos to be a size_t
4139N/Abd72efbd load: Avoid unexpected behaviour in macro expansion
4139N/Ae8fd3e30 Document and tweak json_load_callback()
4139N/A873eddaf Merge pull request #60 from rogerz/contrib
4139N/Abd2c0c73 Ignore the binary test_load_callback
4139N/A17a51a4b Merge branch &#39;2.3&#39;
4139N/A09c39adc Add json_load_callback to the list of exported symbols
4139N/Acbb80baf Merge pull request #57 from rogerz/contrib
4139N/A040bd7b0 Add json_load_callback()
4139N/A2637faa4 Make test stripping locale independent
4139N/A&lt;...&gt;
4139N/A</pre></div>
4139N/A</div>
4139N/A</div>
4139N/A<div class="section" id="conclusion">
4139N/A<h2>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">¶</a></h2>
4139N/A<p>In this tutorial, we implemented a program that fetches the latest
4139N/Acommits of a GitHub repository using the GitHub Repo Commits API.
4139N/AJansson was used to decode the JSON response and to extract the commit
4139N/Adata.</p>
4139N/A<p>This tutorial only covered a small part of Jansson. For example, we
4139N/Adid not create or manipulate JSON values at all. Proceed to
4139N/A<a class="reference internal" href="apiref.html#apiref"><em>API Reference</em></a> to explore all features of Jansson.</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="#">Tutorial</a><ul>
4139N/A<li><a class="reference internal" href="#the-github-repo-commits-api">The GitHub Repo Commits API</a></li>
4139N/A<li><a class="reference internal" href="#the-program">The Program</a></li>
4139N/A<li><a class="reference internal" href="#conclusion">Conclusion</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="upgrading.html"
4139N/A title="previous chapter">Upgrading from 1.x</a></p>
4139N/A <h4>Next topic</h4>
4139N/A <p class="topless"><a href="conformance.html"
4139N/A title="next chapter">RFC Conformance</a></p>
4139N/A <h3>This Page</h3>
4139N/A <ul class="this-page-menu">
4139N/A <li><a href="_sources/tutorial.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="conformance.html" title="RFC Conformance"
4139N/A >next</a> |</li>
4139N/A <li class="right" >
4139N/A <a href="upgrading.html" title="Upgrading from 1.x"
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>