1 <?xml version='1.0' encoding='utf-8'?>
3 <refentry id='gvariant-text'>
5 <refentrytitle>GVariant Text Format</refentrytitle>
8 <refname>GVariant Text Format</refname>
9 <refpurpose>textual representation of GVariants</refpurpose>
13 <title>GVariant Text Format</title>
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
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.
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
35 <informalexample><programlisting>[[1, 2, 3], [4, 5, 6]]</programlisting></informalexample>
37 is parsed as an array of arrays of integers (type '<literal>aai</literal>'), but
39 <informalexample><programlisting>[[1, 2, 3], [4, 5, 6.0]]</programlisting></informalexample>
41 is parsed as a array of arrays of doubles (type '<literal>aad</literal>').
45 As another example, GVariant is able to determine that
47 <informalexample><programlisting>["hello", nothing]</programlisting></informalexample>
49 is an array of maybe strings (type '<literal>ams</literal>').
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.
64 <title>Syntax Summary</title>
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.
73 <colspec colname='col_0'/>
74 <colspec colname='col_1'/>
78 <entry colsep='1' rowsep='1'>
80 <emphasis role='strong'>Symbol</emphasis>
83 <entry colsep='1' rowsep='1'>
85 <emphasis role='strong'>Meaning</emphasis>
91 <entry colsep='1' rowsep='1'>
93 <emphasis role='strong'><literal>true</literal></emphasis>,
94 <emphasis role='strong'><literal>false</literal></emphasis>
97 <entry colsep='1' rowsep='1'>
99 <link linkend='gvariant-text-booleans'>Booleans</link>.
105 <entry colsep='1' rowsep='1'>
107 <emphasis role='strong'><literal>""</literal></emphasis>,
108 <emphasis role='strong'><literal>''</literal></emphasis>
111 <entry colsep='1' rowsep='1'>
113 String literal. See <link linkend='gvariant-text-strings'>Strings</link> below.
119 <entry colsep='1' rowsep='1'>
124 <entry colsep='1' rowsep='1'>
126 See <link linkend='gvariant-text-numbers'>Numbers</link> below.
132 <entry colsep='1' rowsep='1'>
134 <emphasis role='strong'><literal>()</literal></emphasis>
137 <entry colsep='1' rowsep='1'>
139 <link linkend='gvariant-text-tuples'>Tuples</link>.
145 <entry colsep='1' rowsep='1'>
147 <emphasis role='strong'><literal>[]</literal></emphasis>
150 <entry colsep='1' rowsep='1'>
152 <link linkend='gvariant-text-arrays'>Arrays</link>.
158 <entry colsep='1' rowsep='1'>
160 <emphasis role='strong'><literal>{}</literal></emphasis>
163 <entry colsep='1' rowsep='1'>
165 <link linkend='gvariant-text-dictionaries'>Dictionaries and Dictionary Entries</link>.
171 <entry colsep='1' rowsep='1'>
173 <emphasis role='strong'><literal><></literal></emphasis>
176 <entry colsep='1' rowsep='1'>
178 <link linkend='gvariant-text-variants'>Variants</link>.
184 <entry colsep='1' rowsep='1'>
186 <emphasis role='strong'><literal>just</literal></emphasis>,
187 <emphasis role='strong'><literal>nothing</literal></emphasis>
190 <entry colsep='1' rowsep='1'>
192 <link linkend='gvariant-text-maybe-types'>Maybe Types</link>.
198 <entry colsep='1' rowsep='1'>
200 <emphasis role='strong'><literal>@</literal></emphasis>
203 <entry colsep='1' rowsep='1'>
205 <link linkend='gvariant-text-type-annotations'>Type Annotations</link>.
211 <entry colsep='1' rowsep='1'>
216 <entry colsep='1' rowsep='1'>
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>
233 See <link linkend='gvariant-text-type-annotations'>Type Annotations</link> below.
239 <entry colsep='1' rowsep='1'>
241 <emphasis role='strong'><literal>b""</literal></emphasis>,
242 <emphasis role='strong'><literal>b''</literal></emphasis>
245 <entry colsep='1' rowsep='1'>
247 <link linkend='gvariant-text-bytestrings'>Bytestrings</link>.
253 <entry colsep='1' rowsep='1'>
255 <emphasis role='strong'><literal>%</literal></emphasis>
258 <entry colsep='1' rowsep='1'>
260 <link linkend='gvariant-text-positional'>Positional Parameters</link>.
268 <refsect2 id='gvariant-text-booleans'>
269 <title>Booleans</title>
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.
276 <refsect2 id='gvariant-text-strings'>
277 <title>Strings</title>
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).
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.
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.
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.
303 Single-character strings are not interpreted as bytes. Bytes must be specified by their numerical value.
307 <refsect2 id='gvariant-text-numbers'>
308 <title>Numbers</title>
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.
315 Floating point numbers can also be given in the usual ways, including scientific and hexidecimal notations.
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.
326 <literal>5</literal> parses as the int32 value five.
329 <literal>37.5</literal> parses as a floating point value.
332 <literal>3.75e1</literal> parses the same as the value above.
335 <literal>uint64 7</literal> parses seven as a uint64.
336 See <link linkend='gvariant-text-type-annotations'>Type Annotations</link>.
340 <refsect2 id='gvariant-text-tuples'>
341 <title>Tuples</title>
343 Tuples are formed using the same syntax as Python. Here are some examples:
346 <literal>()</literal> parses as the empty tuple.
349 <literal>(5,)</literal> is a tuple containing a single value.
352 <literal>("hello", 42)</literal> is a pair. Note that values of different types are permitted.
356 <refsect2 id='gvariant-text-arrays'>
357 <title>Arrays</title>
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
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.
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:
373 <literal>[1]</literal> parses (without additional type information) as a one-item array of signed integers.
376 <literal>[1, 2, 3]</literal> parses (similarly) as a three-item array.
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.
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
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.
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).
395 <literal>[b'hello', []]</literal> looks odd but will parse properly.
396 See <link linkend='gvariant-text-bytestrings'>Bytestrings</link>
399 And some examples of errors:
402 <literal>["hello", 42]</literal> fails to parse due to conflicting types.
405 <literal>[]</literal> will fail to parse without additional type information.
409 <refsect2 id='gvariant-text-dictionaries'>
410 <title>Dictionaries and Dictionary Entries</title>
412 Dictionaries and dictionary entries are both specified using the <literal>{}</literal> characters.
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).
423 The dictionary syntax is the same as Python's syntax for dictionaries. Some examples:
426 <literal>@a{sv} {}</literal> parses as the empty dictionary of everyone's favourite type.
429 <literal>@a{sv} []</literal> is the same as above (owing to the fact that dictionaries are really arrays).
432 <literal>{1: "one", 2: "two", 3: "three"}</literal> parses as a dictionary mapping integers to strings.
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:
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.
444 <literal>[{1, "one"}, {2, "two"}, {3, "three"}]</literal> is exactly equivalent to the dictionary example
449 <refsect2 id='gvariant-text-variants'>
450 <title>Variants</title>
452 Variants are denoted using angle brackets (aka "XML brackets"), <literal><></literal>. They may not
456 Using <literal><></literal> effectively disrupts the type inferencing that occurs between array
457 elements. This can have positive and negative effects.
460 <literal>[<"hello">, <42>]</literal> will parse whereas <literal>["hello", 42]</literal> would
464 <literal>[<['']>, <[]>]</literal> will fail to parse even though <literal>[[''], []]</literal>
465 parses successfully. You would need to specify <literal>[<['']>, <@as []>]</literal>.
468 <literal>{"title": <"frobit">, "enabled": <true>, width: <800>}</literal> is an example of
469 perhaps the most pervasive use of both dictionaries and variants.
473 <refsect2 id='gvariant-text-maybe-types'>
474 <title>Maybe Types</title>
476 The syntax for specifying maybe types is inspired by Haskell.
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:
486 <para>when using nested maybes, in order to specify the <literal>just nothing</literal> case</para>
490 to establish the nullability of the type of a value without explicitly specifying its full type
498 <literal>just 'hello'</literal> parses as a non-null nullable string.
501 <literal>@ms 'hello'</literal> is the same (demonstrating how <literal>just</literal> can be dropped if the type is already
505 <literal>nothing</literal> will not parse wtihout extra type information.
508 <literal>@ms nothing</literal> parses as a null nullable string.
511 <literal>[just 3, nothing]</literal> is an array of nullable integers
514 <literal>[3, nothing]</literal> is the same as the above (demonstrating another place were
515 <literal>just</literal> can be dropped).
518 <literal>[3, just nothing]</literal> parses as an array of maybe maybe integers (type
519 <literal>'ammi'</literal>).
523 <refsect2 id='gvariant-text-type-annotations'>
524 <title>Type Annotations</title>
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.
531 Type annotations come in two forms: type codes and type keywords.
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.
542 Type codes are an <literal>@</literal> ("at" sign) followed by a definite GVariant type string. Some
546 <literal>uint32 5</literal> causes the number to be parsed unsigned instead of signed (the default).
549 <literal>@u 5</literal> is the same
552 <literal>objectpath "/org/gnome/xyz"</literal> creates an object path instead of a normal string
555 <literal>@au []</literal> specifies the type of the empty array (which would not parse otherwise)
558 <literal>@ms ""</literal> indicates that a string value is meant to have a maybe type
562 <refsect2 id='gvariant-text-bytestrings'>
563 <title>Bytestrings</title>
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. These are
567 normal C strings with no particular encoding enforced, so the bytes may not be valid UTF-8.
568 Bytestrings are a special case of byte arrays; byte arrays (also type '<literal>ay</literal>'), in the general
569 case, can contain nul at any position, and need not end with nul.
572 Bytestrings are specified with either <literal>b""</literal> or <literal>b''</literal>. As with strings,
573 there is no fundamental difference between the two different types of quotes.
576 Bytestrings support the full range of escapes that you would expect (ie: those supported by
577 <link linkend='g-strcompress'><function>g_strcompress()</function></link>. This includes the normal control
578 sequence escapes (as mentioned in the section on strings) as well as octal and hexidecimal escapes of the
579 forms <literal>\0nnn</literal> and <literal>\xnn</literal>.
582 <literal>b'abc'</literal> is equivalent to <literal>[byte 0x61, 0x62, 0x63, 0]</literal>.
585 When formatting arrays of bytes, the printer will choose to display the array as a bytestring if it contains
586 a nul character at the end and no other nul bytes within. Otherwise, it is formatted as a normal array.
590 <refsect2 id='gvariant-text-positional'>
591 <title>Positional Parameters</title>
593 Positional parameters are not a part of the normal GVariant text format, but they are mentioned here because
594 they can be used with <link linkend='g-variant-new-parsed'><function>g_variant_new_parsed()</function></link>.
597 A positional parameter is indicated with a <literal>%</literal> followed by any valid
598 <link linkend='gvariant-format-strings'>GVariant Format String</link>. Variable arguments are collected as
599 specified by the format string and the resulting value is inserted at the current position.
602 This feature is best explained by example:
604 <informalexample><programlisting><![CDATA[char *t = "xyz";
608 value = g_variant_new_parsed ("{'title': <%s>, 'enabled': <%b>}", t, en);]]></programlisting></informalexample>
610 This constructs a dictionary mapping strings to variants (type '<literal>a{sv}</literal>') with two items in
611 it. The key names are parsed from the string and the values for those keys are taken as variable arguments
615 The arguments are always collected in the order that they appear in the string to be parsed. Format strings
616 that collect multiple arguments are permitted, so you may require more varargs parameters than the number of
617 <literal>%</literal> signs that appear. You can also give format strings that collect no arguments, but
618 there's no good reason to do so.