2 * JSON streaming support
4 * Copyright IBM, Corp. 2009
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.
17 #include "qemu-common.h"
18 #include "json-lexer.h"
19 #include "json-streamer.h"
21 static void json_message_process_token(JSONLexer
*lexer
, QString
*token
, JSONTokenType type
, int x
, int y
)
23 JSONMessageParser
*parser
= container_of(lexer
, JSONMessageParser
, lexer
);
26 if (type
== JSON_OPERATOR
) {
27 switch (qstring_get_str(token
)[0]) {
29 parser
->brace_count
++;
32 parser
->brace_count
--;
35 parser
->bracket_count
++;
38 parser
->bracket_count
--;
46 qdict_put(dict
, "type", qint_from_int(type
));
48 qdict_put(dict
, "token", token
);
49 qdict_put(dict
, "x", qint_from_int(x
));
50 qdict_put(dict
, "y", qint_from_int(y
));
52 qlist_append(parser
->tokens
, dict
);
54 if (parser
->brace_count
< 0 ||
55 parser
->bracket_count
< 0 ||
56 (parser
->brace_count
== 0 &&
57 parser
->bracket_count
== 0)) {
58 parser
->brace_count
= 0;
59 parser
->bracket_count
= 0;
60 parser
->emit(parser
, parser
->tokens
);
61 QDECREF(parser
->tokens
);
62 parser
->tokens
= qlist_new();
66 void json_message_parser_init(JSONMessageParser
*parser
,
67 void (*func
)(JSONMessageParser
*, QList
*))
70 parser
->brace_count
= 0;
71 parser
->bracket_count
= 0;
72 parser
->tokens
= qlist_new();
74 json_lexer_init(&parser
->lexer
, json_message_process_token
);
77 int json_message_parser_feed(JSONMessageParser
*parser
,
78 const char *buffer
, size_t size
)
80 return json_lexer_feed(&parser
->lexer
, buffer
, size
);
83 int json_message_parser_flush(JSONMessageParser
*parser
)
85 return json_lexer_flush(&parser
->lexer
);
88 void json_message_parser_destroy(JSONMessageParser
*parser
)
90 json_lexer_destroy(&parser
->lexer
);
91 QDECREF(parser
->tokens
);