]>
Commit | Line | Data |
---|---|---|
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" |
7b1b5d19 | 17 | #include "qapi/qmp/qjson.h" |
6b673957 | 18 | #include "qapi/qmp/qbool.h" |
452fcdbc | 19 | #include "qapi/qmp/qdict.h" |
47e6b297 | 20 | #include "qapi/qmp/qlist.h" |
15280c36 | 21 | #include "qapi/qmp/qnum.h" |
fc81fa1e | 22 | #include "qapi/qmp/qstring.h" |
f348b6d1 | 23 | #include "qemu/unicode.h" |
b4748b9b AL |
24 | |
25 | typedef struct JSONParsingState | |
26 | { | |
27 | JSONMessageParser parser; | |
b4748b9b | 28 | QObject *result; |
99dbfd1d | 29 | Error *err; |
b4748b9b AL |
30 | } JSONParsingState; |
31 | ||
62815d85 | 32 | static 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 | */ | |
63 | static 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 | 81 | QObject *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 | */ | |
91 | QObject *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 | */ | |
110 | QObject *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 | */ | |
127 | QDict *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 | */ | |
141 | QDict *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 | ||
f1cc129d | 152 | static void json_pretty_newline(GString *accu, bool pretty, int indent) |
1cd7741e | 153 | { |
1cd7741e | 154 | if (pretty) { |
f1cc129d | 155 | g_string_append_printf(accu, "\n%*s", indent * 4, ""); |
1cd7741e MA |
156 | } |
157 | } | |
158 | ||
f1cc129d | 159 | static void to_json(const QObject *obj, GString *accu, bool pretty, int indent) |
1fd825f7 AL |
160 | { |
161 | switch (qobject_type(obj)) { | |
481b002c | 162 | case QTYPE_QNULL: |
f1cc129d | 163 | g_string_append(accu, "null"); |
481b002c | 164 | break; |
01b2ffce | 165 | case QTYPE_QNUM: { |
7dc847eb | 166 | QNum *val = qobject_to(QNum, obj); |
01b2ffce | 167 | char *buffer = qnum_to_string(val); |
f1cc129d | 168 | g_string_append(accu, buffer); |
01b2ffce | 169 | g_free(buffer); |
1fd825f7 AL |
170 | break; |
171 | } | |
172 | case QTYPE_QSTRING: { | |
7dc847eb | 173 | QString *val = qobject_to(QString, obj); |
1fd825f7 | 174 | const char *ptr; |
e2ec3f97 | 175 | int cp; |
e2ec3f97 | 176 | char *end; |
1fd825f7 AL |
177 | |
178 | ptr = qstring_get_str(val); | |
f1cc129d | 179 | g_string_append_c(accu, '"'); |
e2ec3f97 MA |
180 | |
181 | for (; *ptr; ptr = end) { | |
182 | cp = mod_utf8_codepoint(ptr, 6, &end); | |
183 | switch (cp) { | |
184 | case '\"': | |
f1cc129d | 185 | g_string_append(accu, "\\\""); |
e2ec3f97 MA |
186 | break; |
187 | case '\\': | |
f1cc129d | 188 | g_string_append(accu, "\\\\"); |
e2ec3f97 MA |
189 | break; |
190 | case '\b': | |
f1cc129d | 191 | g_string_append(accu, "\\b"); |
e2ec3f97 MA |
192 | break; |
193 | case '\f': | |
f1cc129d | 194 | g_string_append(accu, "\\f"); |
e2ec3f97 MA |
195 | break; |
196 | case '\n': | |
f1cc129d | 197 | g_string_append(accu, "\\n"); |
e2ec3f97 MA |
198 | break; |
199 | case '\r': | |
f1cc129d | 200 | g_string_append(accu, "\\r"); |
e2ec3f97 MA |
201 | break; |
202 | case '\t': | |
f1cc129d | 203 | g_string_append(accu, "\\t"); |
e2ec3f97 MA |
204 | break; |
205 | default: | |
206 | if (cp < 0) { | |
207 | cp = 0xFFFD; /* replacement character */ | |
1fd825f7 | 208 | } |
e2ec3f97 MA |
209 | if (cp > 0xFFFF) { |
210 | /* beyond BMP; need a surrogate pair */ | |
f1cc129d MA |
211 | g_string_append_printf(accu, "\\u%04X\\u%04X", |
212 | 0xD800 + ((cp - 0x10000) >> 10), | |
213 | 0xDC00 + ((cp - 0x10000) & 0x3FF)); | |
e2ec3f97 | 214 | } else if (cp < 0x20 || cp >= 0x7F) { |
f1cc129d | 215 | g_string_append_printf(accu, "\\u%04X", cp); |
e2ec3f97 | 216 | } else { |
f1cc129d | 217 | g_string_append_c(accu, cp); |
1fd825f7 | 218 | } |
e2ec3f97 MA |
219 | } |
220 | }; | |
221 | ||
f1cc129d | 222 | g_string_append_c(accu, '"'); |
1fd825f7 AL |
223 | break; |
224 | } | |
225 | case QTYPE_QDICT: { | |
7dc847eb | 226 | QDict *val = qobject_to(QDict, obj); |
7b1cd1c6 MA |
227 | const char *comma = pretty ? "," : ", "; |
228 | const char *sep = ""; | |
229 | const QDictEntry *entry; | |
230 | QString *qkey; | |
1fd825f7 | 231 | |
f1cc129d | 232 | g_string_append_c(accu, '{'); |
7b1cd1c6 MA |
233 | |
234 | for (entry = qdict_first(val); | |
235 | entry; | |
236 | entry = qdict_next(val, entry)) { | |
f1cc129d MA |
237 | g_string_append(accu, sep); |
238 | json_pretty_newline(accu, pretty, indent + 1); | |
7b1cd1c6 MA |
239 | |
240 | qkey = qstring_from_str(qdict_entry_key(entry)); | |
f1cc129d | 241 | to_json(QOBJECT(qkey), accu, pretty, indent + 1); |
7b1cd1c6 MA |
242 | qobject_unref(qkey); |
243 | ||
f1cc129d MA |
244 | g_string_append(accu, ": "); |
245 | to_json(qdict_entry_value(entry), accu, pretty, indent + 1); | |
7b1cd1c6 MA |
246 | sep = comma; |
247 | } | |
248 | ||
f1cc129d MA |
249 | json_pretty_newline(accu, pretty, indent); |
250 | g_string_append_c(accu, '}'); | |
1fd825f7 AL |
251 | break; |
252 | } | |
253 | case QTYPE_QLIST: { | |
7dc847eb | 254 | QList *val = qobject_to(QList, obj); |
2f2ec111 MA |
255 | const char *comma = pretty ? "," : ", "; |
256 | const char *sep = ""; | |
257 | QListEntry *entry; | |
1fd825f7 | 258 | |
f1cc129d | 259 | g_string_append_c(accu, '['); |
2f2ec111 MA |
260 | |
261 | QLIST_FOREACH_ENTRY(val, entry) { | |
f1cc129d MA |
262 | g_string_append(accu, sep); |
263 | json_pretty_newline(accu, pretty, indent + 1); | |
264 | to_json(qlist_entry_obj(entry), accu, pretty, indent + 1); | |
2f2ec111 MA |
265 | sep = comma; |
266 | } | |
267 | ||
f1cc129d MA |
268 | json_pretty_newline(accu, pretty, indent); |
269 | g_string_append_c(accu, ']'); | |
1fd825f7 AL |
270 | break; |
271 | } | |
1fd825f7 | 272 | case QTYPE_QBOOL: { |
7dc847eb | 273 | QBool *val = qobject_to(QBool, obj); |
1fd825f7 | 274 | |
fc48ffc3 | 275 | if (qbool_get_bool(val)) { |
f1cc129d | 276 | g_string_append(accu, "true"); |
1fd825f7 | 277 | } else { |
f1cc129d | 278 | g_string_append(accu, "false"); |
1fd825f7 AL |
279 | } |
280 | break; | |
281 | } | |
a7c31816 | 282 | default: |
69dd62df | 283 | abort(); |
1fd825f7 AL |
284 | } |
285 | } | |
286 | ||
6589f459 | 287 | QString *qobject_to_json_pretty(const QObject *obj, bool pretty) |
1fd825f7 | 288 | { |
f1cc129d | 289 | GString *accu = g_string_new(NULL); |
212b6008 | 290 | |
f1cc129d MA |
291 | to_json(obj, accu, pretty, 0); |
292 | return qstring_from_gstring(accu); | |
212b6008 DB |
293 | } |
294 | ||
6589f459 | 295 | QString *qobject_to_json(const QObject *obj) |
212b6008 | 296 | { |
6589f459 | 297 | return qobject_to_json_pretty(obj, false); |
1fd825f7 | 298 | } |