qobject: Propagate parse errors through qobject_from_jsonv()

The next few commits will put the errors to use where appropriate.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <1488317230-26248-9-git-send-email-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2017-02-28 22:26:54 +01:00
parent e3934b4297
commit 99dbfd1db1
4 changed files with 12 additions and 7 deletions

View File

@ -19,7 +19,8 @@
QObject *qobject_from_json(const char *string);
QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
GCC_FMT_ATTR(1, 0);
QString *qobject_to_json(const QObject *obj);
QString *qobject_to_json_pretty(const QObject *obj);

View File

@ -12,6 +12,7 @@
*/
#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"
@ -24,15 +25,17 @@ typedef struct JSONParsingState
JSONMessageParser parser;
va_list *ap;
QObject *result;
Error *err;
} JSONParsingState;
static void parse_json(JSONMessageParser *parser, GQueue *tokens)
{
JSONParsingState *s = container_of(parser, JSONParsingState, parser);
s->result = json_parser_parse(tokens, s->ap);
s->result = json_parser_parse_err(tokens, s->ap, &s->err);
}
QObject *qobject_from_jsonv(const char *string, va_list *ap)
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
{
JSONParsingState state = {};
@ -43,12 +46,13 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap)
json_message_parser_flush(&state.parser);
json_message_parser_destroy(&state.parser);
error_propagate(errp, state.err);
return state.result;
}
QObject *qobject_from_json(const char *string)
{
return qobject_from_jsonv(string, NULL);
return qobject_from_jsonv(string, NULL, NULL);
}
/*
@ -61,7 +65,7 @@ QObject *qobject_from_jsonf(const char *string, ...)
va_list ap;
va_start(ap, string);
obj = qobject_from_jsonv(string, &ap);
obj = qobject_from_jsonv(string, &ap, NULL);
va_end(ap);
assert(obj != NULL);

View File

@ -442,7 +442,7 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
* is an array type.
*/
va_copy(ap_copy, ap);
qobj = qobject_from_jsonv(fmt, &ap_copy);
qobj = qobject_from_jsonv(fmt, &ap_copy, NULL);
va_end(ap_copy);
/* No need to send anything for an empty QObject. */

View File

@ -51,7 +51,7 @@ static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
{
visitor_input_teardown(data, NULL);
data->obj = qobject_from_jsonv(json_string, ap);
data->obj = qobject_from_jsonv(json_string, ap, NULL);
g_assert(data->obj);
if (keyval) {