#include "qemu/osdep.h"
#include "qapi/error.h"
-#include "qapi/qmp/json-lexer.h"
-#include "qapi/qmp/json-parser.h"
#include "qapi/qmp/json-streamer.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qbool.h"
typedef struct JSONParsingState
{
JSONMessageParser parser;
- va_list *ap;
QObject *result;
Error *err;
} JSONParsingState;
-static void parse_json(JSONMessageParser *parser, GQueue *tokens)
+static void consume_json(void *opaque, QObject *json, Error *err)
{
- JSONParsingState *s = container_of(parser, JSONParsingState, parser);
+ JSONParsingState *s = opaque;
- s->result = json_parser_parse_err(tokens, s->ap, &s->err);
+ assert(!json != !err);
+ assert(!s->result || !s->err);
+
+ if (s->result) {
+ qobject_unref(s->result);
+ s->result = NULL;
+ error_setg(&s->err, "Expecting at most one JSON value");
+ }
+ if (s->err) {
+ qobject_unref(json);
+ error_free(err);
+ return;
+ }
+ s->result = json;
+ s->err = err;
}
-QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
+/*
+ * Parse @string as JSON value.
+ * If @ap is non-null, interpolate %-escapes.
+ * Takes ownership of %p arguments.
+ * On success, return the JSON value.
+ * On failure, store an error through @errp and return NULL.
+ * Ownership of %p arguments becomes indeterminate then. To avoid
+ * leaks, callers passing %p must terminate on error, e.g. by passing
+ * &error_abort.
+ */
+static QObject *qobject_from_jsonv(const char *string, va_list *ap,
+ Error **errp)
{
JSONParsingState state = {};
- state.ap = ap;
-
- json_message_parser_init(&state.parser, parse_json);
+ json_message_parser_init(&state.parser, consume_json, &state, ap);
json_message_parser_feed(&state.parser, string, strlen(string));
json_message_parser_flush(&state.parser);
json_message_parser_destroy(&state.parser);
}
/*
- * IMPORTANT: This function aborts on error, thus it must not
- * be used with untrusted arguments.
+ * Parse @string as JSON value with %-escapes interpolated.
+ * Abort on error. Do not use with untrusted @string.
+ * Return the resulting QObject. It is never null.
+ */
+QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
+{
+ va_list ap_copy;
+ QObject *obj;
+
+ /* va_copy() is needed when va_list is an array type */
+ va_copy(ap_copy, ap);
+ obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
+ va_end(ap_copy);
+
+ assert(obj);
+ return obj;
+}
+
+/*
+ * Parse @string as JSON value with %-escapes interpolated.
+ * Abort on error. Do not use with untrusted @string.
+ * Return the resulting QObject. It is never null.
*/
-QObject *qobject_from_jsonf(const char *string, ...)
+QObject *qobject_from_jsonf_nofail(const char *string, ...)
{
QObject *obj;
va_list ap;
va_start(ap, string);
- obj = qobject_from_jsonv(string, &ap, &error_abort);
+ obj = qobject_from_vjsonf_nofail(string, ap);
va_end(ap);
- assert(obj != NULL);
return obj;
}
+/*
+ * Parse @string as JSON object with %-escapes interpolated.
+ * Abort on error. Do not use with untrusted @string.
+ * Return the resulting QDict. It is never null.
+ */
+QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
+{
+ QDict *qdict;
+
+ qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
+ assert(qdict);
+ return qdict;
+}
+
+/*
+ * Parse @string as JSON object with %-escapes interpolated.
+ * Abort on error. Do not use with untrusted @string.
+ * Return the resulting QDict. It is never null.
+ */
+QDict *qdict_from_jsonf_nofail(const char *string, ...)
+{
+ QDict *qdict;
+ va_list ap;
+
+ va_start(ap, string);
+ qdict = qdict_from_vjsonf_nofail(string, ap);
+ va_end(ap);
+ return qdict;
+}
+
typedef struct ToJsonIterState
{
int indent;