34
34
There are two basic syntaxes used, type definitions and command definitions.
36
36
The first syntax defines a type and is represented by a dictionary. There are
37
two kinds of types that are supported: complex user-defined types, and enums.
39
A complex type is a dictionary containing a single key who's value is a
37
three kinds of user-defined types that are supported: complex types,
38
enumeration types and union types.
40
Generally speaking, types definitions should always use CamelCase for the type
41
names. Command names should be all lower case with words separated by a hyphen.
45
A complex type is a dictionary containing a single key whose value is a
40
46
dictionary. This corresponds to a struct in C or an Object in JSON. An
41
47
example of a complex type is:
47
53
members should always be added to the end of the dictionary to preserve
48
54
backwards compatibility.
50
An enumeration type is a dictionary containing a single key who's value is a
56
=== Enumeration types ===
58
An enumeration type is a dictionary containing a single key whose value is a
51
59
list of strings. An example enumeration is:
53
61
{ 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
55
Generally speaking, complex types and enums should always use CamelCase for
65
Union types are used to let the user choose between several different data
66
types. A union type is defined using a dictionary as explained in the
70
A simple union type defines a mapping from discriminator values to data types
73
{ 'type': 'FileOptions', 'data': { 'filename': 'str' } }
74
{ 'type': 'Qcow2Options',
75
'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
77
{ 'union': 'BlockdevOptions',
78
'data': { 'file': 'FileOptions',
79
'qcow2': 'Qcow2Options' } }
81
In the QMP wire format, a simple union is represented by a dictionary that
82
contains the 'type' field as a discriminator, and a 'data' field that is of the
83
specified data type corresponding to the discriminator value:
85
{ "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
86
"lazy-refcounts": true } }
89
A union definition can specify a complex type as its base. In this case, the
90
fields of the complex type are included as top-level fields of the union
91
dictionary in the QMP wire format. An example definition is:
93
{ 'type': 'BlockdevCommonOptions', 'data': { 'readonly': 'bool' } }
94
{ 'union': 'BlockdevOptions',
95
'base': 'BlockdevCommonOptions',
96
'data': { 'raw': 'RawOptions',
97
'qcow2': 'Qcow2Options' } }
99
And it looks like this on the wire:
103
"data" : { "backing-file": "/some/place/my-image",
104
"lazy-refcounts": true } }
107
Flat union types avoid the nesting on the wire. They are used whenever a
108
specific field of the base type is declared as the discriminator ('type' is
109
then no longer generated). The discriminator must always be a string field.
110
The above example can then be modified as follows:
112
{ 'type': 'BlockdevCommonOptions',
113
'data': { 'driver': 'str', 'readonly': 'bool' } }
114
{ 'union': 'BlockdevOptions',
115
'base': 'BlockdevCommonOptions',
116
'discriminator': 'driver',
117
'data': { 'raw': 'RawOptions',
118
'qcow2': 'Qcow2Options' } }
120
Resulting in this JSON object:
124
"backing-file": "/some/place/my-image",
125
"lazy-refcounts": true }
128
A special type of unions are anonymous unions. They don't form a dictionary in
129
the wire format but allow the direct use of different types in their place. As
130
they aren't structured, they don't have any explicit discriminator but use
131
the (QObject) data type of their value as an implicit discriminator. This means
132
that they are restricted to using only one discriminator value per QObject
133
type. For example, you cannot have two different complex types in an anonymous
134
union, or two different integer types.
136
Anonymous unions are declared using an empty dictionary as their discriminator.
137
The discriminator values never appear on the wire, they are only used in the
138
generated C code. Anonymous unions cannot have a base type.
140
{ 'union': 'BlockRef',
142
'data': { 'definition': 'BlockdevOptions',
143
'reference': 'str' } }
145
This example allows using both of the following example objects:
147
{ "file": "my_existing_block_device_id" }
148
{ "file": { "driver": "file",
150
'filename': "/tmp/mydisk.qcow2" } }
58
155
Commands are defined by using a list containing three members. The first
59
156
member is the command name, the second member is a dictionary containing