virtio: do not reset msix state on soft reset
[qemu/aliguori-queue.git] / qjson.c
blob7270909afa12173aece62e60a713fdb23e696dcb
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 "json-lexer.h"
15 #include "json-parser.h"
16 #include "json-streamer.h"
17 #include "qjson.h"
18 #include "qint.h"
19 #include "qlist.h"
20 #include "qbool.h"
21 #include "qfloat.h"
22 #include "qdict.h"
24 typedef struct JSONParsingState
26 JSONMessageParser parser;
27 va_list *ap;
28 QObject *result;
29 } JSONParsingState;
31 static void parse_json(JSONMessageParser *parser, QList *tokens)
33 JSONParsingState *s = container_of(parser, JSONParsingState, parser);
34 s->result = json_parser_parse(tokens, s->ap);
37 QObject *qobject_from_json(const char *string)
39 JSONParsingState state = {};
41 json_message_parser_init(&state.parser, parse_json);
42 json_message_parser_feed(&state.parser, string, strlen(string));
43 json_message_parser_flush(&state.parser);
44 json_message_parser_destroy(&state.parser);
46 return state.result;
49 QObject *qobject_from_jsonf(const char *string, ...)
51 JSONParsingState state = {};
52 va_list ap;
54 va_start(ap, string);
55 state.ap = &ap;
57 json_message_parser_init(&state.parser, parse_json);
58 json_message_parser_feed(&state.parser, string, strlen(string));
59 json_message_parser_flush(&state.parser);
60 json_message_parser_destroy(&state.parser);
62 va_end(ap);
64 return state.result;
67 typedef struct ToJsonIterState
69 int count;
70 QString *str;
71 } ToJsonIterState;
73 static void to_json(const QObject *obj, QString *str);
75 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
77 ToJsonIterState *s = opaque;
78 QString *qkey;
80 if (s->count) {
81 qstring_append(s->str, ", ");
84 qkey = qstring_from_str(key);
85 to_json(QOBJECT(qkey), s->str);
86 QDECREF(qkey);
88 qstring_append(s->str, ": ");
89 to_json(obj, s->str);
90 s->count++;
93 static void to_json_list_iter(QObject *obj, void *opaque)
95 ToJsonIterState *s = opaque;
97 if (s->count) {
98 qstring_append(s->str, ", ");
101 to_json(obj, s->str);
102 s->count++;
105 static void to_json(const QObject *obj, QString *str)
107 switch (qobject_type(obj)) {
108 case QTYPE_QINT: {
109 QInt *val = qobject_to_qint(obj);
110 char buffer[1024];
112 snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
113 qstring_append(str, buffer);
114 break;
116 case QTYPE_QSTRING: {
117 QString *val = qobject_to_qstring(obj);
118 const char *ptr;
120 ptr = qstring_get_str(val);
121 qstring_append(str, "\"");
122 while (*ptr) {
123 if ((ptr[0] & 0xE0) == 0xE0 &&
124 (ptr[1] & 0x80) && (ptr[2] & 0x80)) {
125 uint16_t wchar;
126 char escape[7];
128 wchar = (ptr[0] & 0x0F) << 12;
129 wchar |= (ptr[1] & 0x3F) << 6;
130 wchar |= (ptr[2] & 0x3F);
131 ptr += 2;
133 snprintf(escape, sizeof(escape), "\\u%04X", wchar);
134 qstring_append(str, escape);
135 } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {
136 uint16_t wchar;
137 char escape[7];
139 wchar = (ptr[0] & 0x1F) << 6;
140 wchar |= (ptr[1] & 0x3F);
141 ptr++;
143 snprintf(escape, sizeof(escape), "\\u%04X", wchar);
144 qstring_append(str, escape);
145 } else switch (ptr[0]) {
146 case '\"':
147 qstring_append(str, "\\\"");
148 break;
149 case '\\':
150 qstring_append(str, "\\\\");
151 break;
152 case '\b':
153 qstring_append(str, "\\b");
154 break;
155 case '\n':
156 qstring_append(str, "\\n");
157 break;
158 case '\r':
159 qstring_append(str, "\\r");
160 break;
161 case '\t':
162 qstring_append(str, "\\t");
163 break;
164 default: {
165 char buf[2] = { ptr[0], 0 };
166 qstring_append(str, buf);
167 break;
170 ptr++;
172 qstring_append(str, "\"");
173 break;
175 case QTYPE_QDICT: {
176 ToJsonIterState s;
177 QDict *val = qobject_to_qdict(obj);
179 s.count = 0;
180 s.str = str;
181 qstring_append(str, "{");
182 qdict_iter(val, to_json_dict_iter, &s);
183 qstring_append(str, "}");
184 break;
186 case QTYPE_QLIST: {
187 ToJsonIterState s;
188 QList *val = qobject_to_qlist(obj);
190 s.count = 0;
191 s.str = str;
192 qstring_append(str, "[");
193 qlist_iter(val, (void *)to_json_list_iter, &s);
194 qstring_append(str, "]");
195 break;
197 case QTYPE_QFLOAT: {
198 QFloat *val = qobject_to_qfloat(obj);
199 char buffer[1024];
200 int len;
202 len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
203 while (len > 0 && buffer[len - 1] == '0') {
204 len--;
207 if (len && buffer[len - 1] == '.') {
208 buffer[len - 1] = 0;
209 } else {
210 buffer[len] = 0;
213 qstring_append(str, buffer);
214 break;
216 case QTYPE_QBOOL: {
217 QBool *val = qobject_to_qbool(obj);
219 if (qbool_get_int(val)) {
220 qstring_append(str, "true");
221 } else {
222 qstring_append(str, "false");
224 break;
226 case QTYPE_NONE:
227 break;
231 QString *qobject_to_json(const QObject *obj)
233 QString *str = qstring_new();
235 to_json(obj, str);
237 return str;