xref: /openbmc/qemu/qobject/qjson.c (revision f1f7e4bf)
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 
14 #include "qapi/qmp/json-lexer.h"
15 #include "qapi/qmp/json-parser.h"
16 #include "qapi/qmp/json-streamer.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qint.h"
19 #include "qapi/qmp/qlist.h"
20 #include "qapi/qmp/qbool.h"
21 #include "qapi/qmp/qfloat.h"
22 #include "qapi/qmp/qdict.h"
23 
24 typedef struct JSONParsingState
25 {
26     JSONMessageParser parser;
27     va_list *ap;
28     QObject *result;
29 } JSONParsingState;
30 
31 static void parse_json(JSONMessageParser *parser, GQueue *tokens)
32 {
33     JSONParsingState *s = container_of(parser, JSONParsingState, parser);
34     s->result = json_parser_parse(tokens, s->ap);
35 }
36 
37 QObject *qobject_from_jsonv(const char *string, va_list *ap)
38 {
39     JSONParsingState state = {};
40 
41     state.ap = ap;
42 
43     json_message_parser_init(&state.parser, parse_json);
44     json_message_parser_feed(&state.parser, string, strlen(string));
45     json_message_parser_flush(&state.parser);
46     json_message_parser_destroy(&state.parser);
47 
48     return state.result;
49 }
50 
51 QObject *qobject_from_json(const char *string)
52 {
53     return qobject_from_jsonv(string, NULL);
54 }
55 
56 /*
57  * IMPORTANT: This function aborts on error, thus it must not
58  * be used with untrusted arguments.
59  */
60 QObject *qobject_from_jsonf(const char *string, ...)
61 {
62     QObject *obj;
63     va_list ap;
64 
65     va_start(ap, string);
66     obj = qobject_from_jsonv(string, &ap);
67     va_end(ap);
68 
69     assert(obj != NULL);
70     return obj;
71 }
72 
73 typedef struct ToJsonIterState
74 {
75     int indent;
76     int pretty;
77     int count;
78     QString *str;
79 } ToJsonIterState;
80 
81 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
82 
83 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
84 {
85     ToJsonIterState *s = opaque;
86     QString *qkey;
87     int j;
88 
89     if (s->count) {
90         qstring_append(s->str, s->pretty ? "," : ", ");
91     }
92 
93     if (s->pretty) {
94         qstring_append(s->str, "\n");
95         for (j = 0 ; j < s->indent ; j++)
96             qstring_append(s->str, "    ");
97     }
98 
99     qkey = qstring_from_str(key);
100     to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
101     QDECREF(qkey);
102 
103     qstring_append(s->str, ": ");
104     to_json(obj, s->str, s->pretty, s->indent);
105     s->count++;
106 }
107 
108 static void to_json_list_iter(QObject *obj, void *opaque)
109 {
110     ToJsonIterState *s = opaque;
111     int j;
112 
113     if (s->count) {
114         qstring_append(s->str, s->pretty ? "," : ", ");
115     }
116 
117     if (s->pretty) {
118         qstring_append(s->str, "\n");
119         for (j = 0 ; j < s->indent ; j++)
120             qstring_append(s->str, "    ");
121     }
122 
123     to_json(obj, s->str, s->pretty, s->indent);
124     s->count++;
125 }
126 
127 static void to_json(const QObject *obj, QString *str, int pretty, int indent)
128 {
129     switch (qobject_type(obj)) {
130     case QTYPE_QNULL:
131         qstring_append(str, "null");
132         break;
133     case QTYPE_QINT: {
134         QInt *val = qobject_to_qint(obj);
135         char buffer[1024];
136 
137         snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
138         qstring_append(str, buffer);
139         break;
140     }
141     case QTYPE_QSTRING: {
142         QString *val = qobject_to_qstring(obj);
143         const char *ptr;
144         int cp;
145         char buf[16];
146         char *end;
147 
148         ptr = qstring_get_str(val);
149         qstring_append(str, "\"");
150 
151         for (; *ptr; ptr = end) {
152             cp = mod_utf8_codepoint(ptr, 6, &end);
153             switch (cp) {
154             case '\"':
155                 qstring_append(str, "\\\"");
156                 break;
157             case '\\':
158                 qstring_append(str, "\\\\");
159                 break;
160             case '\b':
161                 qstring_append(str, "\\b");
162                 break;
163             case '\f':
164                 qstring_append(str, "\\f");
165                 break;
166             case '\n':
167                 qstring_append(str, "\\n");
168                 break;
169             case '\r':
170                 qstring_append(str, "\\r");
171                 break;
172             case '\t':
173                 qstring_append(str, "\\t");
174                 break;
175             default:
176                 if (cp < 0) {
177                     cp = 0xFFFD; /* replacement character */
178                 }
179                 if (cp > 0xFFFF) {
180                     /* beyond BMP; need a surrogate pair */
181                     snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
182                              0xD800 + ((cp - 0x10000) >> 10),
183                              0xDC00 + ((cp - 0x10000) & 0x3FF));
184                 } else if (cp < 0x20 || cp >= 0x7F) {
185                     snprintf(buf, sizeof(buf), "\\u%04X", cp);
186                 } else {
187                     buf[0] = cp;
188                     buf[1] = 0;
189                 }
190                 qstring_append(str, buf);
191             }
192         };
193 
194         qstring_append(str, "\"");
195         break;
196     }
197     case QTYPE_QDICT: {
198         ToJsonIterState s;
199         QDict *val = qobject_to_qdict(obj);
200 
201         s.count = 0;
202         s.str = str;
203         s.indent = indent + 1;
204         s.pretty = pretty;
205         qstring_append(str, "{");
206         qdict_iter(val, to_json_dict_iter, &s);
207         if (pretty) {
208             int j;
209             qstring_append(str, "\n");
210             for (j = 0 ; j < indent ; j++)
211                 qstring_append(str, "    ");
212         }
213         qstring_append(str, "}");
214         break;
215     }
216     case QTYPE_QLIST: {
217         ToJsonIterState s;
218         QList *val = qobject_to_qlist(obj);
219 
220         s.count = 0;
221         s.str = str;
222         s.indent = indent + 1;
223         s.pretty = pretty;
224         qstring_append(str, "[");
225         qlist_iter(val, (void *)to_json_list_iter, &s);
226         if (pretty) {
227             int j;
228             qstring_append(str, "\n");
229             for (j = 0 ; j < indent ; j++)
230                 qstring_append(str, "    ");
231         }
232         qstring_append(str, "]");
233         break;
234     }
235     case QTYPE_QFLOAT: {
236         QFloat *val = qobject_to_qfloat(obj);
237         char buffer[1024];
238         int len;
239 
240         len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
241         while (len > 0 && buffer[len - 1] == '0') {
242             len--;
243         }
244 
245         if (len && buffer[len - 1] == '.') {
246             buffer[len - 1] = 0;
247         } else {
248             buffer[len] = 0;
249         }
250 
251         qstring_append(str, buffer);
252         break;
253     }
254     case QTYPE_QBOOL: {
255         QBool *val = qobject_to_qbool(obj);
256 
257         if (qbool_get_bool(val)) {
258             qstring_append(str, "true");
259         } else {
260             qstring_append(str, "false");
261         }
262         break;
263     }
264     default:
265         abort();
266     }
267 }
268 
269 QString *qobject_to_json(const QObject *obj)
270 {
271     QString *str = qstring_new();
272 
273     to_json(obj, str, 0, 0);
274 
275     return str;
276 }
277 
278 QString *qobject_to_json_pretty(const QObject *obj)
279 {
280     QString *str = qstring_new();
281 
282     to_json(obj, str, 1, 0);
283 
284     return str;
285 }
286