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 "qemu/osdep.h" 15 #include "qapi/error.h" 16 #include "qapi/qmp/json-parser.h" 17 #include "qapi/qmp/qjson.h" 18 #include "qapi/qmp/qbool.h" 19 #include "qapi/qmp/qdict.h" 20 #include "qapi/qmp/qlist.h" 21 #include "qapi/qmp/qnum.h" 22 #include "qapi/qmp/qstring.h" 23 #include "qemu/unicode.h" 24 25 typedef struct JSONParsingState 26 { 27 JSONMessageParser parser; 28 QObject *result; 29 Error *err; 30 } JSONParsingState; 31 32 static void consume_json(void *opaque, QObject *json, Error *err) 33 { 34 JSONParsingState *s = opaque; 35 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 } 49 s->result = json; 50 s->err = err; 51 } 52 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) 65 { 66 JSONParsingState state = {}; 67 68 json_message_parser_init(&state.parser, consume_json, &state, ap); 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 73 if (!state.result && !state.err) { 74 error_setg(&state.err, "Expecting a JSON value"); 75 } 76 77 error_propagate(errp, state.err); 78 return state.result; 79 } 80 81 QObject *qobject_from_json(const char *string, Error **errp) 82 { 83 return qobject_from_jsonv(string, NULL, errp); 84 } 85 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 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, ...) 111 { 112 QObject *obj; 113 va_list ap; 114 115 va_start(ap, string); 116 obj = qobject_from_vjsonf_nofail(string, ap); 117 va_end(ap); 118 119 return obj; 120 } 121 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 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 { 143 QDict *qdict; 144 va_list ap; 145 146 va_start(ap, string); 147 qdict = qdict_from_vjsonf_nofail(string, ap); 148 va_end(ap); 149 return qdict; 150 } 151 152 static void json_pretty_newline(GString *accu, bool pretty, int indent) 153 { 154 if (pretty) { 155 g_string_append_printf(accu, "\n%*s", indent * 4, ""); 156 } 157 } 158 159 static void quoted_str(const char *str, GString *accu) 160 { 161 const char *ptr; 162 int cp; 163 char *end; 164 165 g_string_append_c(accu, '"'); 166 167 for (ptr = str; *ptr; ptr = end) { 168 cp = mod_utf8_codepoint(ptr, 6, &end); 169 switch (cp) { 170 case '\"': 171 g_string_append(accu, "\\\""); 172 break; 173 case '\\': 174 g_string_append(accu, "\\\\"); 175 break; 176 case '\b': 177 g_string_append(accu, "\\b"); 178 break; 179 case '\f': 180 g_string_append(accu, "\\f"); 181 break; 182 case '\n': 183 g_string_append(accu, "\\n"); 184 break; 185 case '\r': 186 g_string_append(accu, "\\r"); 187 break; 188 case '\t': 189 g_string_append(accu, "\\t"); 190 break; 191 default: 192 if (cp < 0) { 193 cp = 0xFFFD; /* replacement character */ 194 } 195 if (cp > 0xFFFF) { 196 /* beyond BMP; need a surrogate pair */ 197 g_string_append_printf(accu, "\\u%04X\\u%04X", 198 0xD800 + ((cp - 0x10000) >> 10), 199 0xDC00 + ((cp - 0x10000) & 0x3FF)); 200 } else if (cp < 0x20 || cp >= 0x7F) { 201 g_string_append_printf(accu, "\\u%04X", cp); 202 } else { 203 g_string_append_c(accu, cp); 204 } 205 } 206 }; 207 208 g_string_append_c(accu, '"'); 209 } 210 211 static void to_json(const QObject *obj, GString *accu, bool pretty, int indent) 212 { 213 switch (qobject_type(obj)) { 214 case QTYPE_QNULL: 215 g_string_append(accu, "null"); 216 break; 217 case QTYPE_QNUM: { 218 QNum *val = qobject_to(QNum, obj); 219 char *buffer = qnum_to_string(val); 220 g_string_append(accu, buffer); 221 g_free(buffer); 222 break; 223 } 224 case QTYPE_QSTRING: { 225 quoted_str(qstring_get_str(qobject_to(QString, obj)), accu); 226 break; 227 } 228 case QTYPE_QDICT: { 229 QDict *val = qobject_to(QDict, obj); 230 const char *comma = pretty ? "," : ", "; 231 const char *sep = ""; 232 const QDictEntry *entry; 233 234 g_string_append_c(accu, '{'); 235 236 for (entry = qdict_first(val); 237 entry; 238 entry = qdict_next(val, entry)) { 239 g_string_append(accu, sep); 240 json_pretty_newline(accu, pretty, indent + 1); 241 quoted_str(qdict_entry_key(entry), accu); 242 g_string_append(accu, ": "); 243 to_json(qdict_entry_value(entry), accu, pretty, indent + 1); 244 sep = comma; 245 } 246 247 json_pretty_newline(accu, pretty, indent); 248 g_string_append_c(accu, '}'); 249 break; 250 } 251 case QTYPE_QLIST: { 252 QList *val = qobject_to(QList, obj); 253 const char *comma = pretty ? "," : ", "; 254 const char *sep = ""; 255 QListEntry *entry; 256 257 g_string_append_c(accu, '['); 258 259 QLIST_FOREACH_ENTRY(val, entry) { 260 g_string_append(accu, sep); 261 json_pretty_newline(accu, pretty, indent + 1); 262 to_json(qlist_entry_obj(entry), accu, pretty, indent + 1); 263 sep = comma; 264 } 265 266 json_pretty_newline(accu, pretty, indent); 267 g_string_append_c(accu, ']'); 268 break; 269 } 270 case QTYPE_QBOOL: { 271 QBool *val = qobject_to(QBool, obj); 272 273 if (qbool_get_bool(val)) { 274 g_string_append(accu, "true"); 275 } else { 276 g_string_append(accu, "false"); 277 } 278 break; 279 } 280 default: 281 abort(); 282 } 283 } 284 285 GString *qobject_to_json_pretty(const QObject *obj, bool pretty) 286 { 287 GString *accu = g_string_new(NULL); 288 289 to_json(obj, accu, pretty, 0); 290 return accu; 291 } 292 293 GString *qobject_to_json(const QObject *obj) 294 { 295 return qobject_to_json_pretty(obj, false); 296 } 297