2 * QObject JSON integration
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.
14 #include "json-lexer.h"
15 #include "json-parser.h"
16 #include "json-streamer.h"
24 typedef struct JSONParsingState
26 JSONMessageParser parser
;
31 static void parse_json(JSONMessageParser
*parser
, QList
*tokens
)
33 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
34 s
->result
= json_parser_parse(tokens
, s
->ap
);
37 QObject
*qobject_from_json(const char *string
)
39 JSONParsingState state
= {};
41 json_message_parser_init(&state
.parser
, parse_json
);
42 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
43 json_message_parser_flush(&state
.parser
);
44 json_message_parser_destroy(&state
.parser
);
49 QObject
*qobject_from_jsonf(const char *string
, ...)
51 JSONParsingState state
= {};
57 json_message_parser_init(&state
.parser
, parse_json
);
58 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
59 json_message_parser_flush(&state
.parser
);
60 json_message_parser_destroy(&state
.parser
);
67 typedef struct ToJsonIterState
73 static void to_json(const QObject
*obj
, QString
*str
);
75 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
77 ToJsonIterState
*s
= opaque
;
81 qstring_append(s
->str
, ", ");
84 qkey
= qstring_from_str(key
);
85 to_json(QOBJECT(qkey
), s
->str
);
88 qstring_append(s
->str
, ": ");
93 static void to_json_list_iter(QObject
*obj
, void *opaque
)
95 ToJsonIterState
*s
= opaque
;
98 qstring_append(s
->str
, ", ");
101 to_json(obj
, s
->str
);
105 static void to_json(const QObject
*obj
, QString
*str
)
107 switch (qobject_type(obj
)) {
109 QInt
*val
= qobject_to_qint(obj
);
112 snprintf(buffer
, sizeof(buffer
), "%" PRId64
, qint_get_int(val
));
113 qstring_append(str
, buffer
);
116 case QTYPE_QSTRING
: {
117 QString
*val
= qobject_to_qstring(obj
);
120 ptr
= qstring_get_str(val
);
121 qstring_append(str
, "\"");
123 if ((ptr
[0] & 0xE0) == 0xE0 &&
124 (ptr
[1] & 0x80) && (ptr
[2] & 0x80)) {
128 wchar
= (ptr
[0] & 0x0F) << 12;
129 wchar
|= (ptr
[1] & 0x3F) << 6;
130 wchar
|= (ptr
[2] & 0x3F);
133 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
134 qstring_append(str
, escape
);
135 } else if ((ptr
[0] & 0xE0) == 0xC0 && (ptr
[1] & 0x80)) {
139 wchar
= (ptr
[0] & 0x1F) << 6;
140 wchar
|= (ptr
[1] & 0x3F);
143 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
144 qstring_append(str
, escape
);
145 } else switch (ptr
[0]) {
147 qstring_append(str
, "\\\"");
150 qstring_append(str
, "\\\\");
153 qstring_append(str
, "\\b");
156 qstring_append(str
, "\\n");
159 qstring_append(str
, "\\r");
162 qstring_append(str
, "\\t");
165 char buf
[2] = { ptr
[0], 0 };
166 qstring_append(str
, buf
);
172 qstring_append(str
, "\"");
177 QDict
*val
= qobject_to_qdict(obj
);
181 qstring_append(str
, "{");
182 qdict_iter(val
, to_json_dict_iter
, &s
);
183 qstring_append(str
, "}");
188 QList
*val
= qobject_to_qlist(obj
);
192 qstring_append(str
, "[");
193 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
194 qstring_append(str
, "]");
198 QFloat
*val
= qobject_to_qfloat(obj
);
202 len
= snprintf(buffer
, sizeof(buffer
), "%f", qfloat_get_double(val
));
203 while (len
> 0 && buffer
[len
- 1] == '0') {
207 if (len
&& buffer
[len
- 1] == '.') {
213 qstring_append(str
, buffer
);
217 QBool
*val
= qobject_to_qbool(obj
);
219 if (qbool_get_int(val
)) {
220 qstring_append(str
, "true");
222 qstring_append(str
, "false");
231 QString
*qobject_to_json(const QObject
*obj
)
233 QString
*str
= qstring_new();