]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/qjson.c
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2020-12-21' into...
[mirror_qemu.git] / qobject / qjson.c
CommitLineData
b4748b9b
AL
1/*
2 * QObject JSON integration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
f2ad72b3 14#include "qemu/osdep.h"
99dbfd1d 15#include "qapi/error.h"
86cdf9ec 16#include "qapi/qmp/json-parser.h"
998da0b1 17#include "qapi/qmp/json-writer.h"
7b1b5d19 18#include "qapi/qmp/qjson.h"
6b673957 19#include "qapi/qmp/qbool.h"
452fcdbc 20#include "qapi/qmp/qdict.h"
47e6b297 21#include "qapi/qmp/qlist.h"
15280c36 22#include "qapi/qmp/qnum.h"
fc81fa1e 23#include "qapi/qmp/qstring.h"
b4748b9b
AL
24
25typedef struct JSONParsingState
26{
27 JSONMessageParser parser;
b4748b9b 28 QObject *result;
99dbfd1d 29 Error *err;
b4748b9b
AL
30} JSONParsingState;
31
62815d85 32static void consume_json(void *opaque, QObject *json, Error *err)
b4748b9b 33{
62815d85 34 JSONParsingState *s = opaque;
99dbfd1d 35
2a4794ba
MA
36 assert(!json != !err);
37 assert(!s->result || !s->err);
38
39 if (s->result) {
40 qobject_unref(s->result);
41 s->result = NULL;
42 error_setg(&s->err, "Expecting at most one JSON value");
43 }
44 if (s->err) {
45 qobject_unref(json);
46 error_free(err);
47 return;
48 }
62815d85 49 s->result = json;
2a4794ba 50 s->err = err;
b4748b9b
AL
51}
52
2d36e843
MA
53/*
54 * Parse @string as JSON value.
55 * If @ap is non-null, interpolate %-escapes.
56 * Takes ownership of %p arguments.
57 * On success, return the JSON value.
58 * On failure, store an error through @errp and return NULL.
59 * Ownership of %p arguments becomes indeterminate then. To avoid
60 * leaks, callers passing %p must terminate on error, e.g. by passing
61 * &error_abort.
62 */
63static QObject *qobject_from_jsonv(const char *string, va_list *ap,
64 Error **errp)
b4748b9b
AL
65{
66 JSONParsingState state = {};
67
62815d85 68 json_message_parser_init(&state.parser, consume_json, &state, ap);
b4748b9b
AL
69 json_message_parser_feed(&state.parser, string, strlen(string));
70 json_message_parser_flush(&state.parser);
71 json_message_parser_destroy(&state.parser);
72
dd98e848
MA
73 if (!state.result && !state.err) {
74 error_setg(&state.err, "Expecting a JSON value");
75 }
76
99dbfd1d 77 error_propagate(errp, state.err);
b4748b9b
AL
78 return state.result;
79}
80
57348c2f 81QObject *qobject_from_json(const char *string, Error **errp)
8ff5a7d3 82{
57348c2f 83 return qobject_from_jsonv(string, NULL, errp);
8ff5a7d3
LC
84}
85
4ff18468
MA
86/*
87 * Parse @string as JSON value with %-escapes interpolated.
88 * Abort on error. Do not use with untrusted @string.
89 * Return the resulting QObject. It is never null.
90 */
91QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
92{
93 va_list ap_copy;
94 QObject *obj;
95
96 /* va_copy() is needed when va_list is an array type */
97 va_copy(ap_copy, ap);
98 obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
99 va_end(ap_copy);
100
101 assert(obj);
102 return obj;
103}
104
6ce80fd8
MA
105/*
106 * Parse @string as JSON value with %-escapes interpolated.
107 * Abort on error. Do not use with untrusted @string.
108 * Return the resulting QObject. It is never null.
109 */
110QObject *qobject_from_jsonf_nofail(const char *string, ...)
b4748b9b 111{
8ff5a7d3 112 QObject *obj;
b4748b9b
AL
113 va_list ap;
114
115 va_start(ap, string);
4ff18468 116 obj = qobject_from_vjsonf_nofail(string, ap);
b4748b9b
AL
117 va_end(ap);
118
8ff5a7d3 119 return obj;
b4748b9b 120}
1fd825f7 121
4ff18468
MA
122/*
123 * Parse @string as JSON object with %-escapes interpolated.
124 * Abort on error. Do not use with untrusted @string.
125 * Return the resulting QDict. It is never null.
126 */
127QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
128{
129 QDict *qdict;
130
131 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
132 assert(qdict);
133 return qdict;
134}
135
a193352f
MA
136/*
137 * Parse @string as JSON object with %-escapes interpolated.
138 * Abort on error. Do not use with untrusted @string.
139 * Return the resulting QDict. It is never null.
140 */
141QDict *qdict_from_jsonf_nofail(const char *string, ...)
142{
4ff18468 143 QDict *qdict;
a193352f
MA
144 va_list ap;
145
146 va_start(ap, string);
4ff18468 147 qdict = qdict_from_vjsonf_nofail(string, ap);
a193352f 148 va_end(ap);
4ff18468 149 return qdict;
a193352f
MA
150}
151
998da0b1
MA
152static void to_json(JSONWriter *writer, const char *name,
153 const QObject *obj)
1cd7741e 154{
998da0b1
MA
155 switch (qobject_type(obj)) {
156 case QTYPE_QNULL:
157 json_writer_null(writer, name);
158 break;
159 case QTYPE_QNUM: {
160 QNum *val = qobject_to(QNum, obj);
91f54d92 161
998da0b1
MA
162 switch (val->kind) {
163 case QNUM_I64:
164 json_writer_int64(writer, name, val->u.i64);
91f54d92 165 break;
998da0b1
MA
166 case QNUM_U64:
167 json_writer_uint64(writer, name, val->u.u64);
91f54d92 168 break;
998da0b1
MA
169 case QNUM_DOUBLE:
170 json_writer_double(writer, name, val->u.dbl);
91f54d92
MA
171 break;
172 default:
998da0b1 173 abort();
91f54d92 174 }
1fd825f7
AL
175 break;
176 }
177 case QTYPE_QSTRING: {
998da0b1
MA
178 QString *val = qobject_to(QString, obj);
179
180 json_writer_str(writer, name, qstring_get_str(val));
1fd825f7
AL
181 break;
182 }
183 case QTYPE_QDICT: {
7dc847eb 184 QDict *val = qobject_to(QDict, obj);
7b1cd1c6 185 const QDictEntry *entry;
1fd825f7 186
998da0b1 187 json_writer_start_object(writer, name);
7b1cd1c6
MA
188
189 for (entry = qdict_first(val);
190 entry;
191 entry = qdict_next(val, entry)) {
998da0b1 192 to_json(writer, qdict_entry_key(entry), qdict_entry_value(entry));
7b1cd1c6
MA
193 }
194
998da0b1 195 json_writer_end_object(writer);
1fd825f7
AL
196 break;
197 }
198 case QTYPE_QLIST: {
7dc847eb 199 QList *val = qobject_to(QList, obj);
2f2ec111 200 QListEntry *entry;
1fd825f7 201
998da0b1 202 json_writer_start_array(writer, name);
2f2ec111
MA
203
204 QLIST_FOREACH_ENTRY(val, entry) {
998da0b1 205 to_json(writer, NULL, qlist_entry_obj(entry));
2f2ec111
MA
206 }
207
998da0b1 208 json_writer_end_array(writer);
1fd825f7
AL
209 break;
210 }
1fd825f7 211 case QTYPE_QBOOL: {
7dc847eb 212 QBool *val = qobject_to(QBool, obj);
1fd825f7 213
998da0b1 214 json_writer_bool(writer, name, qbool_get_bool(val));
1fd825f7
AL
215 break;
216 }
a7c31816 217 default:
69dd62df 218 abort();
1fd825f7
AL
219 }
220}
221
eab3a467 222GString *qobject_to_json_pretty(const QObject *obj, bool pretty)
1fd825f7 223{
998da0b1 224 JSONWriter *writer = json_writer_new(pretty);
212b6008 225
998da0b1
MA
226 to_json(writer, NULL, obj);
227 return json_writer_get_and_free(writer);
212b6008
DB
228}
229
eab3a467 230GString *qobject_to_json(const QObject *obj)
212b6008 231{
6589f459 232 return qobject_to_json_pretty(obj, false);
1fd825f7 233}