Add a QObject JSON wrapper
[qemu/aliguori-queue.git] / qjson.c
blob45207f2433cde896ab4699b4c102888e33a2e253
1 /*
2 * QObject JSON integration
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "json-lexer.h"
15 #include "json-parser.h"
16 #include "json-streamer.h"
17 #include "qjson.h"
19 typedef struct JSONParsingState
21 JSONMessageParser parser;
22 va_list *ap;
23 QObject *result;
24 } JSONParsingState;
26 static void parse_json(JSONMessageParser *parser, QList *tokens)
28 JSONParsingState *s = container_of(parser, JSONParsingState, parser);
29 s->result = json_parser_parse(tokens, s->ap);
32 QObject *qobject_from_json(const char *string)
34 JSONParsingState state = {};
36 json_message_parser_init(&state.parser, parse_json);
37 json_message_parser_feed(&state.parser, string, strlen(string));
38 json_message_parser_flush(&state.parser);
39 json_message_parser_destroy(&state.parser);
41 return state.result;
44 QObject *qobject_from_jsonf(const char *string, ...)
46 JSONParsingState state = {};
47 va_list ap;
49 va_start(ap, string);
50 state.ap = &ap;
52 json_message_parser_init(&state.parser, parse_json);
53 json_message_parser_feed(&state.parser, string, strlen(string));
54 json_message_parser_flush(&state.parser);
55 json_message_parser_destroy(&state.parser);
57 va_end(ap);
59 return state.result;