]>
Commit | Line | Data |
---|---|---|
b4748b9b AL |
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 "json-lexer.h" | |
15 | #include "json-parser.h" | |
16 | #include "json-streamer.h" | |
17 | #include "qjson.h" | |
18 | ||
19 | typedef struct JSONParsingState | |
20 | { | |
21 | JSONMessageParser parser; | |
22 | va_list *ap; | |
23 | QObject *result; | |
24 | } JSONParsingState; | |
25 | ||
26 | static void parse_json(JSONMessageParser *parser, QList *tokens) | |
27 | { | |
28 | JSONParsingState *s = container_of(parser, JSONParsingState, parser); | |
29 | s->result = json_parser_parse(tokens, s->ap); | |
30 | } | |
31 | ||
32 | QObject *qobject_from_json(const char *string) | |
33 | { | |
34 | JSONParsingState state = {}; | |
35 | ||
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); | |
40 | ||
41 | return state.result; | |
42 | } | |
43 | ||
44 | QObject *qobject_from_jsonf(const char *string, ...) | |
45 | { | |
46 | JSONParsingState state = {}; | |
47 | va_list ap; | |
48 | ||
49 | va_start(ap, string); | |
50 | state.ap = ≈ | |
51 | ||
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); | |
56 | ||
57 | va_end(ap); | |
58 | ||
59 | return state.result; | |
60 | } |