vapic: use tcg_enabled
[qemu.git] / qobject / qjson.c
blob2e0930884e5dc7224e879ef1567f18d85cd6558a
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-lexer.h"
17 #include "qapi/qmp/json-parser.h"
18 #include "qapi/qmp/json-streamer.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/types.h"
21 #include "qemu/unicode.h"
23 typedef struct JSONParsingState
25 JSONMessageParser parser;
26 va_list *ap;
27 QObject *result;
28 Error *err;
29 } JSONParsingState;
31 static void parse_json(JSONMessageParser *parser, GQueue *tokens)
33 JSONParsingState *s = container_of(parser, JSONParsingState, parser);
35 s->result = json_parser_parse_err(tokens, s->ap, &s->err);
38 QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
40 JSONParsingState state = {};
42 state.ap = ap;
44 json_message_parser_init(&state.parser, parse_json);
45 json_message_parser_feed(&state.parser, string, strlen(string));
46 json_message_parser_flush(&state.parser);
47 json_message_parser_destroy(&state.parser);
49 error_propagate(errp, state.err);
50 return state.result;
53 QObject *qobject_from_json(const char *string, Error **errp)
55 return qobject_from_jsonv(string, NULL, errp);
59 * IMPORTANT: This function aborts on error, thus it must not
60 * be used with untrusted arguments.
62 QObject *qobject_from_jsonf(const char *string, ...)
64 QObject *obj;
65 va_list ap;
67 va_start(ap, string);
68 obj = qobject_from_jsonv(string, &ap, &error_abort);
69 va_end(ap);
71 assert(obj != NULL);
72 return obj;
75 typedef struct ToJsonIterState
77 int indent;
78 int pretty;
79 int count;
80 QString *str;
81 } ToJsonIterState;
83 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
85 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
87 ToJsonIterState *s = opaque;
88 QString *qkey;
89 int j;
91 if (s->count) {
92 qstring_append(s->str, s->pretty ? "," : ", ");
95 if (s->pretty) {
96 qstring_append(s->str, "\n");
97 for (j = 0 ; j < s->indent ; j++)
98 qstring_append(s->str, " ");
101 qkey = qstring_from_str(key);
102 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
103 QDECREF(qkey);
105 qstring_append(s->str, ": ");
106 to_json(obj, s->str, s->pretty, s->indent);
107 s->count++;
110 static void to_json_list_iter(QObject *obj, void *opaque)
112 ToJsonIterState *s = opaque;
113 int j;
115 if (s->count) {
116 qstring_append(s->str, s->pretty ? "," : ", ");
119 if (s->pretty) {
120 qstring_append(s->str, "\n");
121 for (j = 0 ; j < s->indent ; j++)
122 qstring_append(s->str, " ");
125 to_json(obj, s->str, s->pretty, s->indent);
126 s->count++;
129 static void to_json(const QObject *obj, QString *str, int pretty, int indent)
131 switch (qobject_type(obj)) {
132 case QTYPE_QNULL:
133 qstring_append(str, "null");
134 break;
135 case QTYPE_QNUM: {
136 QNum *val = qobject_to_qnum(obj);
137 char *buffer = qnum_to_string(val);
138 qstring_append(str, buffer);
139 g_free(buffer);
140 break;
142 case QTYPE_QSTRING: {
143 QString *val = qobject_to_qstring(obj);
144 const char *ptr;
145 int cp;
146 char buf[16];
147 char *end;
149 ptr = qstring_get_str(val);
150 qstring_append(str, "\"");
152 for (; *ptr; ptr = end) {
153 cp = mod_utf8_codepoint(ptr, 6, &end);
154 switch (cp) {
155 case '\"':
156 qstring_append(str, "\\\"");
157 break;
158 case '\\':
159 qstring_append(str, "\\\\");
160 break;
161 case '\b':
162 qstring_append(str, "\\b");
163 break;
164 case '\f':
165 qstring_append(str, "\\f");
166 break;
167 case '\n':
168 qstring_append(str, "\\n");
169 break;
170 case '\r':
171 qstring_append(str, "\\r");
172 break;
173 case '\t':
174 qstring_append(str, "\\t");
175 break;
176 default:
177 if (cp < 0) {
178 cp = 0xFFFD; /* replacement character */
180 if (cp > 0xFFFF) {
181 /* beyond BMP; need a surrogate pair */
182 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
183 0xD800 + ((cp - 0x10000) >> 10),
184 0xDC00 + ((cp - 0x10000) & 0x3FF));
185 } else if (cp < 0x20 || cp >= 0x7F) {
186 snprintf(buf, sizeof(buf), "\\u%04X", cp);
187 } else {
188 buf[0] = cp;
189 buf[1] = 0;
191 qstring_append(str, buf);
195 qstring_append(str, "\"");
196 break;
198 case QTYPE_QDICT: {
199 ToJsonIterState s;
200 QDict *val = qobject_to_qdict(obj);
202 s.count = 0;
203 s.str = str;
204 s.indent = indent + 1;
205 s.pretty = pretty;
206 qstring_append(str, "{");
207 qdict_iter(val, to_json_dict_iter, &s);
208 if (pretty) {
209 int j;
210 qstring_append(str, "\n");
211 for (j = 0 ; j < indent ; j++)
212 qstring_append(str, " ");
214 qstring_append(str, "}");
215 break;
217 case QTYPE_QLIST: {
218 ToJsonIterState s;
219 QList *val = qobject_to_qlist(obj);
221 s.count = 0;
222 s.str = str;
223 s.indent = indent + 1;
224 s.pretty = pretty;
225 qstring_append(str, "[");
226 qlist_iter(val, (void *)to_json_list_iter, &s);
227 if (pretty) {
228 int j;
229 qstring_append(str, "\n");
230 for (j = 0 ; j < indent ; j++)
231 qstring_append(str, " ");
233 qstring_append(str, "]");
234 break;
236 case QTYPE_QBOOL: {
237 QBool *val = qobject_to_qbool(obj);
239 if (qbool_get_bool(val)) {
240 qstring_append(str, "true");
241 } else {
242 qstring_append(str, "false");
244 break;
246 default:
247 abort();
251 QString *qobject_to_json(const QObject *obj)
253 QString *str = qstring_new();
255 to_json(obj, str, 0, 0);
257 return str;
260 QString *qobject_to_json_pretty(const QObject *obj)
262 QString *str = qstring_new();
264 to_json(obj, str, 1, 0);
266 return str;