Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 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 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Markus Armbruster | 99dbfd1 | 2017-02-28 22:26:54 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 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" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 21 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 22 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 23 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/qstring.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 25 | #include "qemu/unicode.h" |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 26 | |
| 27 | typedef struct JSONParsingState |
| 28 | { |
| 29 | JSONMessageParser parser; |
| 30 | va_list *ap; |
| 31 | QObject *result; |
Markus Armbruster | 99dbfd1 | 2017-02-28 22:26:54 +0100 | [diff] [blame] | 32 | Error *err; |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 33 | } JSONParsingState; |
| 34 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 35 | static void parse_json(JSONMessageParser *parser, GQueue *tokens) |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 36 | { |
| 37 | JSONParsingState *s = container_of(parser, JSONParsingState, parser); |
Markus Armbruster | 99dbfd1 | 2017-02-28 22:26:54 +0100 | [diff] [blame] | 38 | |
| 39 | s->result = json_parser_parse_err(tokens, s->ap, &s->err); |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Markus Armbruster | 99dbfd1 | 2017-02-28 22:26:54 +0100 | [diff] [blame] | 42 | QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp) |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 43 | { |
| 44 | JSONParsingState state = {}; |
| 45 | |
Luiz Capitulino | 8ff5a7d | 2009-11-18 23:05:24 -0200 | [diff] [blame] | 46 | state.ap = ap; |
| 47 | |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 48 | json_message_parser_init(&state.parser, parse_json); |
| 49 | json_message_parser_feed(&state.parser, string, strlen(string)); |
| 50 | json_message_parser_flush(&state.parser); |
| 51 | json_message_parser_destroy(&state.parser); |
| 52 | |
Markus Armbruster | 99dbfd1 | 2017-02-28 22:26:54 +0100 | [diff] [blame] | 53 | error_propagate(errp, state.err); |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 54 | return state.result; |
| 55 | } |
| 56 | |
Markus Armbruster | 57348c2 | 2017-02-28 22:26:58 +0100 | [diff] [blame] | 57 | QObject *qobject_from_json(const char *string, Error **errp) |
Luiz Capitulino | 8ff5a7d | 2009-11-18 23:05:24 -0200 | [diff] [blame] | 58 | { |
Markus Armbruster | 57348c2 | 2017-02-28 22:26:58 +0100 | [diff] [blame] | 59 | return qobject_from_jsonv(string, NULL, errp); |
Luiz Capitulino | 8ff5a7d | 2009-11-18 23:05:24 -0200 | [diff] [blame] | 60 | } |
| 61 | |
Luiz Capitulino | 668e3ca | 2010-02-08 17:01:27 -0200 | [diff] [blame] | 62 | /* |
| 63 | * IMPORTANT: This function aborts on error, thus it must not |
| 64 | * be used with untrusted arguments. |
| 65 | */ |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 66 | QObject *qobject_from_jsonf(const char *string, ...) |
| 67 | { |
Luiz Capitulino | 8ff5a7d | 2009-11-18 23:05:24 -0200 | [diff] [blame] | 68 | QObject *obj; |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 69 | va_list ap; |
| 70 | |
| 71 | va_start(ap, string); |
Markus Armbruster | ea5ef5c | 2017-02-28 22:26:56 +0100 | [diff] [blame] | 72 | obj = qobject_from_jsonv(string, &ap, &error_abort); |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 73 | va_end(ap); |
| 74 | |
Luiz Capitulino | 668e3ca | 2010-02-08 17:01:27 -0200 | [diff] [blame] | 75 | assert(obj != NULL); |
Luiz Capitulino | 8ff5a7d | 2009-11-18 23:05:24 -0200 | [diff] [blame] | 76 | return obj; |
Anthony Liguori | b4748b9 | 2009-11-11 10:42:41 -0600 | [diff] [blame] | 77 | } |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 78 | |
| 79 | typedef struct ToJsonIterState |
| 80 | { |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 81 | int indent; |
| 82 | int pretty; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 83 | int count; |
| 84 | QString *str; |
| 85 | } ToJsonIterState; |
| 86 | |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 87 | static void to_json(const QObject *obj, QString *str, int pretty, int indent); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 88 | |
| 89 | static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) |
| 90 | { |
| 91 | ToJsonIterState *s = opaque; |
| 92 | QString *qkey; |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 93 | int j; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 94 | |
Max Reitz | 4b58554 | 2014-11-21 10:29:59 +0100 | [diff] [blame] | 95 | if (s->count) { |
| 96 | qstring_append(s->str, s->pretty ? "," : ", "); |
| 97 | } |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 98 | |
| 99 | if (s->pretty) { |
| 100 | qstring_append(s->str, "\n"); |
| 101 | for (j = 0 ; j < s->indent ; j++) |
| 102 | qstring_append(s->str, " "); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | qkey = qstring_from_str(key); |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 106 | to_json(QOBJECT(qkey), s->str, s->pretty, s->indent); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 107 | QDECREF(qkey); |
| 108 | |
| 109 | qstring_append(s->str, ": "); |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 110 | to_json(obj, s->str, s->pretty, s->indent); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 111 | s->count++; |
| 112 | } |
| 113 | |
| 114 | static void to_json_list_iter(QObject *obj, void *opaque) |
| 115 | { |
| 116 | ToJsonIterState *s = opaque; |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 117 | int j; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 118 | |
Max Reitz | 4b58554 | 2014-11-21 10:29:59 +0100 | [diff] [blame] | 119 | if (s->count) { |
| 120 | qstring_append(s->str, s->pretty ? "," : ", "); |
| 121 | } |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 122 | |
| 123 | if (s->pretty) { |
| 124 | qstring_append(s->str, "\n"); |
| 125 | for (j = 0 ; j < s->indent ; j++) |
| 126 | qstring_append(s->str, " "); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 129 | to_json(obj, s->str, s->pretty, s->indent); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 130 | s->count++; |
| 131 | } |
| 132 | |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 133 | static void to_json(const QObject *obj, QString *str, int pretty, int indent) |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 134 | { |
| 135 | switch (qobject_type(obj)) { |
Markus Armbruster | 481b002 | 2015-04-29 15:35:05 -0600 | [diff] [blame] | 136 | case QTYPE_QNULL: |
| 137 | qstring_append(str, "null"); |
| 138 | break; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 139 | case QTYPE_QNUM: { |
| 140 | QNum *val = qobject_to_qnum(obj); |
| 141 | char *buffer = qnum_to_string(val); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 142 | qstring_append(str, buffer); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 143 | g_free(buffer); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | case QTYPE_QSTRING: { |
| 147 | QString *val = qobject_to_qstring(obj); |
| 148 | const char *ptr; |
Markus Armbruster | e2ec3f9 | 2013-04-11 18:07:21 +0200 | [diff] [blame] | 149 | int cp; |
| 150 | char buf[16]; |
| 151 | char *end; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 152 | |
| 153 | ptr = qstring_get_str(val); |
| 154 | qstring_append(str, "\""); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 155 | |
Markus Armbruster | e2ec3f9 | 2013-04-11 18:07:21 +0200 | [diff] [blame] | 156 | for (; *ptr; ptr = end) { |
| 157 | cp = mod_utf8_codepoint(ptr, 6, &end); |
| 158 | switch (cp) { |
| 159 | case '\"': |
| 160 | qstring_append(str, "\\\""); |
| 161 | break; |
| 162 | case '\\': |
| 163 | qstring_append(str, "\\\\"); |
| 164 | break; |
| 165 | case '\b': |
| 166 | qstring_append(str, "\\b"); |
| 167 | break; |
| 168 | case '\f': |
| 169 | qstring_append(str, "\\f"); |
| 170 | break; |
| 171 | case '\n': |
| 172 | qstring_append(str, "\\n"); |
| 173 | break; |
| 174 | case '\r': |
| 175 | qstring_append(str, "\\r"); |
| 176 | break; |
| 177 | case '\t': |
| 178 | qstring_append(str, "\\t"); |
| 179 | break; |
| 180 | default: |
| 181 | if (cp < 0) { |
| 182 | cp = 0xFFFD; /* replacement character */ |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 183 | } |
Markus Armbruster | e2ec3f9 | 2013-04-11 18:07:21 +0200 | [diff] [blame] | 184 | if (cp > 0xFFFF) { |
| 185 | /* beyond BMP; need a surrogate pair */ |
| 186 | snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", |
| 187 | 0xD800 + ((cp - 0x10000) >> 10), |
| 188 | 0xDC00 + ((cp - 0x10000) & 0x3FF)); |
| 189 | } else if (cp < 0x20 || cp >= 0x7F) { |
| 190 | snprintf(buf, sizeof(buf), "\\u%04X", cp); |
| 191 | } else { |
| 192 | buf[0] = cp; |
| 193 | buf[1] = 0; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 194 | } |
Markus Armbruster | e2ec3f9 | 2013-04-11 18:07:21 +0200 | [diff] [blame] | 195 | qstring_append(str, buf); |
| 196 | } |
| 197 | }; |
| 198 | |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 199 | qstring_append(str, "\""); |
| 200 | break; |
| 201 | } |
| 202 | case QTYPE_QDICT: { |
| 203 | ToJsonIterState s; |
| 204 | QDict *val = qobject_to_qdict(obj); |
| 205 | |
| 206 | s.count = 0; |
| 207 | s.str = str; |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 208 | s.indent = indent + 1; |
| 209 | s.pretty = pretty; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 210 | qstring_append(str, "{"); |
| 211 | qdict_iter(val, to_json_dict_iter, &s); |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 212 | if (pretty) { |
| 213 | int j; |
| 214 | qstring_append(str, "\n"); |
| 215 | for (j = 0 ; j < indent ; j++) |
| 216 | qstring_append(str, " "); |
| 217 | } |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 218 | qstring_append(str, "}"); |
| 219 | break; |
| 220 | } |
| 221 | case QTYPE_QLIST: { |
| 222 | ToJsonIterState s; |
| 223 | QList *val = qobject_to_qlist(obj); |
| 224 | |
| 225 | s.count = 0; |
| 226 | s.str = str; |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 227 | s.indent = indent + 1; |
| 228 | s.pretty = pretty; |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 229 | qstring_append(str, "["); |
| 230 | qlist_iter(val, (void *)to_json_list_iter, &s); |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 231 | if (pretty) { |
| 232 | int j; |
| 233 | qstring_append(str, "\n"); |
| 234 | for (j = 0 ; j < indent ; j++) |
| 235 | qstring_append(str, " "); |
| 236 | } |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 237 | qstring_append(str, "]"); |
| 238 | break; |
| 239 | } |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 240 | case QTYPE_QBOOL: { |
| 241 | QBool *val = qobject_to_qbool(obj); |
| 242 | |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 243 | if (qbool_get_bool(val)) { |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 244 | qstring_append(str, "true"); |
| 245 | } else { |
| 246 | qstring_append(str, "false"); |
| 247 | } |
| 248 | break; |
| 249 | } |
Markus Armbruster | a7c3181 | 2015-04-29 15:35:04 -0600 | [diff] [blame] | 250 | default: |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 251 | abort(); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | QString *qobject_to_json(const QObject *obj) |
| 256 | { |
| 257 | QString *str = qstring_new(); |
| 258 | |
Daniel P. Berrange | 212b600 | 2010-06-07 15:42:14 +0100 | [diff] [blame] | 259 | to_json(obj, str, 0, 0); |
| 260 | |
| 261 | return str; |
| 262 | } |
| 263 | |
| 264 | QString *qobject_to_json_pretty(const QObject *obj) |
| 265 | { |
| 266 | QString *str = qstring_new(); |
| 267 | |
| 268 | to_json(obj, str, 1, 0); |
Anthony Liguori | 1fd825f | 2009-11-11 12:01:22 -0600 | [diff] [blame] | 269 | |
| 270 | return str; |
| 271 | } |