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_jsonv(const char *string
, va_list *ap
)
39 JSONParsingState state
= {};
43 json_message_parser_init(&state
.parser
, parse_json
);
44 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
45 json_message_parser_flush(&state
.parser
);
46 json_message_parser_destroy(&state
.parser
);
51 QObject
*qobject_from_json(const char *string
)
53 return qobject_from_jsonv(string
, NULL
);
57 * IMPORTANT: This function aborts on error, thus it must not
58 * be used with untrusted arguments.
60 QObject
*qobject_from_jsonf(const char *string
, ...)
66 obj
= qobject_from_jsonv(string
, &ap
);
73 typedef struct ToJsonIterState
79 static void to_json(const QObject
*obj
, QString
*str
);
81 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
83 ToJsonIterState
*s
= opaque
;
87 qstring_append(s
->str
, ", ");
90 qkey
= qstring_from_str(key
);
91 to_json(QOBJECT(qkey
), s
->str
);
94 qstring_append(s
->str
, ": ");
99 static void to_json_list_iter(QObject
*obj
, void *opaque
)
101 ToJsonIterState
*s
= opaque
;
104 qstring_append(s
->str
, ", ");
107 to_json(obj
, s
->str
);
111 static void to_json(const QObject
*obj
, QString
*str
)
113 switch (qobject_type(obj
)) {
115 QInt
*val
= qobject_to_qint(obj
);
118 snprintf(buffer
, sizeof(buffer
), "%" PRId64
, qint_get_int(val
));
119 qstring_append(str
, buffer
);
122 case QTYPE_QSTRING
: {
123 QString
*val
= qobject_to_qstring(obj
);
126 ptr
= qstring_get_str(val
);
127 qstring_append(str
, "\"");
129 if ((ptr
[0] & 0xE0) == 0xE0 &&
130 (ptr
[1] & 0x80) && (ptr
[2] & 0x80)) {
134 wchar
= (ptr
[0] & 0x0F) << 12;
135 wchar
|= (ptr
[1] & 0x3F) << 6;
136 wchar
|= (ptr
[2] & 0x3F);
139 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
140 qstring_append(str
, escape
);
141 } else if ((ptr
[0] & 0xE0) == 0xC0 && (ptr
[1] & 0x80)) {
145 wchar
= (ptr
[0] & 0x1F) << 6;
146 wchar
|= (ptr
[1] & 0x3F);
149 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
150 qstring_append(str
, escape
);
151 } else switch (ptr
[0]) {
153 qstring_append(str
, "\\\"");
156 qstring_append(str
, "\\\\");
159 qstring_append(str
, "\\b");
162 qstring_append(str
, "\\n");
165 qstring_append(str
, "\\r");
168 qstring_append(str
, "\\t");
171 if (ptr
[0] <= 0x1F) {
173 snprintf(escape
, sizeof(escape
), "\\u%04X", ptr
[0]);
174 qstring_append(str
, escape
);
176 char buf
[2] = { ptr
[0], 0 };
177 qstring_append(str
, buf
);
184 qstring_append(str
, "\"");
189 QDict
*val
= qobject_to_qdict(obj
);
193 qstring_append(str
, "{");
194 qdict_iter(val
, to_json_dict_iter
, &s
);
195 qstring_append(str
, "}");
200 QList
*val
= qobject_to_qlist(obj
);
204 qstring_append(str
, "[");
205 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
206 qstring_append(str
, "]");
210 QFloat
*val
= qobject_to_qfloat(obj
);
214 len
= snprintf(buffer
, sizeof(buffer
), "%f", qfloat_get_double(val
));
215 while (len
> 0 && buffer
[len
- 1] == '0') {
219 if (len
&& buffer
[len
- 1] == '.') {
225 qstring_append(str
, buffer
);
229 QBool
*val
= qobject_to_qbool(obj
);
231 if (qbool_get_int(val
)) {
232 qstring_append(str
, "true");
234 qstring_append(str
, "false");
239 /* XXX: should QError be emitted? */
245 QString
*qobject_to_json(const QObject
*obj
)
247 QString
*str
= qstring_new();