hw/block/nvme: Document zoned parameters in usage text
[qemu/rayw.git] / qobject / qjson.c
blob167fcb429c90bd7ed78b6f3bf71218ac35f02b4c
1 /*
2 * QObject JSON integration
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/json-parser.h"
17 #include "qapi/qmp/json-writer.h"
18 #include "qapi/qmp/qjson.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qlist.h"
22 #include "qapi/qmp/qnum.h"
23 #include "qapi/qmp/qstring.h"
25 typedef struct JSONParsingState {
26 JSONMessageParser parser;
27 QObject *result;
28 Error *err;
29 } JSONParsingState;
31 static void consume_json(void *opaque, QObject *json, Error *err)
33 JSONParsingState *s = opaque;
35 assert(!json != !err);
36 assert(!s->result || !s->err);
38 if (s->result) {
39 qobject_unref(s->result);
40 s->result = NULL;
41 error_setg(&s->err, "Expecting at most one JSON value");
43 if (s->err) {
44 qobject_unref(json);
45 error_free(err);
46 return;
48 s->result = json;
49 s->err = err;
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.
62 static QObject *qobject_from_jsonv(const char *string, va_list *ap,
63 Error **errp)
65 JSONParsingState state = {};
67 json_message_parser_init(&state.parser, consume_json, &state, ap);
68 json_message_parser_feed(&state.parser, string, strlen(string));
69 json_message_parser_flush(&state.parser);
70 json_message_parser_destroy(&state.parser);
72 if (!state.result && !state.err) {
73 error_setg(&state.err, "Expecting a JSON value");
76 error_propagate(errp, state.err);
77 return state.result;
80 QObject *qobject_from_json(const char *string, Error **errp)
82 return qobject_from_jsonv(string, NULL, errp);
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.
90 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
92 va_list ap_copy;
93 QObject *obj;
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);
100 assert(obj);
101 return obj;
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.
109 QObject *qobject_from_jsonf_nofail(const char *string, ...)
111 QObject *obj;
112 va_list ap;
114 va_start(ap, string);
115 obj = qobject_from_vjsonf_nofail(string, ap);
116 va_end(ap);
118 return obj;
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.
126 QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
128 QDict *qdict;
130 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
131 assert(qdict);
132 return qdict;
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.
140 QDict *qdict_from_jsonf_nofail(const char *string, ...)
142 QDict *qdict;
143 va_list ap;
145 va_start(ap, string);
146 qdict = qdict_from_vjsonf_nofail(string, ap);
147 va_end(ap);
148 return qdict;
151 static void to_json(JSONWriter *writer, const char *name,
152 const QObject *obj)
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);
161 switch (val->kind) {
162 case QNUM_I64:
163 json_writer_int64(writer, name, val->u.i64);
164 break;
165 case QNUM_U64:
166 json_writer_uint64(writer, name, val->u.u64);
167 break;
168 case QNUM_DOUBLE:
169 json_writer_double(writer, name, val->u.dbl);
170 break;
171 default:
172 abort();
174 break;
176 case QTYPE_QSTRING: {
177 QString *val = qobject_to(QString, obj);
179 json_writer_str(writer, name, qstring_get_str(val));
180 break;
182 case QTYPE_QDICT: {
183 QDict *val = qobject_to(QDict, obj);
184 const QDictEntry *entry;
186 json_writer_start_object(writer, name);
188 for (entry = qdict_first(val);
189 entry;
190 entry = qdict_next(val, entry)) {
191 to_json(writer, qdict_entry_key(entry), qdict_entry_value(entry));
194 json_writer_end_object(writer);
195 break;
197 case QTYPE_QLIST: {
198 QList *val = qobject_to(QList, obj);
199 QListEntry *entry;
201 json_writer_start_array(writer, name);
203 QLIST_FOREACH_ENTRY(val, entry) {
204 to_json(writer, NULL, qlist_entry_obj(entry));
207 json_writer_end_array(writer);
208 break;
210 case QTYPE_QBOOL: {
211 QBool *val = qobject_to(QBool, obj);
213 json_writer_bool(writer, name, qbool_get_bool(val));
214 break;
216 default:
217 abort();
221 GString *qobject_to_json_pretty(const QObject *obj, bool pretty)
223 JSONWriter *writer = json_writer_new(pretty);
225 to_json(writer, NULL, obj);
226 return json_writer_get_and_free(writer);
229 GString *qobject_to_json(const QObject *obj)
231 return qobject_to_json_pretty(obj, false);