root / json-streamer.c @ c05c7a73
History | View | Annotate | Download (2.2 kB)
1 | d7ff3acb | Anthony Liguori | /*
|
---|---|---|---|
2 | d7ff3acb | Anthony Liguori | * JSON streaming support
|
3 | d7ff3acb | Anthony Liguori | *
|
4 | d7ff3acb | Anthony Liguori | * Copyright IBM, Corp. 2009
|
5 | d7ff3acb | Anthony Liguori | *
|
6 | d7ff3acb | Anthony Liguori | * Authors:
|
7 | d7ff3acb | Anthony Liguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | d7ff3acb | Anthony Liguori | *
|
9 | d7ff3acb | Anthony Liguori | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
10 | d7ff3acb | Anthony Liguori | * See the COPYING.LIB file in the top-level directory.
|
11 | d7ff3acb | Anthony Liguori | *
|
12 | d7ff3acb | Anthony Liguori | */
|
13 | d7ff3acb | Anthony Liguori | |
14 | d7ff3acb | Anthony Liguori | #include "qlist.h" |
15 | d7ff3acb | Anthony Liguori | #include "qint.h" |
16 | d7ff3acb | Anthony Liguori | #include "qdict.h" |
17 | d7ff3acb | Anthony Liguori | #include "qemu-common.h" |
18 | d7ff3acb | Anthony Liguori | #include "json-lexer.h" |
19 | d7ff3acb | Anthony Liguori | #include "json-streamer.h" |
20 | d7ff3acb | Anthony Liguori | |
21 | d7ff3acb | Anthony Liguori | static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTokenType type, int x, int y) |
22 | d7ff3acb | Anthony Liguori | { |
23 | d7ff3acb | Anthony Liguori | JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer); |
24 | d7ff3acb | Anthony Liguori | QDict *dict; |
25 | d7ff3acb | Anthony Liguori | |
26 | d7ff3acb | Anthony Liguori | if (type == JSON_OPERATOR) {
|
27 | d7ff3acb | Anthony Liguori | switch (qstring_get_str(token)[0]) { |
28 | d7ff3acb | Anthony Liguori | case '{': |
29 | d7ff3acb | Anthony Liguori | parser->brace_count++; |
30 | d7ff3acb | Anthony Liguori | break;
|
31 | d7ff3acb | Anthony Liguori | case '}': |
32 | d7ff3acb | Anthony Liguori | parser->brace_count--; |
33 | d7ff3acb | Anthony Liguori | break;
|
34 | d7ff3acb | Anthony Liguori | case '[': |
35 | d7ff3acb | Anthony Liguori | parser->bracket_count++; |
36 | d7ff3acb | Anthony Liguori | break;
|
37 | d7ff3acb | Anthony Liguori | case ']': |
38 | d7ff3acb | Anthony Liguori | parser->bracket_count--; |
39 | d7ff3acb | Anthony Liguori | break;
|
40 | d7ff3acb | Anthony Liguori | default:
|
41 | d7ff3acb | Anthony Liguori | break;
|
42 | d7ff3acb | Anthony Liguori | } |
43 | d7ff3acb | Anthony Liguori | } |
44 | d7ff3acb | Anthony Liguori | |
45 | d7ff3acb | Anthony Liguori | dict = qdict_new(); |
46 | d7ff3acb | Anthony Liguori | qdict_put_obj(dict, "type", QOBJECT(qint_from_int(type)));
|
47 | d7ff3acb | Anthony Liguori | QINCREF(token); |
48 | d7ff3acb | Anthony Liguori | qdict_put_obj(dict, "token", QOBJECT(token));
|
49 | d7ff3acb | Anthony Liguori | qdict_put_obj(dict, "x", QOBJECT(qint_from_int(x)));
|
50 | d7ff3acb | Anthony Liguori | qdict_put_obj(dict, "y", QOBJECT(qint_from_int(y)));
|
51 | d7ff3acb | Anthony Liguori | |
52 | d7ff3acb | Anthony Liguori | qlist_append(parser->tokens, dict); |
53 | d7ff3acb | Anthony Liguori | |
54 | d7ff3acb | Anthony Liguori | if (parser->brace_count == 0 && |
55 | d7ff3acb | Anthony Liguori | parser->bracket_count == 0) {
|
56 | d7ff3acb | Anthony Liguori | parser->emit(parser, parser->tokens); |
57 | d7ff3acb | Anthony Liguori | QDECREF(parser->tokens); |
58 | d7ff3acb | Anthony Liguori | parser->tokens = qlist_new(); |
59 | d7ff3acb | Anthony Liguori | } |
60 | d7ff3acb | Anthony Liguori | } |
61 | d7ff3acb | Anthony Liguori | |
62 | d7ff3acb | Anthony Liguori | void json_message_parser_init(JSONMessageParser *parser,
|
63 | d7ff3acb | Anthony Liguori | void (*func)(JSONMessageParser *, QList *))
|
64 | d7ff3acb | Anthony Liguori | { |
65 | d7ff3acb | Anthony Liguori | parser->emit = func; |
66 | d7ff3acb | Anthony Liguori | parser->brace_count = 0;
|
67 | d7ff3acb | Anthony Liguori | parser->bracket_count = 0;
|
68 | d7ff3acb | Anthony Liguori | parser->tokens = qlist_new(); |
69 | d7ff3acb | Anthony Liguori | |
70 | d7ff3acb | Anthony Liguori | json_lexer_init(&parser->lexer, json_message_process_token); |
71 | d7ff3acb | Anthony Liguori | } |
72 | d7ff3acb | Anthony Liguori | |
73 | d7ff3acb | Anthony Liguori | int json_message_parser_feed(JSONMessageParser *parser,
|
74 | d7ff3acb | Anthony Liguori | const char *buffer, size_t size) |
75 | d7ff3acb | Anthony Liguori | { |
76 | d7ff3acb | Anthony Liguori | return json_lexer_feed(&parser->lexer, buffer, size);
|
77 | d7ff3acb | Anthony Liguori | } |
78 | d7ff3acb | Anthony Liguori | |
79 | d7ff3acb | Anthony Liguori | int json_message_parser_flush(JSONMessageParser *parser)
|
80 | d7ff3acb | Anthony Liguori | { |
81 | d7ff3acb | Anthony Liguori | return json_lexer_flush(&parser->lexer);
|
82 | d7ff3acb | Anthony Liguori | } |
83 | d7ff3acb | Anthony Liguori | |
84 | d7ff3acb | Anthony Liguori | void json_message_parser_destroy(JSONMessageParser *parser)
|
85 | d7ff3acb | Anthony Liguori | { |
86 | d7ff3acb | Anthony Liguori | json_lexer_destroy(&parser->lexer); |
87 | d7ff3acb | Anthony Liguori | QDECREF(parser->tokens); |
88 | d7ff3acb | Anthony Liguori | } |