root / qjson.c @ 9ea37780
History | View | Annotate | Download (5.7 kB)
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 |
#include "qint.h" |
19 |
#include "qlist.h" |
20 |
#include "qbool.h" |
21 |
#include "qfloat.h" |
22 |
#include "qdict.h" |
23 |
|
24 |
typedef struct JSONParsingState |
25 |
{ |
26 |
JSONMessageParser parser; |
27 |
va_list *ap; |
28 |
QObject *result; |
29 |
} JSONParsingState; |
30 |
|
31 |
static void parse_json(JSONMessageParser *parser, QList *tokens) |
32 |
{ |
33 |
JSONParsingState *s = container_of(parser, JSONParsingState, parser); |
34 |
s->result = json_parser_parse(tokens, s->ap); |
35 |
} |
36 |
|
37 |
QObject *qobject_from_jsonv(const char *string, va_list *ap) |
38 |
{ |
39 |
JSONParsingState state = {}; |
40 |
|
41 |
state.ap = ap; |
42 |
|
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); |
47 |
|
48 |
return state.result;
|
49 |
} |
50 |
|
51 |
QObject *qobject_from_json(const char *string) |
52 |
{ |
53 |
return qobject_from_jsonv(string, NULL); |
54 |
} |
55 |
|
56 |
QObject *qobject_from_jsonf(const char *string, ...) |
57 |
{ |
58 |
QObject *obj; |
59 |
va_list ap; |
60 |
|
61 |
va_start(ap, string); |
62 |
obj = qobject_from_jsonv(string, &ap); |
63 |
va_end(ap); |
64 |
|
65 |
return obj;
|
66 |
} |
67 |
|
68 |
typedef struct ToJsonIterState |
69 |
{ |
70 |
int count;
|
71 |
QString *str; |
72 |
} ToJsonIterState; |
73 |
|
74 |
static void to_json(const QObject *obj, QString *str); |
75 |
|
76 |
static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) |
77 |
{ |
78 |
ToJsonIterState *s = opaque; |
79 |
QString *qkey; |
80 |
|
81 |
if (s->count) {
|
82 |
qstring_append(s->str, ", ");
|
83 |
} |
84 |
|
85 |
qkey = qstring_from_str(key); |
86 |
to_json(QOBJECT(qkey), s->str); |
87 |
QDECREF(qkey); |
88 |
|
89 |
qstring_append(s->str, ": ");
|
90 |
to_json(obj, s->str); |
91 |
s->count++; |
92 |
} |
93 |
|
94 |
static void to_json_list_iter(QObject *obj, void *opaque) |
95 |
{ |
96 |
ToJsonIterState *s = opaque; |
97 |
|
98 |
if (s->count) {
|
99 |
qstring_append(s->str, ", ");
|
100 |
} |
101 |
|
102 |
to_json(obj, s->str); |
103 |
s->count++; |
104 |
} |
105 |
|
106 |
static void to_json(const QObject *obj, QString *str) |
107 |
{ |
108 |
switch (qobject_type(obj)) {
|
109 |
case QTYPE_QINT: {
|
110 |
QInt *val = qobject_to_qint(obj); |
111 |
char buffer[1024]; |
112 |
|
113 |
snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val)); |
114 |
qstring_append(str, buffer); |
115 |
break;
|
116 |
} |
117 |
case QTYPE_QSTRING: {
|
118 |
QString *val = qobject_to_qstring(obj); |
119 |
const char *ptr; |
120 |
|
121 |
ptr = qstring_get_str(val); |
122 |
qstring_append(str, "\"");
|
123 |
while (*ptr) {
|
124 |
if ((ptr[0] & 0xE0) == 0xE0 && |
125 |
(ptr[1] & 0x80) && (ptr[2] & 0x80)) { |
126 |
uint16_t wchar; |
127 |
char escape[7]; |
128 |
|
129 |
wchar = (ptr[0] & 0x0F) << 12; |
130 |
wchar |= (ptr[1] & 0x3F) << 6; |
131 |
wchar |= (ptr[2] & 0x3F); |
132 |
ptr += 2;
|
133 |
|
134 |
snprintf(escape, sizeof(escape), "\\u%04X", wchar); |
135 |
qstring_append(str, escape); |
136 |
} else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) { |
137 |
uint16_t wchar; |
138 |
char escape[7]; |
139 |
|
140 |
wchar = (ptr[0] & 0x1F) << 6; |
141 |
wchar |= (ptr[1] & 0x3F); |
142 |
ptr++; |
143 |
|
144 |
snprintf(escape, sizeof(escape), "\\u%04X", wchar); |
145 |
qstring_append(str, escape); |
146 |
} else switch (ptr[0]) { |
147 |
case '\"': |
148 |
qstring_append(str, "\\\"");
|
149 |
break;
|
150 |
case '\\': |
151 |
qstring_append(str, "\\\\");
|
152 |
break;
|
153 |
case '\b': |
154 |
qstring_append(str, "\\b");
|
155 |
break;
|
156 |
case '\n': |
157 |
qstring_append(str, "\\n");
|
158 |
break;
|
159 |
case '\r': |
160 |
qstring_append(str, "\\r");
|
161 |
break;
|
162 |
case '\t': |
163 |
qstring_append(str, "\\t");
|
164 |
break;
|
165 |
default: {
|
166 |
char buf[2] = { ptr[0], 0 }; |
167 |
qstring_append(str, buf); |
168 |
break;
|
169 |
} |
170 |
} |
171 |
ptr++; |
172 |
} |
173 |
qstring_append(str, "\"");
|
174 |
break;
|
175 |
} |
176 |
case QTYPE_QDICT: {
|
177 |
ToJsonIterState s; |
178 |
QDict *val = qobject_to_qdict(obj); |
179 |
|
180 |
s.count = 0;
|
181 |
s.str = str; |
182 |
qstring_append(str, "{");
|
183 |
qdict_iter(val, to_json_dict_iter, &s); |
184 |
qstring_append(str, "}");
|
185 |
break;
|
186 |
} |
187 |
case QTYPE_QLIST: {
|
188 |
ToJsonIterState s; |
189 |
QList *val = qobject_to_qlist(obj); |
190 |
|
191 |
s.count = 0;
|
192 |
s.str = str; |
193 |
qstring_append(str, "[");
|
194 |
qlist_iter(val, (void *)to_json_list_iter, &s);
|
195 |
qstring_append(str, "]");
|
196 |
break;
|
197 |
} |
198 |
case QTYPE_QFLOAT: {
|
199 |
QFloat *val = qobject_to_qfloat(obj); |
200 |
char buffer[1024]; |
201 |
int len;
|
202 |
|
203 |
len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val)); |
204 |
while (len > 0 && buffer[len - 1] == '0') { |
205 |
len--; |
206 |
} |
207 |
|
208 |
if (len && buffer[len - 1] == '.') { |
209 |
buffer[len - 1] = 0; |
210 |
} else {
|
211 |
buffer[len] = 0;
|
212 |
} |
213 |
|
214 |
qstring_append(str, buffer); |
215 |
break;
|
216 |
} |
217 |
case QTYPE_QBOOL: {
|
218 |
QBool *val = qobject_to_qbool(obj); |
219 |
|
220 |
if (qbool_get_int(val)) {
|
221 |
qstring_append(str, "true");
|
222 |
} else {
|
223 |
qstring_append(str, "false");
|
224 |
} |
225 |
break;
|
226 |
} |
227 |
case QTYPE_QERROR:
|
228 |
/* XXX: should QError be emitted? */
|
229 |
case QTYPE_NONE:
|
230 |
break;
|
231 |
} |
232 |
} |
233 |
|
234 |
QString *qobject_to_json(const QObject *obj)
|
235 |
{ |
236 |
QString *str = qstring_new(); |
237 |
|
238 |
to_json(obj, str); |
239 |
|
240 |
return str;
|
241 |
} |