Tizen 2.1 base
[platform/upstream/glib2.0.git] / docs / reference / glib / gvariant-text.xml
1 <?xml version='1.0' encoding='utf-8'?>
2
3 <refentry id='gvariant-text'>
4  <refmeta>
5   <refentrytitle>GVariant Text Format</refentrytitle>
6  </refmeta>
7  <refnamediv>
8   <refname>GVariant Text Format</refname>
9   <refpurpose>textual representation of GVariants</refpurpose>
10  </refnamediv>
11
12  <refsect1>
13   <title>GVariant Text Format</title>
14
15   <para>
16    This page attempts to document the GVariant text format as produced by
17    <link linkend='g-variant-print'><function>g_variant_print()</function></link> and parsed by the
18    <link linkend='g-variant-parse'><function>g_variant_parse()</function></link> family of functions.  In most
19    cases the style closely resembles the formatting of literals in Python but there are some additions and
20    exceptions.
21   </para>
22
23   <para>
24    The functions that deal with GVariant text format absolutely always deal in utf-8.  Conceptually, GVariant
25    text format is a string of Unicode characters -- not bytes.  Non-ASCII but otherwise printable Unicode
26    characters are not treated any differently from normal ASCII characters.
27   </para>
28
29   <para>
30    The parser makes two passes.  The purpose of the first pass is to determine the type of the value being
31    parsed.  The second pass does the actual parsing.  Based on the fact that all elements in an array have to
32    have the same type, GVariant is able to make some deductions that would not otherwise be possible.  As an
33    example:
34
35    <informalexample><programlisting>[[1, 2, 3], [4, 5, 6]]</programlisting></informalexample>
36
37    is parsed as an array of arrays of integers (type '<literal>aai</literal>'), but
38
39    <informalexample><programlisting>[[1, 2, 3], [4, 5, 6.0]]</programlisting></informalexample>
40
41    is parsed as a array of arrays of doubles (type '<literal>aad</literal>').
42   </para>
43
44   <para>
45    As another example, GVariant is able to determine that
46
47    <informalexample><programlisting>["hello", nothing]</programlisting></informalexample>
48
49    is an array of maybe strings (type '<literal>ams</literal>').
50   </para>
51
52   <para>
53    What the parser accepts as valid input is dependent on context.  The API permits for out-of-band type
54    information to be supplied to the parser (which will change its behaviour).  This can be seen in the
55    GSettings and GDBus command line utilities where the type information is available from the schema or the
56    remote introspection information.  The additional information can cause parses to succeed when they would not
57    otherwise have been able to (by resolving ambiguous type information) or can cause them to fail (due to
58    conflicting type information).  Unless stated otherwise, the examples given in this section assume that no
59    out-of-band type data has been given to the parser.
60   </para>
61  </refsect1>
62
63  <refsect1>
64   <title>Syntax Summary</title>
65
66   <para>
67    The following table describes the rough meaning of symbols that may appear inside GVariant text format.
68    Each symbol is described in detail in its own section, including usage examples.
69   </para>
70
71   <informaltable>
72    <tgroup cols='2'>
73     <colspec colname='col_0'/>
74     <colspec colname='col_1'/>
75     <tbody>
76
77      <row rowsep='1'>
78       <entry colsep='1' rowsep='1'>
79        <para>
80         <emphasis role='strong'>Symbol</emphasis>
81        </para>
82       </entry>
83       <entry colsep='1' rowsep='1'>
84        <para>
85          <emphasis role='strong'>Meaning</emphasis>
86        </para>
87       </entry>
88      </row>
89
90      <row rowsep='1'>
91       <entry colsep='1' rowsep='1'>
92        <para>
93         <emphasis role='strong'><literal>true</literal></emphasis>,
94         <emphasis role='strong'><literal>false</literal></emphasis>
95        </para>
96       </entry>
97       <entry colsep='1' rowsep='1'>
98        <para>
99          <link linkend='gvariant-text-booleans'>Booleans</link>.
100        </para>
101       </entry>
102      </row>
103
104      <row rowsep='1'>
105       <entry colsep='1' rowsep='1'>
106        <para>
107         <emphasis role='strong'><literal>""</literal></emphasis>,
108         <emphasis role='strong'><literal>''</literal></emphasis>
109        </para>
110       </entry>
111       <entry colsep='1' rowsep='1'>
112        <para>
113          String literal.  See <link linkend='gvariant-text-strings'>Strings</link> below.
114        </para>
115       </entry>
116      </row>
117
118      <row rowsep='1'>
119       <entry colsep='1' rowsep='1'>
120        <para>
121         numbers
122        </para>
123       </entry>
124       <entry colsep='1' rowsep='1'>
125        <para>
126         See <link linkend='gvariant-text-numbers'>Numbers</link> below.
127        </para>
128       </entry>
129      </row>
130
131      <row rowsep='1'>
132       <entry colsep='1' rowsep='1'>
133        <para>
134         <emphasis role='strong'><literal>()</literal></emphasis>
135        </para>
136       </entry>
137       <entry colsep='1' rowsep='1'>
138        <para>
139         <link linkend='gvariant-text-tuples'>Tuples</link>.
140        </para>
141       </entry>
142      </row>
143
144      <row rowsep='1'>
145       <entry colsep='1' rowsep='1'>
146        <para>
147         <emphasis role='strong'><literal>[]</literal></emphasis>
148        </para>
149       </entry>
150       <entry colsep='1' rowsep='1'>
151        <para>
152         <link linkend='gvariant-text-arrays'>Arrays</link>.
153        </para>
154       </entry>
155      </row>
156
157      <row rowsep='1'>
158       <entry colsep='1' rowsep='1'>
159        <para>
160         <emphasis role='strong'><literal>{}</literal></emphasis>
161        </para>
162       </entry>
163       <entry colsep='1' rowsep='1'>
164        <para>
165         <link linkend='gvariant-text-dictionaries'>Dictionaries and Dictionary Entries</link>.
166        </para>
167       </entry>
168      </row>
169
170      <row rowsep='1'>
171       <entry colsep='1' rowsep='1'>
172        <para>
173         <emphasis role='strong'><literal>&lt;&gt;</literal></emphasis>
174        </para>
175       </entry>
176       <entry colsep='1' rowsep='1'>
177        <para>
178         <link linkend='gvariant-text-variants'>Variants</link>.
179        </para>
180       </entry>
181      </row>
182
183      <row rowsep='1'>
184       <entry colsep='1' rowsep='1'>
185        <para>
186         <emphasis role='strong'><literal>just</literal></emphasis>,
187         <emphasis role='strong'><literal>nothing</literal></emphasis>
188        </para>
189       </entry>
190       <entry colsep='1' rowsep='1'>
191        <para>
192         <link linkend='gvariant-text-maybe-types'>Maybe Types</link>.
193        </para>
194       </entry>
195      </row>
196
197      <row rowsep='1'>
198       <entry colsep='1' rowsep='1'>
199        <para>
200         <emphasis role='strong'><literal>@</literal></emphasis>
201        </para>
202       </entry>
203       <entry colsep='1' rowsep='1'>
204        <para>
205         <link linkend='gvariant-text-type-annotations'>Type Annotations</link>.
206        </para>
207       </entry>
208      </row>
209
210      <row rowsep='1'>
211       <entry colsep='1' rowsep='1'>
212        <para>
213         type keywords
214        </para>
215       </entry>
216       <entry colsep='1' rowsep='1'>
217        <para>
218         <literal>boolean</literal>,
219         <literal>byte</literal>,
220         <literal>int16</literal>,
221         <literal>uint16</literal>,
222         <literal>int32</literal>,
223         <literal>uint32</literal>,
224         <literal>handle</literal>,
225         <literal>int64</literal>,
226         <literal>uint64</literal>,
227         <literal>double</literal>,
228         <literal>string</literal>,
229         <literal>objectpath</literal>,
230         <literal>signature</literal>
231        </para>
232        <para>
233         See <link linkend='gvariant-text-type-annotations'>Type Annotations</link> below.
234        </para>
235       </entry>
236      </row>
237
238      <row rowsep='1'>
239       <entry colsep='1' rowsep='1'>
240        <para>
241         <emphasis role='strong'><literal>b""</literal></emphasis>,
242         <emphasis role='strong'><literal>b''</literal></emphasis>
243        </para>
244       </entry>
245       <entry colsep='1' rowsep='1'>
246        <para>
247         <link linkend='gvariant-text-bytestrings'>Bytestrings</link>.
248        </para>
249       </entry>
250      </row>
251
252      <row rowsep='1'>
253       <entry colsep='1' rowsep='1'>
254        <para>
255         <emphasis role='strong'><literal>%</literal></emphasis>
256        </para>
257       </entry>
258       <entry colsep='1' rowsep='1'>
259        <para>
260         <link linkend='gvariant-text-positional'>Positional Parameters</link>.
261        </para>
262       </entry>
263      </row>
264     </tbody>
265    </tgroup>
266   </informaltable>
267
268   <refsect2 id='gvariant-text-booleans'>
269    <title>Booleans</title>
270    <para>
271     The strings <literal>true</literal> and <literal>false</literal> are parsed as booleans.  This is the only
272     way to specify a boolean value.
273    </para>
274   </refsect2>
275
276   <refsect2 id='gvariant-text-strings'>
277    <title>Strings</title>
278    <para>
279     Strings literals must be quoted using <literal>""</literal> or <literal>''</literal>.  The two are
280     completely equivalent (except for the fact that each one is unable to contain itself unescaped).
281    </para>
282    <para>
283     Strings are Unicode strings with no particular encoding.  For example, to specify the character
284     <literal>é</literal>, you just write <literal>'é'</literal>.  You could also give the Unicode codepoint of
285     that character (U+E9) as the escape sequence <literal>'\u00e9'</literal>.  Since the strings are pure
286     Unicode, you should not attempt to encode the utf-8 byte sequence corresponding to the string using escapes;
287     it won't work and you'll end up with the individual characters corresponding to each byte.
288    </para>
289    <para>
290     Unicode escapes of the form <literal>\uxxxx</literal> and <literal>\Uxxxxxxxx</literal> are supported, in
291     hexidecimal.  The usual control sequence escapes <literal>\a</literal>, <literal>\b</literal>,
292     <literal>\f</literal>, <literal>\n</literal>, <literal>\r</literal>, <literal>\t</literal> and
293     <literal>\v</literal> are supported.  Additionally, a <literal>\</literal> before a newline character causes
294     the newline to be ignored.  Finally, any other character following <literal>\</literal> is copied literally
295     (for example, <literal>\"</literal> or <literal>\\</literal>) but for forwards compatibility with future
296     additions you should only use this feature when necessary for escaping backslashes or quotes.
297    </para>
298    <para>
299     The usual octal and hexidecimal escapes <literal>\0nnn</literal> and <literal>\xnn</literal> are not
300     supported here.  Those escapes are used to encode byte values and GVariant strings are Unicode.
301    </para>
302    <para>
303     Single-character strings are not interpreted as bytes.  Bytes must be specified by their numerical value.
304    </para>
305   </refsect2>
306
307   <refsect2 id='gvariant-text-numbers'>
308    <title>Numbers</title>
309    <para>
310     Numbers are given by default as decimal values.  Octal and hex values can be given in the usual way (by
311     prefixing with <literal>0</literal> or <literal>0x</literal>).  Note that GVariant considers bytes to be
312     unsigned integers and will print them as a two digit hexidecimal number by default.
313    </para>
314    <para>
315     Floating point numbers can also be given in the usual ways, including scientific and hexidecimal notations.
316    </para>
317    <para>
318     For lack of additional information, integers will be parsed as int32 values by default.  If the number has a
319     point or an 'e' in it, then it will be parsed as a double precision floating point number by default.  If
320     type information is available (either explicitly or inferred) then that type will be used instead.
321    </para>
322    <para>
323     Some examples:
324    </para>
325    <para>
326     <literal>5</literal> parses as the int32 value five.
327    </para>
328    <para>
329     <literal>37.5</literal> parses as a floating point value.
330    </para>
331    <para>
332     <literal>3.75e1</literal> parses the same as the value above.
333    </para>
334    <para>
335     <literal>uint64 7</literal> parses seven as a uint64.
336     See <link linkend='gvariant-text-type-annotations'>Type Annotations</link>.
337    </para>
338   </refsect2>
339
340   <refsect2 id='gvariant-text-tuples'>
341    <title>Tuples</title>
342    <para>
343     Tuples are formed using the same syntax as Python.  Here are some examples:
344    </para>
345    <para>
346     <literal>()</literal> parses as the empty tuple.
347    </para>
348    <para>
349     <literal>(5,)</literal> is a tuple containing a single value.
350    </para>
351    <para>
352     <literal>("hello", 42)</literal> is a pair.  Note that values of different types are permitted.
353    </para>
354   </refsect2>
355
356   <refsect2 id='gvariant-text-arrays'>
357    <title>Arrays</title>
358    <para>
359     Arrays are formed using the same syntax as Python uses for lists (which is arguably the term that GVariant
360     should have used).  Note that, unlike Python lists, GVariant arrays are statically typed.  This has two
361     implications.
362    </para>
363    <para>
364     First, all items in the array must have the same type.  Second, the type of the array must be known, even in
365     the case that it is empty.  This means that (unless there is some other way to infer it) type information
366     will need to be given explicitly for empty arrays.
367    </para>
368    <para>
369     The parser is able to infer some types based on the fact that all items in an array must have the same type.
370     See the examples below:
371    </para>
372    <para>
373     <literal>[1]</literal> parses (without additional type information) as a one-item array of signed integers.
374    </para>
375    <para>
376     <literal>[1, 2, 3]</literal> parses (similarly) as a three-item array.
377    </para>
378    <para>
379     <literal>[1, 2, 3.0]</literal> parses as an array of doubles.  This is the most simple case of the type
380     inferencing in action.
381    </para>
382    <para>
383     <literal>[(1, 2), (3, 4.0)]</literal> causes the 2 to also be parsed as a double (but the 1 and 4 are still
384     integers).
385    </para>
386    <para>
387     <literal>["", nothing]</literal> parses as an array of maybe strings.  The presence of
388     "<literal>nothing</literal>" clearly implies that the array elements are nullable.
389    </para>
390    <para>
391     <literal>[[], [""]]</literal> will parse properly because the type of the first (empty) array can be
392     inferred to be equal to the type of the second array (both are arrays of strings).
393    </para>
394    <para>
395     <literal>[b'hello', []]</literal> looks odd but will parse properly.
396     See <link linkend='gvariant-text-bytestrings'>Bytestrings</link>
397    </para>
398    <para>
399     And some examples of errors:
400    </para>
401    <para>
402     <literal>["hello", 42]</literal> fails to parse due to conflicting types.
403    </para>
404    <para>
405     <literal>[]</literal> will fail to parse without additional type information.
406    </para>
407   </refsect2>
408
409   <refsect2 id='gvariant-text-dictionaries'>
410    <title>Dictionaries and Dictionary Entries</title>
411    <para>
412     Dictionaries and dictionary entries are both specified using the <literal>{}</literal> characters.
413    </para>
414    <para>
415     The dictionary syntax is more commonly used.  This is what the printer elects to use in the normal case of
416     dictionary entries appearing in an array (aka "a dictionary").  The separate syntax for dictionary entries
417     is typically only used for when the entries appear on their own, outside of an array (which is valid but
418     unusual).  Of course, you are free to use the dictionary entry syntax within arrays but there is no good
419     reason to do so (and the printer itself will never do so).  Note that, as with arrays, the type of empty
420     dictionaries must be established (either explicitly or through inference).
421    </para>
422    <para>
423     The dictionary syntax is the same as Python's syntax for dictionaries.  Some examples:
424    </para>
425    <para>
426     <literal>@a{sv} {}</literal> parses as the empty dictionary of everyone's favourite type.
427    </para>
428    <para>
429     <literal>@a{sv} []</literal> is the same as above (owing to the fact that dictionaries are really arrays).
430    </para>
431    <para>
432     <literal>{1: "one", 2: "two", 3: "three"}</literal> parses as a dictionary mapping integers to strings.
433    </para>
434    <para>
435     The dictionary entry syntax looks just like a pair (2-tuple) that uses braces instead of parens.  The
436     presence of a comma immediately following the key differentiates it from the dictionary syntax (which
437     features a colon after the first key).  Some examples:
438    </para>
439    <para>
440     <literal>{1, "one"}</literal> is a free-standing dictionary entry that can be parsed on its own or as part
441     of another container value.
442    </para>
443    <para>
444     <literal>[{1, "one"}, {2, "two"}, {3, "three"}]</literal> is exactly equivalent to the dictionary example
445     given above.
446    </para>
447   </refsect2>
448
449   <refsect2 id='gvariant-text-variants'>
450    <title>Variants</title>
451    <para>
452     Variants are denoted using angle brackets (aka "XML brackets"), <literal>&lt;&gt;</literal>.  They may not
453     be omitted.
454    </para>
455    <para>
456     Using <literal>&lt;&gt;</literal> effectively disrupts the type inferencing that occurs between array
457     elements.  This can have positive and negative effects.
458    </para>
459    <para>
460     <literal>[&lt;"hello"&gt;, &lt;42&gt;]</literal> will parse whereas <literal>["hello", 42]</literal> would
461     not.
462    </para>
463    <para>
464     <literal>[&lt;['']&gt;, &lt;[]&gt;]</literal> will fail to parse even though <literal>[[''], []]</literal>
465     parses successfully.  You would need to specify <literal>[&lt;['']&gt;, &lt;@as []&gt;]</literal>.
466    </para>
467    <para>
468     <literal>{"title": &lt;"frobit"&gt;, "enabled": &lt;true&gt;, width: &lt;800&gt;}</literal> is an example of
469     perhaps the most pervasive use of both dictionaries and variants.
470    </para>
471   </refsect2>
472
473   <refsect2 id='gvariant-text-maybe-types'>
474    <title>Maybe Types</title>
475    <para>
476     The syntax for specifying maybe types is inspired by Haskell.
477    </para>
478    <para>
479     The null case is specified using the keyword <literal>nothing</literal> and the non-null case is explicitly
480     specified using the keyword <literal>just</literal>.  GVariant allows <literal>just</literal> to be omitted
481     in every case that it is able to unambiguously determine the intention of the writer.  There are two cases
482     where it must be specified:
483    </para>
484    <itemizedlist>
485     <listitem>
486      <para>when using nested maybes, in order to specify the <literal>just nothing</literal> case</para>
487     </listitem>
488     <listitem>
489      <para>
490       to establish the nullability of the type of a value without explicitly specifying its full type
491      </para>
492     </listitem>
493    </itemizedlist>
494    <para>
495     Some examples:
496    </para>
497    <para>
498     <literal>just 'hello'</literal> parses as a non-null nullable string.
499    </para>
500    <para>
501     <literal>@ms 'hello'</literal> is the same (demonstrating how <literal>just</literal> can be dropped if the type is already
502     known).
503    </para>
504    <para>
505     <literal>nothing</literal> will not parse wtihout extra type information.
506    </para>
507    <para>
508     <literal>@ms nothing</literal> parses as a null nullable string.
509    </para>
510    <para>
511     <literal>[just 3, nothing]</literal> is an array of nullable integers
512    </para>
513    <para>
514     <literal>[3, nothing]</literal> is the same as the above (demonstrating another place were
515     <literal>just</literal> can be dropped).
516    </para>
517    <para>
518     <literal>[3, just nothing]</literal> parses as an array of maybe maybe integers (type
519     <literal>'ammi'</literal>).
520    </para>
521   </refsect2>
522
523   <refsect2 id='gvariant-text-type-annotations'>
524    <title>Type Annotations</title>
525    <para>
526     Type annotations allow additional type information to be given to the parser.  Depending on the context,
527     this type information can change the output of the parser, cause an error when parsing would otherwise have
528     succeeded or resolve an error when parsing would have otherwise failed.
529    </para>
530    <para>
531     Type annotations come in two forms: type codes and type keywords.
532    </para>
533    <para>
534     Type keywords can be seen as more verbose (and more legible) versions of a common subset of the type codes.
535     The type keywords <literal>boolean</literal>, <literal>byte</literal>, <literal>int16</literal>,
536     <literal>uint16</literal>, <literal>int32</literal>, <literal>uint32</literal>, <literal>handle</literal>,
537     <literal>int64</literal>, <literal>uint64</literal>, <literal>double</literal>, <literal>string</literal>,
538     <literal>objectpath</literal> and literal <literal>signature</literal> are each exactly equivalent to their
539     corresponding type code.
540    </para>
541    <para>
542     Type codes are an <literal>@</literal> ("at" sign) followed by a definite GVariant type string.  Some
543     examples:
544    </para>
545    <para>
546     <literal>uint32 5</literal> causes the number to be parsed unsigned instead of signed (the default).
547    </para>
548    <para>
549     <literal>@u 5</literal> is the same
550    </para>
551    <para>
552     <literal>objectpath "/org/gnome/xyz"</literal> creates an object path instead of a normal string
553    </para>
554    <para>
555     <literal>@au []</literal> specifies the type of the empty array (which would not parse otherwise)
556    </para>
557    <para>
558     <literal>@ms ""</literal> indicates that a string value is meant to have a maybe type
559    </para>
560   </refsect2>
561
562   <refsect2 id='gvariant-text-bytestrings'>
563    <title>Bytestrings</title>
564    <para>
565     The bytestring syntax is a piece of syntactic sugar meant to complement the bytestring APIs in GVariant.  It
566     constructs arrays of non-nul bytes (type '<literal>ay</literal>') with a nul terminator at the end.
567    </para>
568    <para>
569     Bytestrings are specified with either <literal>b""</literal> or <literal>b''</literal>.  As with strings,
570     there is no fundamental difference between the two different types of quotes.
571    </para>
572    <para>
573     Bytestrings support the full range of escapes that you would expect (ie: those supported by
574     <link linkend='g-strcompress'><function>g_strcompress()</function></link>.  This includes the normal control
575     sequence escapes (as mentioned in the section on strings) as well as octal and hexidecimal escapes of the
576     forms <literal>\0nnn</literal> and <literal>\xnn</literal>.
577    </para>
578    <para>
579     <literal>b'abc'</literal> is equivalent to <literal>[byte 0x97, 0x98, 0x99, 0]</literal>.
580    </para>
581    <para>
582     When formatting arrays of bytes, the printer will choose to display the array as a bytestring if it contains
583     a nul character at the end and no other nul bytes within.  Otherwise, it is formatted as a normal array.
584    </para>
585   </refsect2>
586
587   <refsect2 id='gvariant-text-positional'>
588    <title>Positional Parameters</title>
589    <para>
590     Positional parameters are not a part of the normal GVariant text format, but they are mentioned here because
591     they can be used with <link linkend='g-variant-new-parsed'><function>g_variant_new_parsed()</function></link>.
592    </para>
593    <para>
594     A positional parameter is indicated with a <literal>%</literal> followed by any valid
595     <link linkend='gvariant-format-strings'>GVariant Format String</link>.  Variable arguments are collected as
596     specified by the format string and the resulting value is inserted at the current position.
597    </para>
598    <para>
599     This feature is best explained by example:
600    </para>
601    <informalexample><programlisting><![CDATA[char *t = "xyz";
602 gboolean en = false;
603 GVariant *value;
604
605 value = g_variant_new_parsed ("{'title': <%s>, 'enabled': <%b>}", t, en);]]></programlisting></informalexample>
606    <para>
607     This constructs a dictionary mapping strings to variants (type '<literal>a{sv}</literal>') with two items in
608     it.  The key names are parsed from the string and the values for those keys are taken as variable arguments
609     parameters.
610    </para>
611    <para>
612     The arguments are always collected in the order that they appear in the string to be parsed.  Format strings
613     that collect multiple arguments are permitted, so you may require more varargs parameters than the number of
614     <literal>%</literal> signs that appear.  You can also give format strings that collect no arguments, but
615     there's no good reason to do so.
616    </para>
617   </refsect2>
618  </refsect1>
619 </refentry>