1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>JSON GVariant Integration</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="JSON-GLib Reference Manual">
8 <link rel="up" href="json-advanced.html" title="Part IV. JSON-GLib Advanced API">
9 <link rel="prev" href="json-glib-Boxed-Types-Serialization.html" title="Boxed Types Serialization">
10 <link rel="next" href="json-tools.html" title="Part V. JSON-GLib Additional Reference">
11 <meta name="generator" content="GTK-Doc V1.18.1 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
17 <td><a accesskey="p" href="json-glib-Boxed-Types-Serialization.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
18 <td><a accesskey="u" href="json-advanced.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
19 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
20 <th width="100%" align="center">JSON-GLib Reference Manual</th>
21 <td><a accesskey="n" href="json-tools.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
23 <tr><td colspan="5" class="shortcuts">
24 <a href="#json-glib-JSON-GVariant-Integration.synopsis" class="shortcut">Top</a>
26 <a href="#json-glib-JSON-GVariant-Integration.description" class="shortcut">Description</a>
29 <div class="refentry">
30 <a name="json-glib-JSON-GVariant-Integration"></a><div class="titlepage"></div>
31 <div class="refnamediv"><table width="100%"><tr>
33 <h2><span class="refentrytitle"><a name="json-glib-JSON-GVariant-Integration.top_of_page"></a>JSON GVariant Integration</span></h2>
34 <p>JSON GVariant Integration — Serialize and deserialize GVariant types</p>
36 <td valign="top" align="right"></td>
38 <div class="refsynopsisdiv">
39 <a name="json-glib-JSON-GVariant-Integration.synopsis"></a><h2>Synopsis</h2>
40 <pre class="synopsis"><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-serialize" title="json_gvariant_serialize ()">json_gvariant_serialize</a> (<em class="parameter"><code><span class="type">GVariant</span> *variant</code></em>);
41 <span class="returnvalue">gchar</span> * <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-serialize-data" title="json_gvariant_serialize_data ()">json_gvariant_serialize_data</a> (<em class="parameter"><code><span class="type">GVariant</span> *variant</code></em>,
42 <em class="parameter"><code><span class="type">gsize</span> *length</code></em>);
43 <span class="returnvalue">GVariant</span> * <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize" title="json_gvariant_deserialize ()">json_gvariant_deserialize</a> (<em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *json_node</code></em>,
44 <em class="parameter"><code>const <span class="type">gchar</span> *signature</code></em>,
45 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);
46 <span class="returnvalue">GVariant</span> * <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize-data" title="json_gvariant_deserialize_data ()">json_gvariant_deserialize_data</a> (<em class="parameter"><code>const <span class="type">gchar</span> *json</code></em>,
47 <em class="parameter"><code><span class="type">gssize</span> length</code></em>,
48 <em class="parameter"><code>const <span class="type">gchar</span> *signature</code></em>,
49 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);
52 <div class="refsect1">
53 <a name="json-glib-JSON-GVariant-Integration.description"></a><h2>Description</h2>
55 Use <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-serialize" title="json_gvariant_serialize ()"><code class="function">json_gvariant_serialize()</code></a> and <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-serialize-data" title="json_gvariant_serialize_data ()"><code class="function">json_gvariant_serialize_data()</code></a> to
56 convert from any <span class="type">GVariant</span> value to a <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> tree or its string
60 Use <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize" title="json_gvariant_deserialize ()"><code class="function">json_gvariant_deserialize()</code></a> and <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize-data" title="json_gvariant_deserialize_data ()"><code class="function">json_gvariant_deserialize_data()</code></a> to
61 obtain the <span class="type">GVariant</span> value from a <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> tree or directly from a JSON
63 Since many <span class="type">GVariant</span> data types cannot be directly represented as
64 JSON, a <span class="type">GVariant</span> type string (signature) should be provided to these
65 methods in order to obtain a correct, type-contrained result.
66 If no signature is provided, conversion can still be done, but the
67 resulting <span class="type">GVariant</span> value will be "guessed" from the JSON data types,
68 according to the following table:
73 <a name="id2971653"></a><p class="title"><b>Table 2. Default JSON to GVariant conversion (without signature constrains)</b></p>
74 <div class="table-contents"><table summary="Default JSON to GVariant conversion (without signature constrains)" border="1">
80 <th align="left">JSON</th>
81 <th align="left">GVariant</th>
85 <th align="left">string</th>
86 <th align="left">string (s)</th>
89 <th align="left">int64</th>
90 <th align="left">int64 (x)</th>
93 <th align="left">boolean</th>
94 <th align="left">boolean (b)</th>
97 <th align="left">double</th>
98 <th align="left">double (d)</th>
101 <th align="left">array</th>
102 <th align="left">array of variants (av)</th>
105 <th align="left">object</th>
106 <th align="left">dictionary of string-variant (a{sv})</th>
109 <th align="left">null</th>
110 <th align="left">maybe variant (mv)</th>
115 <p><br class="table-break">
118 <div class="refsect1">
119 <a name="json-glib-JSON-GVariant-Integration.details"></a><h2>Details</h2>
120 <div class="refsect2">
121 <a name="json-gvariant-serialize"></a><h3>json_gvariant_serialize ()</h3>
122 <pre class="programlisting"><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * json_gvariant_serialize (<em class="parameter"><code><span class="type">GVariant</span> *variant</code></em>);</pre>
124 Converts <em class="parameter"><code>variant</code></em> to a JSON tree.
126 <div class="variablelist"><table border="0">
127 <col align="left" valign="top">
130 <td><p><span class="term"><em class="parameter"><code>variant</code></em> :</span></p></td>
131 <td>A <span class="type">GVariant</span> to convert</td>
134 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
135 <td>A <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> representing the root of the
136 JSON data structure obtained from <em class="parameter"><code>variant</code></em>. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
141 <p class="since">Since 0.14</p>
144 <div class="refsect2">
145 <a name="json-gvariant-serialize-data"></a><h3>json_gvariant_serialize_data ()</h3>
146 <pre class="programlisting"><span class="returnvalue">gchar</span> * json_gvariant_serialize_data (<em class="parameter"><code><span class="type">GVariant</span> *variant</code></em>,
147 <em class="parameter"><code><span class="type">gsize</span> *length</code></em>);</pre>
149 Converts <em class="parameter"><code>variant</code></em> to its JSON encoded string representation. This method
150 is actually a helper function. It uses <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-serialize" title="json_gvariant_serialize ()"><code class="function">json_gvariant_serialize()</code></a> to obtain the
151 JSON tree, and then <a class="link" href="JsonGenerator.html" title="JsonGenerator"><span class="type">JsonGenerator</span></a> to stringify it.
153 <div class="variablelist"><table border="0">
154 <col align="left" valign="top">
157 <td><p><span class="term"><em class="parameter"><code>variant</code></em> :</span></p></td>
158 <td>A <span class="type">GVariant</span> to convert</td>
161 <td><p><span class="term"><em class="parameter"><code>length</code></em> :</span></p></td>
162 <td>Return location for the length of the returned
163 string, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>. <span class="annotation">[<acronym title="Parameter for returning results. Default is transfer full."><span class="acronym">out</span></acronym>][<acronym title="NULL is ok, both for passing and for returning."><span class="acronym">allow-none</span></acronym>]</span>
167 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
168 <td>The JSON encoded string corresponding to
169 <em class="parameter"><code>variant</code></em>. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
174 <p class="since">Since 0.14</p>
177 <div class="refsect2">
178 <a name="json-gvariant-deserialize"></a><h3>json_gvariant_deserialize ()</h3>
179 <pre class="programlisting"><span class="returnvalue">GVariant</span> * json_gvariant_deserialize (<em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *json_node</code></em>,
180 <em class="parameter"><code>const <span class="type">gchar</span> *signature</code></em>,
181 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);</pre>
183 Converts a JSON data structure to a GVariant value using <em class="parameter"><code>signature</code></em> to
184 resolve ambiguous data types. If no error occurs, the resulting <span class="type">GVariant</span>
185 is guaranteed to conform to <em class="parameter"><code>signature</code></em>.
188 If <em class="parameter"><code>signature</code></em> is not <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a> but does not represent a valid GVariant type
189 string, <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a> is returned and error is set to <code class="literal">G_IO_ERROR_INVALID_ARGUMENT</code>.
190 If a <em class="parameter"><code>signature</code></em> is provided but the JSON structure cannot be mapped to it,
191 <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a> is returned and error is set to <code class="literal">G_IO_ERROR_INVALID_DATA</code>.
192 If <em class="parameter"><code>signature</code></em> is <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>, the conversion is done based strictly on the types
195 <div class="variablelist"><table border="0">
196 <col align="left" valign="top">
199 <td><p><span class="term"><em class="parameter"><code>json_node</code></em> :</span></p></td>
200 <td>A <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> to convert</td>
203 <td><p><span class="term"><em class="parameter"><code>signature</code></em> :</span></p></td>
204 <td>A valid <span class="type">GVariant</span> type string, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>. <span class="annotation">[<acronym title="NULL is ok, both for passing and for returning."><span class="acronym">allow-none</span></acronym>]</span>
208 <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
209 <td>A pointer to a <span class="type">GError</span>
213 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
214 <td>A newly created <span class="type">GVariant</span> compliant with
215 <em class="parameter"><code>signature</code></em>, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a> on error. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
220 <p class="since">Since 0.14</p>
223 <div class="refsect2">
224 <a name="json-gvariant-deserialize-data"></a><h3>json_gvariant_deserialize_data ()</h3>
225 <pre class="programlisting"><span class="returnvalue">GVariant</span> * json_gvariant_deserialize_data (<em class="parameter"><code>const <span class="type">gchar</span> *json</code></em>,
226 <em class="parameter"><code><span class="type">gssize</span> length</code></em>,
227 <em class="parameter"><code>const <span class="type">gchar</span> *signature</code></em>,
228 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);</pre>
230 Converts a JSON string to a <span class="type">GVariant</span> value. This method works exactly
231 like <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize" title="json_gvariant_deserialize ()"><code class="function">json_gvariant_deserialize()</code></a>, but takes a JSON encoded string instead.
232 The string is first converted to a <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> using <a class="link" href="JsonParser.html" title="JsonParser"><span class="type">JsonParser</span></a>, and then
233 <a class="link" href="json-glib-JSON-GVariant-Integration.html#json-gvariant-deserialize" title="json_gvariant_deserialize ()"><code class="function">json_gvariant_deserialize()</code></a> is called.
235 <div class="variablelist"><table border="0">
236 <col align="left" valign="top">
239 <td><p><span class="term"><em class="parameter"><code>json</code></em> :</span></p></td>
240 <td>A JSON data string</td>
243 <td><p><span class="term"><em class="parameter"><code>length</code></em> :</span></p></td>
244 <td>The length of <em class="parameter"><code>json</code></em>, or -1 if <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>-terminated</td>
247 <td><p><span class="term"><em class="parameter"><code>signature</code></em> :</span></p></td>
248 <td>A valid <span class="type">GVariant</span> type string, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>. <span class="annotation">[<acronym title="NULL is ok, both for passing and for returning."><span class="acronym">allow-none</span></acronym>]</span>
252 <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
253 <td>A pointer to a <span class="type">GError</span>
257 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
258 <td>A newly created <span class="type">GVariant</span> compliant with
259 <em class="parameter"><code>signature</code></em>, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a> on error. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
264 <p class="since">Since 0.14</p>
270 Generated by GTK-Doc V1.18.1</div>