GVariant Format StringsGVariant Format Stringsvarargs conversion of GVariantsVariable Argument Conversions
This page attempts to document how to perform variable argument
conversions with GVariant.
Conversions occur according to format strings. A format string is a two-way mapping between a single
GVariant value and one or more C values.
A conversion from C values into a GVariant value is made using the
g_variant_new() function. A conversion from a
GVariant into C values is made using the
g_variant_get() function.
Syntax
This section exhaustively describes all possibilities for GVariant format strings. There are no valid forms of
format strings other than those described here. Please note that the format string syntax is likely to expand in the
future.
Valid format strings have one of the following forms:
any type string
a type string prefixed with a '@'
'&s' '&o', '&g', '^as',
'^a&s', '^ao', '^a&o','^ay',
'^&ay', '^aay' or '^a&ay'.
any format string, prefixed with an 'm'
a sequence of zero or more format strings, concatenated and enclosed in parentheses
an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
first format string correspond to a type valid for use as the key type of a dictionary)
Symbols
The following table describes the rough meaning of symbols that may appear inside a GVariant format string. Each
symbol is described in detail in its own section, including usage examples.
SymbolMeaningb, y, n, q, i,
u, x, t, h, d
Used for building or deconstructing boolean, byte and numeric types. See
Numeric Types below.
s, o, g
Used for building or deconstructing string types. See
Strings below.
v
Used for building or deconstructing variant types. See
Variants below.
a
Used for building or deconstructing arrays. See
Arrays below.
m
Used for building or deconstructing maybe types. See
Maybe Types below.
()
Used for building or deconstructing tuples. See
Tuples below.
{}
Used for building or deconstructing dictionary entries. See
Dictionaries below.
@
Used as a prefix for a GVariant type string (not a prefix for a format string, so @as is
a valid format string but @^as is not). Denotes that a pointer to a
GVariant should be used in place of the normal C type or types. For
g_variant_new() this means that you must pass a
non-NULL(GVariant
*); if it is a floating reference, ownership will be taken, as
if by using g_variant_ref_sink().
For g_variant_get() this means that you
must pass a pointer to a (GVariant *) for the value to be returned
by reference or NULL to ignore the value. See
GVariant * below.
*, ?, r
Exactly equivalent to @*, @? and @r. Provided only for
completeness so that all GVariant type strings can be used also as format strings. See GVariant * below.
&
Used as a prefix for a GVariant type string (not a prefix for a format string, so &s is
a valid format string but &@s is not).
Denotes that a C pointer to serialised data
should be used in place of the normal C type. See
Pointers below.
^
Used as a prefix on some specific types of format strings. See
Convenience Conversions below.
Numeric Types
Characters: b, y, n, q,
i, u, x, t, h,
d
Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
these characters, g_variant_new() takes the equivalent C
type as an argument. g_variant_get() takes a pointer to
the equivalent C type (or NULL to ignore the value).
The equivalent C types are as follows:
CharacterEquivalent C typebgbooleanygucharngint16qguint16igint32uguint32xgint64tguint64hgint32dgdouble
Note that in C, small integer types in variable argument lists are promoted up to int or unsigned int as appropriate, and
read back accordingly. int is 32 bits on every platform on which GLib is
currently supported. This means that you can use C expressions of type int
with g_variant_new() and format characters
'b', 'y', 'n', 'q',
'i', 'u' and 'h'. Specifically, you can use integer
literals with these characters.
When using the 'x' and 't' characters, you must ensure that the value that you
provide is 64 bit. This means that you should use a cast or make use of the
G_GINT64_CONSTANT or
G_GUINT64_CONSTANT macros.
No type promotion occurs when using g_variant_get() since
it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
ExamplesStrings
Characters: s, o, g
String conversions occur to and from standard nul-terminated C strings. Upon encountering an
's', 'o' or 'g' in a format string,
g_variant_new() takes a (const
gchar *) and makes a copy of it.
NULL is not a valid string; use
maybe types to encode that. If the 'o' or
'g' characters are used, care must be taken to ensure that the passed string is a valid D-Bus
object path or D-Bus type signature, respectively.
Upon encounting 's', 'o' or 'g', g_variant_get() takes a pointer to a
(gchar *) (ie: (gchar **)) and
sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
g_free().
NULL may also be passed to indicate that the value of the
string should be ignored (in which case no copy is made).
ExamplesVariants
Characters: v
Upon encountering a 'v',
g_variant_new() takes a (GVariant *). The value of the
GVariant is used as the contents of the variant value.
Upon encountering a 'v', g_variant_get() takes a pointer to a
(GVariant *) (ie: (GVariant **)
). It is set to a new reference to a GVariant instance
containing the contents of the variant value. It is appropriate to free this reference using
g_variant_unref().
NULL may also be passed to indicate that the value should be
ignored (in which case no new reference is created).
ExamplesArrays
Characters: a
Upon encountering an 'a' character followed by a type string,
g_variant_new() will take a
(GVariantBuilder *) that has been created as an array builder
for an array of the type given in the type string. The builder will have
g_variant_builder_end() called on it and the
result will be used as the value. As a special exception, if the given type string is a definite type, then
NULL may be given to mean an empty array of that type.
Upon encountering an 'a' character followed by a type string,
g_variant_get() will take a pointer to a
(GVariantIter *) (ie:
(GVariantIter **)).
A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
This iterator should be freed when you are done with it, using
g_variant_iter_free().
NULL may also be given to indicate that the value of the array
should be ignored.
ExamplesMaybe Types
Characters: m
Maybe types are handled in two separate ways depending on the format string that follows the
'm'. The method that is used currently depends entirely on the character immediately following the
'm'.
The first way is used with format strings starting with 'a', 's',
'o', 'g', 'v', '@',
'*', '?', 'r', '&', or
'^'. In all of these cases, for non-maybe types,
g_variant_new() takes a pointer to a
non-NULL value and
g_variant_get() returns (by reference) a
non-NULL pointer. When any of these format strings are
prefixed with an 'm', the type of arguments that are collected does not change in any way, but
NULL becomes a permissible value, to indicate the Nothing case.
Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
here. Using a NULL pointer with the format string 'mas' constructs
the Nothing value -- not an empty array.
The second way is used with all other format strings. For
g_variant_new() an additional
gboolean argument is collected and for
g_variant_get() an additional
(gboolean *). Following this argument, the arguments that are normally
collected for the equivalent non-maybe type will be collected.
If FALSE is given to
g_variant_new() then the Nothing value is constructed and
the collected arguments are ignored. Otherwise (if TRUE was
given), the arguments are used in the normal way to create the Just value.
If NULL is given to
g_variant_get() then the value is ignored. If a
non-NULL pointer is given then it is used to return by reference
whether the value was Just. In the case that the value was Just, the
gboolean will be set to
TRUE and the value will be stored in the arguments in the usual
way. In the case that the value was Nothing, the gboolean will be set to
FALSE and the arguments will be collected in the normal way
but have their values set to binary zero.
ExamplesTuples
Characters: ()
Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
ExamplesGVariant *
Characters: @, *, ?, r
Upon encountering a '@' in front of a type string,
g_variant_new() takes a
non-NULL pointer to a
GVariant and uses its value directly instead of collecting arguments to
create the value. The provided GVariant must have a type that matches the
type string following the '@'. '*' is
the same as '@*' (ie: take a GVariant of any type).
'?' is the same as '@?' (ie: take a
GVariant of any basic type). 'r' is the same as
'@r' (ie: take a GVariant of any tuple type).
Upon encountering a '@' in front of a type string,
g_variant_get()
takes a pointer to a (GVariant *) (ie: a
(GVariant **)) and sets it to a new reference to a
GVariant containing the value (instead of deconstructing the value into
C types in the usual way). NULL can be given to ignore the
value. '*', '?' and 'r' are handled in a way analogous to
what is stated above.
You can always use '*' as an alternative to '?', 'r' or any
use of '@'. Using the other characters where possible is recommended, however, due to the
improvements in type safety and code self-documentation.
ExamplesDictionaries
Characters: {}
Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
Examples
To extract data from nested dictionaries you can go through a vardict.
Examples
, 'max': <%i>}})",
"/object/path", value, max);
{
GVariant *params;
GVariant *p_brightness;
gchar *obj
gint p_max;
g_variant_get (data, "(o@a{?*})", &obj, ¶ms);
g_print ("object_path: %s\n", obj);
p_brightness = g_variant_lookup_value (params, "brightness", G_VARIANT_TYPE_VARDICT);
g_variant_lookup (p_brightness, "max", "i", &p_max);
g_print ("max: %d\n", p_max);
}]]>Pointers
Characters: &
The '&' character is used to indicate that serialised data should be directly exchanged via a
pointer.
Currently, the only use for this character is when it is applied to a string (ie: '&s',
'&o' or '&g'). For
g_variant_new() this has absolutely no effect. The string
is collected and duplicated normally. For g_variant_get()
it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
always be properly nul-terminated.
ExamplesConvenience Conversions
Characters: ^
The '^' character currently supports conversion to and from bytestrings or to and from arrays
of strings or bytestrings. It does not support byte arrays. It has a number of forms.
In all forms, when used with g_variant_new() one
pointer value is collected from the variable arguments and passed to a function (as given in the table below).
The result of that function is used as the value for this position. When used with
g_variant_get() one pointer value is produced by using
the function (given in the table) and returned by reference.
Conversion
Used with g_variant_new()
Used with g_variant_get()^as
equivalent to g_variant_new_strv()
equivalent to g_variant_dup_strv()^a&s
equivalent to g_variant_get_strv()^ao
equivalent to g_variant_new_objv()
equivalent to g_variant_dup_objv()^a&o
equivalent to g_variant_get_objv()^ay
equivalent to g_variant_new_bytestring()
equivalent to g_variant_dup_bytestring()^&ay
equivalent to g_variant_get_bytestring()^aay
equivalent to g_variant_new_bytestring_array()
equivalent to g_variant_dup_bytestring_array()^a&ay
equivalent to g_variant_get_bytestring_array()