2190
|
1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3 |
|
|
4 |
|
|
5 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
6 |
<head>
|
|
7 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
8 |
|
|
9 |
<title>Tutorial — Jansson 2.7 documentation</title>
|
|
10 |
|
|
11 |
<link rel="stylesheet" href="_static/default.css" type="text/css" />
|
|
12 |
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
13 |
|
|
14 |
<script type="text/javascript">
|
|
15 |
var DOCUMENTATION_OPTIONS = {
|
|
16 |
URL_ROOT: './',
|
|
17 |
VERSION: '2.7',
|
|
18 |
COLLAPSE_INDEX: false,
|
|
19 |
FILE_SUFFIX: '.html',
|
|
20 |
HAS_SOURCE: true
|
|
21 |
};
|
|
22 |
</script>
|
|
23 |
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
24 |
<script type="text/javascript" src="_static/underscore.js"></script>
|
|
25 |
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
26 |
<link rel="top" title="Jansson 2.7 documentation" href="index.html" />
|
|
27 |
<link rel="next" title="RFC Conformance" href="conformance.html" />
|
|
28 |
<link rel="prev" title="Upgrading from 1.x" href="upgrading.html" />
|
|
29 |
</head>
|
|
30 |
<body>
|
|
31 |
<div class="related">
|
|
32 |
<h3>Navigation</h3>
|
|
33 |
<ul>
|
|
34 |
<li class="right" style="margin-right: 10px">
|
|
35 |
<a href="genindex.html" title="General Index"
|
|
36 |
accesskey="I">index</a></li>
|
|
37 |
<li class="right" >
|
|
38 |
<a href="conformance.html" title="RFC Conformance"
|
|
39 |
accesskey="N">next</a> |</li>
|
|
40 |
<li class="right" >
|
|
41 |
<a href="upgrading.html" title="Upgrading from 1.x"
|
|
42 |
accesskey="P">previous</a> |</li>
|
|
43 |
<li><a href="index.html">Jansson 2.7 documentation</a> »</li>
|
|
44 |
</ul>
|
|
45 |
</div>
|
|
46 |
|
|
47 |
<div class="document">
|
|
48 |
<div class="documentwrapper">
|
|
49 |
<div class="bodywrapper">
|
|
50 |
<div class="body">
|
|
51 |
|
|
52 |
<div class="section" id="tutorial">
|
|
53 |
<span id="id1"></span><h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">ΒΆ</a></h1>
|
|
54 |
<p>In this tutorial, we create a program that fetches the latest commits
|
|
55 |
of 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
|
|
56 |
the result can be parsed using Jansson.</p>
|
|
57 |
<p>To stick to the the scope of this tutorial, we will only cover the the
|
|
58 |
parts of the program related to handling JSON data. For the best user
|
|
59 |
experience, the full source code is available:
|
|
60 |
<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
|
|
61 |
gcc), use the following command:</p>
|
|
62 |
<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>
|
|
63 |
</pre></div>
|
|
64 |
</div>
|
|
65 |
<p><a class="reference external" href="http://curl.haxx.se/">libcurl</a> is used to communicate over the web, so it is required to
|
|
66 |
compile the program.</p>
|
|
67 |
<p>The command line syntax is:</p>
|
|
68 |
<div class="highlight-c"><div class="highlight"><pre><span class="n">github_commits</span> <span class="n">USER</span> <span class="n">REPOSITORY</span>
|
|
69 |
</pre></div>
|
|
70 |
</div>
|
|
71 |
<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
|
|
72 |
name. Please note that the GitHub API is rate limited, so if you run
|
|
73 |
the program too many times within a short period of time, the sever
|
|
74 |
starts to respond with an error.</p>
|
|
75 |
<div class="section" id="the-github-repo-commits-api">
|
|
76 |
<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>
|
|
77 |
<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
|
|
78 |
URLs like <tt class="docutils literal"><span class="pre">https://api.github.com/repos/USER/REPOSITORY/commits</span></tt>,
|
|
79 |
where <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
|
|
80 |
of the repository whose commits are to be listed, respectively.</p>
|
|
81 |
<p>GitHub responds with a JSON array of the following form:</p>
|
|
82 |
<div class="highlight-none"><div class="highlight"><pre>[
|
|
83 |
{
|
|
84 |
"sha": "<the commit ID>",
|
|
85 |
"commit": {
|
|
86 |
"message": "<the commit message>",
|
|
87 |
<more fields, not important to this tutorial...>
|
|
88 |
},
|
|
89 |
<more fields...>
|
|
90 |
},
|
|
91 |
{
|
|
92 |
"sha": "<the commit ID>",
|
|
93 |
"commit": {
|
|
94 |
"message": "<the commit message>",
|
|
95 |
<more fields...>
|
|
96 |
},
|
|
97 |
<more fields...>
|
|
98 |
},
|
|
99 |
<more commits...>
|
|
100 |
]
|
|
101 |
</pre></div>
|
|
102 |
</div>
|
|
103 |
<p>In our program, the HTTP request is sent using the following
|
|
104 |
function:</p>
|
|
105 |
<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>
|
|
106 |
</pre></div>
|
|
107 |
</div>
|
|
108 |
<p>It takes the URL as a parameter, preforms a HTTP GET request, and
|
|
109 |
returns a newly allocated string that contains the response body. If
|
|
110 |
the request fails, an error message is printed to stderr and the
|
|
111 |
return 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
|
|
112 |
here.</p>
|
|
113 |
</div>
|
|
114 |
<div class="section" id="the-program">
|
|
115 |
<span id="tutorial-the-program"></span><h2>The Program<a class="headerlink" href="#the-program" title="Permalink to this headline">ΒΆ</a></h2>
|
|
116 |
<p>First the includes:</p>
|
|
117 |
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <string.h></span>
|
|
118 |
<span class="cp">#include <jansson.h></span>
|
|
119 |
</pre></div>
|
|
120 |
</div>
|
|
121 |
<p>Like all the programs using Jansson, we need to include
|
|
122 |
<tt class="file docutils literal"><span class="pre">jansson.h</span></tt>.</p>
|
|
123 |
<p>The following definitions are used to build the GitHub API request
|
|
124 |
URL:</p>
|
|
125 |
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#define URL_FORMAT "https:</span><span class="c1">//api.github.com/repos/%s/%s/commits"</span>
|
|
126 |
<span class="cp">#define URL_SIZE 256</span>
|
|
127 |
</pre></div>
|
|
128 |
</div>
|
|
129 |
<p>The following function is used when formatting the result to find the
|
|
130 |
first newline in the commit message:</p>
|
|
131 |
<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>
|
|
132 |
<span class="cm"> text if there's no newline */</span>
|
|
133 |
<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>
|
|
134 |
<span class="p">{</span>
|
|
135 |
<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">'\n'</span><span class="p">);</span>
|
|
136 |
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">newline</span><span class="p">)</span>
|
|
137 |
<span class="k">return</span> <span class="n">strlen</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
|
|
138 |
<span class="k">else</span>
|
|
139 |
<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>
|
|
140 |
<span class="p">}</span>
|
|
141 |
</pre></div>
|
|
142 |
</div>
|
|
143 |
<p>The main function follows. In the beginning, we first declare a bunch
|
|
144 |
of variables and check the command line parameters:</p>
|
|
145 |
<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>
|
|
146 |
<span class="p">{</span>
|
|
147 |
<span class="kt">size_t</span> <span class="n">i</span><span class="p">;</span>
|
|
148 |
<span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
|
|
149 |
<span class="kt">char</span> <span class="n">url</span><span class="p">[</span><span class="n">URL_SIZE</span><span class="p">];</span>
|
|
150 |
|
|
151 |
<span class="kt">json_t</span> <span class="o">*</span><span class="n">root</span><span class="p">;</span>
|
|
152 |
<span class="kt">json_error_t</span> <span class="n">error</span><span class="p">;</span>
|
|
153 |
|
|
154 |
<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>
|
|
155 |
<span class="p">{</span>
|
|
156 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"usage: %s USER REPOSITORY</span><span class="se">\n\n</span><span class="s">"</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
|
|
157 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"List commits at USER's REPOSITORY.</span><span class="se">\n\n</span><span class="s">"</span><span class="p">);</span>
|
|
158 |
<span class="k">return</span> <span class="mi">2</span><span class="p">;</span>
|
|
159 |
<span class="p">}</span>
|
|
160 |
</pre></div>
|
|
161 |
</div>
|
|
162 |
<p>Then we build the request URL using the user and repository names
|
|
163 |
given as command line parameters:</p>
|
|
164 |
<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>
|
|
165 |
</pre></div>
|
|
166 |
</div>
|
|
167 |
<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.
|
|
168 |
Now we’re ready to actually request the JSON data over the web:</p>
|
|
169 |
<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>
|
|
170 |
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">text</span><span class="p">)</span>
|
|
171 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
172 |
</pre></div>
|
|
173 |
</div>
|
|
174 |
<p>If an error occurs, our function <tt class="docutils literal"><span class="pre">request</span></tt> prints the error and
|
|
175 |
returns <em>NULL</em>, so it’s enough to just return 1 from the main
|
|
176 |
function.</p>
|
|
177 |
<p>Next we’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
|
|
178 |
as a response:</p>
|
|
179 |
<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">&</span><span class="n">error</span><span class="p">);</span>
|
|
180 |
<span class="n">free</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
|
|
181 |
|
|
182 |
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">root</span><span class="p">)</span>
|
|
183 |
<span class="p">{</span>
|
|
184 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: on line %d: %s</span><span class="se">\n</span><span class="s">"</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>
|
|
185 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
186 |
<span class="p">}</span>
|
|
187 |
</pre></div>
|
|
188 |
</div>
|
|
189 |
<p>We don’t need the JSON text anymore, so we can free the <tt class="docutils literal"><span class="pre">text</span></tt>
|
|
190 |
variable 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
|
|
191 |
returns <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>
|
|
192 |
structure given as the second parameter. In this case, our program
|
|
193 |
prints the error information out and returns 1 from the main function.</p>
|
|
194 |
<p>Now we’re ready to extract the data out of the decoded JSON response.
|
|
195 |
The structure of the response JSON was explained in section
|
|
196 |
<a class="reference internal" href="#tutorial-github-commits-api"><em>The GitHub Repo Commits API</em></a>.</p>
|
|
197 |
<p>We check that the returned value really is an array:</p>
|
|
198 |
<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>
|
|
199 |
<span class="p">{</span>
|
|
200 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: root is not an array</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
|
|
201 |
<span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
|
202 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
203 |
<span class="p">}</span>
|
|
204 |
</pre></div>
|
|
205 |
</div>
|
|
206 |
<p>Then we proceed to loop over all the commits in the array:</p>
|
|
207 |
<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"><</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>
|
|
208 |
<span class="p">{</span>
|
|
209 |
<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>
|
|
210 |
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">message_text</span><span class="p">;</span>
|
|
211 |
|
|
212 |
<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>
|
|
213 |
<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>
|
|
214 |
<span class="p">{</span>
|
|
215 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: commit data %d is not an object</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
|
|
216 |
<span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
|
217 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
218 |
<span class="p">}</span>
|
|
219 |
<span class="p">...</span>
|
|
220 |
</pre></div>
|
|
221 |
</div>
|
|
222 |
<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
|
|
223 |
array. First, we again declare some variables and then extract the
|
|
224 |
i’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>.
|
|
225 |
We also check that the resulting value is a JSON object.</p>
|
|
226 |
<p>Next we’ll extract the commit ID (a hexadecimal SHA-1 sum),
|
|
227 |
intermediate commit info object, and the commit message from that
|
|
228 |
object. We also do proper type checks:</p>
|
|
229 |
<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">"sha"</span><span class="p">);</span>
|
|
230 |
<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>
|
|
231 |
<span class="p">{</span>
|
|
232 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: commit %d: sha is not a string</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
|
|
233 |
<span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
|
234 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
235 |
<span class="p">}</span>
|
|
236 |
|
|
237 |
<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">"commit"</span><span class="p">);</span>
|
|
238 |
<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>
|
|
239 |
<span class="p">{</span>
|
|
240 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: commit %d: commit is not an object</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
|
|
241 |
<span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
|
242 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
243 |
<span class="p">}</span>
|
|
244 |
|
|
245 |
<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">"message"</span><span class="p">);</span>
|
|
246 |
<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>
|
|
247 |
<span class="p">{</span>
|
|
248 |
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"error: commit %d: message is not a string</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
|
|
249 |
<span class="n">json_decref</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
|
250 |
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
|
251 |
<span class="p">}</span>
|
|
252 |
<span class="p">...</span>
|
|
253 |
</pre></div>
|
|
254 |
</div>
|
|
255 |
<p>And finally, we’ll print the first 8 characters of the commit ID and
|
|
256 |
the first line of the commit message. A C-style string is extracted
|
|
257 |
from 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>
|
|
258 |
<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>
|
|
259 |
<span class="n">printf</span><span class="p">(</span><span class="s">"%.8s %.*s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
|
|
260 |
<span class="n">json_string_value</span><span class="p">(</span><span class="n">id</span><span class="p">),</span>
|
|
261 |
<span class="n">newline_offset</span><span class="p">(</span><span class="n">message_text</span><span class="p">),</span>
|
|
262 |
<span class="n">message_text</span><span class="p">);</span>
|
|
263 |
<span class="p">}</span>
|
|
264 |
</pre></div>
|
|
265 |
</div>
|
|
266 |
<p>After sending the HTTP request, we decoded the JSON text using
|
|
267 |
<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
|
|
268 |
JSON value it decodes. When we’re finished with the value, we’ll need
|
|
269 |
to 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
|
|
270 |
Jansson can release the resources:</p>
|
|
271 |
<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>
|
|
272 |
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
|
|
273 |
</pre></div>
|
|
274 |
</div>
|
|
275 |
<p>For a detailed explanation of reference counting in Jansson, see
|
|
276 |
<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>
|
|
277 |
<p>The program’s ready, let’s test it and view the latest commits in
|
|
278 |
Jansson’s repository:</p>
|
|
279 |
<div class="highlight-c"><div class="highlight"><pre>$ ./github_commits akheron jansson
|
|
280 |
1581f26a Merge branch '2.3'
|
|
281 |
aabfd493 load: Change buffer_pos to be a size_t
|
|
282 |
bd72efbd load: Avoid unexpected behaviour in macro expansion
|
|
283 |
e8fd3e30 Document and tweak json_load_callback()
|
|
284 |
873eddaf Merge pull request #60 from rogerz/contrib
|
|
285 |
bd2c0c73 Ignore the binary test_load_callback
|
|
286 |
17a51a4b Merge branch '2.3'
|
|
287 |
09c39adc Add json_load_callback to the list of exported symbols
|
|
288 |
cbb80baf Merge pull request #57 from rogerz/contrib
|
|
289 |
040bd7b0 Add json_load_callback()
|
|
290 |
2637faa4 Make test stripping locale independent
|
|
291 |
<...>
|
|
292 |
</pre></div>
|
|
293 |
</div>
|
|
294 |
</div>
|
|
295 |
<div class="section" id="conclusion">
|
|
296 |
<h2>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">ΒΆ</a></h2>
|
|
297 |
<p>In this tutorial, we implemented a program that fetches the latest
|
|
298 |
commits of a GitHub repository using the GitHub Repo Commits API.
|
|
299 |
Jansson was used to decode the JSON response and to extract the commit
|
|
300 |
data.</p>
|
|
301 |
<p>This tutorial only covered a small part of Jansson. For example, we
|
|
302 |
did not create or manipulate JSON values at all. Proceed to
|
|
303 |
<a class="reference internal" href="apiref.html#apiref"><em>API Reference</em></a> to explore all features of Jansson.</p>
|
|
304 |
</div>
|
|
305 |
</div>
|
|
306 |
|
|
307 |
|
|
308 |
</div>
|
|
309 |
</div>
|
|
310 |
</div>
|
|
311 |
<div class="sphinxsidebar">
|
|
312 |
<div class="sphinxsidebarwrapper">
|
|
313 |
<h3><a href="index.html">Table Of Contents</a></h3>
|
|
314 |
<ul>
|
|
315 |
<li><a class="reference internal" href="#">Tutorial</a><ul>
|
|
316 |
<li><a class="reference internal" href="#the-github-repo-commits-api">The GitHub Repo Commits API</a></li>
|
|
317 |
<li><a class="reference internal" href="#the-program">The Program</a></li>
|
|
318 |
<li><a class="reference internal" href="#conclusion">Conclusion</a></li>
|
|
319 |
</ul>
|
|
320 |
</li>
|
|
321 |
</ul>
|
|
322 |
|
|
323 |
<h4>Previous topic</h4>
|
|
324 |
<p class="topless"><a href="upgrading.html"
|
|
325 |
title="previous chapter">Upgrading from 1.x</a></p>
|
|
326 |
<h4>Next topic</h4>
|
|
327 |
<p class="topless"><a href="conformance.html"
|
|
328 |
title="next chapter">RFC Conformance</a></p>
|
|
329 |
<h3>This Page</h3>
|
|
330 |
<ul class="this-page-menu">
|
|
331 |
<li><a href="_sources/tutorial.txt"
|
|
332 |
rel="nofollow">Show Source</a></li>
|
|
333 |
</ul>
|
|
334 |
<div id="searchbox" style="display: none">
|
|
335 |
<h3>Quick search</h3>
|
|
336 |
<form class="search" action="search.html" method="get">
|
|
337 |
<input type="text" name="q" />
|
|
338 |
<input type="submit" value="Go" />
|
|
339 |
<input type="hidden" name="check_keywords" value="yes" />
|
|
340 |
<input type="hidden" name="area" value="default" />
|
|
341 |
</form>
|
|
342 |
<p class="searchtip" style="font-size: 90%">
|
|
343 |
Enter search terms or a module, class or function name.
|
|
344 |
</p>
|
|
345 |
</div>
|
|
346 |
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
347 |
</div>
|
|
348 |
</div>
|
|
349 |
<div class="clearer"></div>
|
|
350 |
</div>
|
|
351 |
<div class="related">
|
|
352 |
<h3>Navigation</h3>
|
|
353 |
<ul>
|
|
354 |
<li class="right" style="margin-right: 10px">
|
|
355 |
<a href="genindex.html" title="General Index"
|
|
356 |
>index</a></li>
|
|
357 |
<li class="right" >
|
|
358 |
<a href="conformance.html" title="RFC Conformance"
|
|
359 |
>next</a> |</li>
|
|
360 |
<li class="right" >
|
|
361 |
<a href="upgrading.html" title="Upgrading from 1.x"
|
|
362 |
>previous</a> |</li>
|
|
363 |
<li><a href="index.html">Jansson 2.7 documentation</a> »</li>
|
|
364 |
</ul>
|
|
365 |
</div>
|
|
366 |
<div class="footer">
|
|
367 |
© Copyright 2009-2014, Petri Lehtinen.
|
|
368 |
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
|
|
369 |
</div>
|
|
370 |
</body>
|
|
371 |
</html> |