GVariant: add support for single precision floats
[platform/upstream/glib.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>float</literal>,
228         <literal>double</literal>,
229         <literal>string</literal>,
230         <literal>objectpath</literal>,
231         <literal>signature</literal>
232        </para>
233        <para>
234         See <link linkend='gvariant-text-type-annotations'>Type Annotations</link> below.
235        </para>
236       </entry>
237      </row>
238
239      <row rowsep='1'>
240       <entry colsep='1' rowsep='1'>
241        <para>
242         <emphasis role='strong'><literal>b""</literal></emphasis>,
243         <emphasis role='strong'><literal>b''</literal></emphasis>
244        </para>
245       </entry>
246       <entry colsep='1' rowsep='1'>
247        <para>
248         <link linkend='gvariant-text-bytestrings'>Bytestrings</link>.
249        </para>
250       </entry>
251      </row>
252
253      <row rowsep='1'>
254       <entry colsep='1' rowsep='1'>
255        <para>
256         <emphasis role='strong'><literal>%</literal></emphasis>
257        </para>
258       </entry>
259       <entry colsep='1' rowsep='1'>
260        <para>
261         <link linkend='gvariant-text-positional'>Positional Parameters</link>.
262        </para>
263       </entry>
264      </row>
265     </tbody>
266    </tgroup>
267   </informaltable>
268
269   <refsect2 id='gvariant-text-booleans'>
270    <title>Booleans</title>
271    <para>
272     The strings <literal>true</literal> and <literal>false</literal> are parsed as booleans.  This is the only
273     way to specify a boolean value.
274    </para>
275   </refsect2>
276
277   <refsect2 id='gvariant-text-strings'>
278    <title>Strings</title>
279    <para>
280     Strings literals must be quoted using <literal>""</literal> or <literal>''</literal>.  The two are
281     completely equivalent (except for the fact that each one is unable to contain itself unescaped).
282    </para>
283    <para>
284     Strings are Unicode strings with no particular encoding.  For example, to specify the character
285     <literal>é</literal>, you just write <literal>'é'</literal>.  You could also give the Unicode codepoint of
286     that character (U+E9) as the escape sequence <literal>'\u00e9'</literal>.  Since the strings are pure
287     Unicode, you should not attempt to encode the utf-8 byte sequence corresponding to the string using escapes;
288     it won't work and you'll end up with the individual characters corresponding to each byte.
289    </para>
290    <para>
291     Unicode escapes of the form <literal>\uxxxx</literal> and <literal>\Uxxxxxxxx</literal> are supported, in
292     hexidecimal.  The usual control sequence escapes <literal>\a</literal>, <literal>\b</literal>,
293     <literal>\f</literal>, <literal>\n</literal>, <literal>\r</literal>, <literal>\t</literal> and
294     <literal>\v</literal> are supported.  Additionally, a <literal>\</literal> before a newline character causes
295     the newline to be ignored.  Finally, any other character following <literal>\</literal> is copied literally
296     (for example, <literal>\"</literal> or <literal>\\</literal>) but for forwards compatibility with future
297     additions you should only use this feature when necessary for escaping backslashes or quotes.
298    </para>
299    <para>
300     The usual octal and hexidecimal escapes <literal>\0nnn</literal> and <literal>\xnn</literal> are not
301     supported here.  Those escapes are used to encode byte values and GVariant strings are Unicode.
302    </para>
303    <para>
304     Single-character strings are not interpreted as bytes.  Bytes must be specified by their numerical value.
305    </para>
306   </refsect2>
307
308   <refsect2 id='gvariant-text-numbers'>
309    <title>Numbers</title>
310    <para>
311     Numbers are given by default as decimal values.  Octal and hex values can be given in the usual way (by
312     prefixing with <literal>0</literal> or <literal>0x</literal>).  Note that GVariant considers bytes to be
313     unsigned integers and will print them as a two digit hexidecimal number by default.
314    </para>
315    <para>
316     Floating point numbers can also be given in the usual ways, including scientific and hexidecimal notations.
317    </para>
318    <para>
319     For lack of additional information, integers will be parsed as int32 values by default.  If the number has a
320     point or an 'e' in it, then it will be parsed as a double precision floating point number by default.  If
321     type information is available (either explicitly or inferred) then that type will be used instead.
322    </para>
323    <para>
324     Some examples:
325    </para>
326    <para>
327     <literal>5</literal> parses as the int32 value five.
328    </para>
329    <para>
330     <literal>37.5</literal> parses as a floating point value.
331    </para>
332    <para>
333     <literal>3.75e1</literal> parses the same as the value above.
334    </para>
335    <para>
336     <literal>uint64 7</literal> parses seven as a uint64.
337     See <link linkend='gvariant-text-type-annotations'>Type Annotations</link>.
338    </para>
339   </refsect2>
340
341   <refsect2 id='gvariant-text-tuples'>
342    <title>Tuples</title>
343    <para>
344     Tuples are formed using the same syntax as Python.  Here are some examples:
345    </para>
346    <para>
347     <literal>()</literal> parses as the empty tuple.
348    </para>
349    <para>
350     <literal>(5,)</literal> is a tuple containing a single value.
351    </para>
352    <para>
353     <literal>("hello", 42)</literal> is a pair.  Note that values of different types are permitted.
354    </para>
355   </refsect2>
356
357   <refsect2 id='gvariant-text-arrays'>
358    <title>Arrays</title>
359    <para>
360     Arrays are formed using the same syntax as Python uses for lists (which is arguably the term that GVariant
361     should have used).  Note that, unlike Python lists, GVariant arrays are statically typed.  This has two
362     implications.
363    </para>
364    <para>
365     First, all items in the array must have the same type.  Second, the type of the array must be known, even in
366     the case that it is empty.  This means that (unless there is some other way to infer it) type information
367     will need to be given explicitly for empty arrays.
368    </para>
369    <para>
370     The parser is able to infer some types based on the fact that all items in an array must have the same type.
371     See the examples below:
372    </para>
373    <para>
374     <literal>[1]</literal> parses (without additional type information) as a one-item array of signed integers.
375    </para>
376    <para>
377     <literal>[1, 2, 3]</literal> parses (similarly) as a three-item array.
378    </para>
379    <para>
380     <literal>[1, 2, 3.0]</literal> parses as an array of doubles.  This is the most simple case of the type
381     inferencing in action.
382    </para>
383    <para>
384     <literal>[(1, 2), (3, 4.0)]</literal> causes the 2 to also be parsed as a double (but the 1 and 4 are still
385     integers).
386    </para>
387    <para>
388     <literal>["", nothing]</literal> parses as an array of maybe strings.  The presence of
389     "<literal>nothing</literal>" clearly implies that the array elements are nullable.
390    </para>
391    <para>
392     <literal>[[], [""]]</literal> will parse properly because the type of the first (empty) array can be
393     inferred to be equal to the type of the second array (both are arrays of strings).
394    </para>
395    <para>
396     <literal>[b'hello', []]</literal> looks odd but will parse properly.
397     See <link linkend='gvariant-text-bytestrings'>Bytestrings</link>
398    </para>
399    <para>
400     And some examples of errors:
401    </para>
402    <para>
403     <literal>["hello", 42]</literal> fails to parse due to conflicting types.
404    </para>
405    <para>
406     <literal>[]</literal> will fail to parse without additional type information.
407    </para>
408   </refsect2>
409
410   <refsect2 id='gvariant-text-dictionaries'>
411    <title>Dictionaries and Dictionary Entries</title>
412    <para>
413     Dictionaries and dictionary entries are both specified using the <literal>{}</literal> characters.
414    </para>
415    <para>
416     The dictionary syntax is more commonly used.  This is what the printer elects to use in the normal case of
417     dictionary entries appearing in an array (aka "a dictionary").  The separate syntax for dictionary entries
418     is typically only used for when the entries appear on their own, outside of an array (which is valid but
419     unusual).  Of course, you are free to use the dictionary entry syntax within arrays but there is no good
420     reason to do so (and the printer itself will never do so).  Note that, as with arrays, the type of empty
421     dictionaries must be established (either explicitly or through inference).
422    </para>
423    <para>
424     The dictionary syntax is the same as Python's syntax for dictionaries.  Some examples:
425    </para>
426    <para>
427     <literal>@a{sv} {}</literal> parses as the empty dictionary of everyone's favourite type.
428    </para>
429    <para>
430     <literal>@a{sv} []</literal> is the same as above (owing to the fact that dictionaries are really arrays).
431    </para>
432    <para>
433     <literal>{1: "one", 2: "two", 3: "three"}</literal> parses as a dictionary mapping integers to strings.
434    </para>
435    <para>
436     The dictionary entry syntax looks just like a pair (2-tuple) that uses braces instead of parens.  The
437     presence of a comma immediately following the key differentiates it from the dictionary syntax (which
438     features a colon after the first key).  Some examples:
439    </para>
440    <para>
441     <literal>{1, "one"}</literal> is a free-standing dictionary entry that can be parsed on its own or as part
442     of another container value.
443    </para>
444    <para>
445     <literal>[{1, "one"}, {2, "two"}, {3, "three"}]</literal> is exactly equivalent to the dictionary example
446     given above.
447    </para>
448   </refsect2>
449
450   <refsect2 id='gvariant-text-variants'>
451    <title>Variants</title>
452    <para>
453     Variants are denoted using angle brackets (aka "XML brackets"), <literal>&lt;&gt;</literal>.  They may not
454     be omitted.
455    </para>
456    <para>
457     Using <literal>&lt;&gt;</literal> effectively disrupts the type inferencing that occurs between array
458     elements.  This can have positive and negative effects.
459    </para>
460    <para>
461     <literal>[&lt;"hello"&gt;, &lt;42&gt;]</literal> will parse whereas <literal>["hello", 42]</literal> would
462     not.
463    </para>
464    <para>
465     <literal>[&lt;['']&gt;, &lt;[]&gt;]</literal> will fail to parse even though <literal>[[''], []]</literal>
466     parses successfully.  You would need to specify <literal>[&lt;['']&gt;, &lt;@as []&gt;]</literal>.
467    </para>
468    <para>
469     <literal>{"title": &lt;"frobit"&gt;, "enabled": &lt;true&gt;, width: &lt;800&gt;}</literal> is an example of
470     perhaps the most pervasive use of both dictionaries and variants.
471    </para>
472   </refsect2>
473
474   <refsect2 id='gvariant-text-maybe-types'>
475    <title>Maybe Types</title>
476    <para>
477     The syntax for specifying maybe types is inspired by Haskell.
478    </para>
479    <para>
480     The null case is specified using the keyword <literal>nothing</literal> and the non-null case is explicitly
481     specified using the keyword <literal>just</literal>.  GVariant allows <literal>just</literal> to be omitted
482     in every case that it is able to unambiguously determine the intention of the writer.  There are two cases
483     where it must be specified:
484    </para>
485    <itemizedlist>
486     <listitem>
487      <para>when using nested maybes, in order to specify the <literal>just nothing</literal> case</para>
488     </listitem>
489     <listitem>
490      <para>
491       to establish the nullability of the type of a value without explicitly specifying its full type
492      </para>
493     </listitem>
494    </itemizedlist>
495    <para>
496     Some examples:
497    </para>
498    <para>
499     <literal>just 'hello'</literal> parses as a non-null nullable string.
500    </para>
501    <para>
502     <literal>@ms 'hello'</literal> is the same (demonstrating how <literal>just</literal> can be dropped if the type is already
503     known).
504    </para>
505    <para>
506     <literal>nothing</literal> will not parse wtihout extra type information.
507    </para>
508    <para>
509     <literal>@ms nothing</literal> parses as a null nullable string.
510    </para>
511    <para>
512     <literal>[just 3, nothing]</literal> is an array of nullable integers
513    </para>
514    <para>
515     <literal>[3, nothing]</literal> is the same as the above (demonstrating another place were
516     <literal>just</literal> can be dropped).
517    </para>
518    <para>
519     <literal>[3, just nothing]</literal> parses as an array of maybe maybe integers (type
520     <literal>'ammi'</literal>).
521    </para>
522   </refsect2>
523
524   <refsect2 id='gvariant-text-type-annotations'>
525    <title>Type Annotations</title>
526    <para>
527     Type annotations allow additional type information to be given to the parser.  Depending on the context,
528     this type information can change the output of the parser, cause an error when parsing would otherwise have
529     succeeded or resolve an error when parsing would have otherwise failed.
530    </para>
531    <para>
532     Type annotations come in two forms: type codes and type keywords.
533    </para>
534    <para>
535     Type keywords can be seen as more verbose (and more legible) versions of a common subset of the type codes.
536     The type keywords <literal>boolean</literal>, <literal>byte</literal>, <literal>int16</literal>,
537     <literal>uint16</literal>, <literal>int32</literal>, <literal>uint32</literal>, <literal>handle</literal>,
538     <literal>int64</literal>, <literal>uint64</literal>, <literal>float</literal>, <literal>double</literal>,
539     <literal>string</literal>, <literal>objectpath</literal> and literal <literal>signature</literal> are each
540     exactly equivalent to their corresponding type code.
541    </para>
542    <para>
543     Type codes are an <literal>@</literal> ("at" sign) followed by a definite GVariant type string.  Some
544     examples:
545    </para>
546    <para>
547     <literal>uint32 5</literal> causes the number to be parsed unsigned instead of signed (the default).
548    </para>
549    <para>
550     <literal>@u 5</literal> is the same
551    </para>
552    <para>
553     <literal>objectpath "/org/gnome/xyz"</literal> creates an object path instead of a normal string
554    </para>
555    <para>
556     <literal>@au []</literal> specifies the type of the empty array (which would not parse otherwise)
557    </para>
558    <para>
559     <literal>@ms ""</literal> indicates that a string value is meant to have a maybe type
560    </para>
561   </refsect2>
562
563   <refsect2 id='gvariant-text-bytestrings'>
564    <title>Bytestrings</title>
565    <para>
566     The bytestring syntax is a piece of syntactic sugar meant to complement the bytestring APIs in GVariant.  It
567     constructs arrays of non-nul bytes (type '<literal>ay</literal>') with a nul terminator at the end.
568    </para>
569    <para>
570     Bytestrings are specified with either <literal>b""</literal> or <literal>b''</literal>.  As with strings,
571     there is no fundamental difference between the two different types of quotes.
572    </para>
573    <para>
574     Bytestrings support the full range of escapes that you would expect (ie: those supported by
575     <link linkend='g-strcompress'><function>g_strcompress()</function></link>.  This includes the normal control
576     sequence escapes (as mentioned in the section on strings) as well as octal and hexidecimal escapes of the
577     forms <literal>\0nnn</literal> and <literal>\xnn</literal>.
578    </para>
579    <para>
580     <literal>b'abc'</literal> is equivalent to <literal>[byte 0x97, 0x98, 0x99, 0]</literal>.
581    </para>
582    <para>
583     When formatting arrays of bytes, the printer will choose to display the array as a bytestring if it contains
584     a nul character at the end and no other nul bytes within.  Otherwise, it is formatted as a normal array.
585    </para>
586   </refsect2>
587
588   <refsect2 id='gvariant-text-positional'>
589    <title>Positional Parameters</title>
590    <para>
591     Positional parameters are not a part of the normal GVariant text format, but they are mentioned here because
592     they can be used with <link linkend='g-variant-new-parsed'><function>g_variant_new_parsed()</function></link>.
593    </para>
594    <para>
595     A positional parameter is indicated with a <literal>%</literal> followed by any valid
596     <link linkend='gvariant-format-strings'>GVariant Format String</link>.  Variable arguments are collected as
597     specified by the format string and the resulting value is inserted at the current position.
598    </para>
599    <para>
600     This feature is best explained by example:
601    </para>
602    <informalexample><programlisting><![CDATA[char *t = "xyz";
603 gboolean en = false;
604 GVariant *value;
605
606 value = g_variant_new_parsed ("{'title': <%s>, 'enabled': <%b>}", t, en);]]></programlisting></informalexample>
607    <para>
608     This constructs a dictionary mapping strings to variants (type '<literal>a{sv}</literal>') with two items in
609     it.  The key names are parsed from the string and the values for those keys are taken as variable arguments
610     parameters.
611    </para>
612    <para>
613     The arguments are always collected in the order that they appear in the string to be parsed.  Format strings
614     that collect multiple arguments are permitted, so you may require more varargs parameters than the number of
615     <literal>%</literal> signs that appear.  You can also give format strings that collect no arguments, but
616     there's no good reason to do so.
617    </para>
618   </refsect2>
619  </refsect1>
620 </refentry>