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