darwin: remove redundant dependency declaration
[qemu/ar7.git] / qobject / qjson.c
blobbcc376e6267ea7688c87c73869f9c50a635d14bc
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
27 JSONMessageParser parser;
28 QObject *result;
29 Error *err;
30 } JSONParsingState;
32 static void consume_json(void *opaque, QObject *json, Error *err)
34 JSONParsingState *s = opaque;
36 assert(!json != !err);
37 assert(!s->result || !s->err);
39 if (s->result) {
40 qobject_unref(s->result);
41 s->result = NULL;
42 error_setg(&s->err, "Expecting at most one JSON value");
44 if (s->err) {
45 qobject_unref(json);
46 error_free(err);
47 return;
49 s->result = json;
50 s->err = err;
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.
63 static QObject *qobject_from_jsonv(const char *string, va_list *ap,
64 Error **errp)
66 JSONParsingState state = {};
68 json_message_parser_init(&state.parser, consume_json, &state, ap);
69 json_message_parser_feed(&state.parser, string, strlen(string));
70 json_message_parser_flush(&state.parser);
71 json_message_parser_destroy(&state.parser);
73 if (!state.result && !state.err) {
74 error_setg(&state.err, "Expecting a JSON value");
77 error_propagate(errp, state.err);
78 return state.result;
81 QObject *qobject_from_json(const char *string, Error **errp)
83 return qobject_from_jsonv(string, NULL, errp);
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.
91 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
93 va_list ap_copy;
94 QObject *obj;
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);
101 assert(obj);
102 return obj;
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.
110 QObject *qobject_from_jsonf_nofail(const char *string, ...)
112 QObject *obj;
113 va_list ap;
115 va_start(ap, string);
116 obj = qobject_from_vjsonf_nofail(string, ap);
117 va_end(ap);
119 return obj;
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.
127 QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
129 QDict *qdict;
131 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
132 assert(qdict);
133 return qdict;
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.
141 QDict *qdict_from_jsonf_nofail(const char *string, ...)
143 QDict *qdict;
144 va_list ap;
146 va_start(ap, string);
147 qdict = qdict_from_vjsonf_nofail(string, ap);
148 va_end(ap);
149 return qdict;
152 static void to_json(JSONWriter *writer, const char *name,
153 const QObject *obj)
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);
162 switch (val->kind) {
163 case QNUM_I64:
164 json_writer_int64(writer, name, val->u.i64);
165 break;
166 case QNUM_U64:
167 json_writer_uint64(writer, name, val->u.u64);
168 break;
169 case QNUM_DOUBLE:
170 json_writer_double(writer, name, val->u.dbl);
171 break;
172 default:
173 abort();
175 break;
177 case QTYPE_QSTRING: {
178 QString *val = qobject_to(QString, obj);
180 json_writer_str(writer, name, qstring_get_str(val));
181 break;
183 case QTYPE_QDICT: {
184 QDict *val = qobject_to(QDict, obj);
185 const QDictEntry *entry;
187 json_writer_start_object(writer, name);
189 for (entry = qdict_first(val);
190 entry;
191 entry = qdict_next(val, entry)) {
192 to_json(writer, qdict_entry_key(entry), qdict_entry_value(entry));
195 json_writer_end_object(writer);
196 break;
198 case QTYPE_QLIST: {
199 QList *val = qobject_to(QList, obj);
200 QListEntry *entry;
202 json_writer_start_array(writer, name);
204 QLIST_FOREACH_ENTRY(val, entry) {
205 to_json(writer, NULL, qlist_entry_obj(entry));
208 json_writer_end_array(writer);
209 break;
211 case QTYPE_QBOOL: {
212 QBool *val = qobject_to(QBool, obj);
214 json_writer_bool(writer, name, qbool_get_bool(val));
215 break;
217 default:
218 abort();
222 GString *qobject_to_json_pretty(const QObject *obj, bool pretty)
224 JSONWriter *writer = json_writer_new(pretty);
226 to_json(writer, NULL, obj);
227 return json_writer_get_and_free(writer);
230 GString *qobject_to_json(const QObject *obj)
232 return qobject_to_json_pretty(obj, false);