-
Notifications
You must be signed in to change notification settings - Fork 9
/
bjson.c
282 lines (235 loc) · 5.7 KB
/
bjson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "postgres.h"
#include "utils/jsonapi.h"
#include "bson.h"
#include "bjson.h"
static char *bson_to_json_recurse(StringInfo buf_in,
const char *data, bool is_object);
static char *
quote_string(const char *s)
{
char *result = palloc(strlen(s) * 2 + 3);
char *r = result;
*r++ = '"';
while (*s)
{
if (*s == '"')
*r++ = '\\';
*r++ = *s;
s++;
}
*r++ = '"';
*r++ = '\0';
return result;
}
char *
bson_to_json(StringInfo buf_in, const char *data)
{
StringInfoData sidata, *buf = &sidata;
if (buf_in == NULL)
initStringInfo(buf);
else
buf = buf_in;
appendStringInfoChar(buf, '{');
bson_to_json_recurse(buf, data, true);
appendStringInfoChar(buf, '}');
if (buf_in == NULL)
return pstrdup(buf->data);
else
return buf->data;
}
static char *
bson_to_json_recurse(StringInfo buf, const char *data, bool is_object)
{
bson_iterator i;
const char *key;
char oidhex[25];
char *str;
bool first = true;
bson_iterator_from_buffer(&i, data);
while (bson_iterator_next(&i))
{
bson_type t = bson_iterator_type(&i);
if (t == 0)
break;
if (!first)
appendStringInfoChar(buf, ',');
first = false;
if (is_object)
{
key = bson_iterator_key(&i);
appendStringInfo(buf, "\"%s\"", key);
appendStringInfoChar(buf, ':');
}
switch (t)
{
case BSON_DOUBLE:
appendStringInfo(buf, "%f", bson_iterator_double(&i));
break;
case BSON_STRING:
str = quote_string(bson_iterator_string(&i));
appendStringInfoString(buf, str);
break;
case BSON_OID:
bson_oid_to_string(bson_iterator_oid(&i), oidhex);
str = quote_string(oidhex);
appendStringInfoString(buf, str);
break;
case BSON_BOOL:
appendStringInfoString(buf, bson_iterator_bool(&i) ? "true" : "false");
break;
case BSON_INT:
appendStringInfo(buf, "%d", bson_iterator_int(&i));
break;
case BSON_LONG:
appendStringInfo(buf, "%lld", (uint64_t) bson_iterator_long(&i));
break;
case BSON_NULL:
appendStringInfoString(buf, "null");
break;
case BSON_OBJECT:
appendStringInfoChar(buf, '{');
bson_to_json_recurse(buf, bson_iterator_value(&i), true);
appendStringInfoChar(buf, '}');
break;
case BSON_ARRAY:
appendStringInfoChar(buf, '[');
bson_to_json_recurse(buf, bson_iterator_value(&i), false);
appendStringInfoChar(buf, ']');
break;
case BSON_DATE:
case BSON_TIMESTAMP:
case BSON_SYMBOL:
case BSON_BINDATA:
case BSON_UNDEFINED:
case BSON_REGEX:
case BSON_CODE:
case BSON_CODEWSCOPE:
ereport(ERROR,
(errmsg("unsupported bson type: %d", t)));
break;
default:
elog(ERROR, "unknown bson type: %d", t);
break;
}
}
return buf->data;
}
typedef struct json_to_bson_state {
bson *bson;
char *fname;
JsonLexContext *lex;
} json_to_bson_state;
static void
jbson_object_start(void *state)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
if (_state->lex->lex_level == 0)
return;
if (_state->fname == NULL)
elog(ERROR, "unsupported json structure");
bson_append_start_object(_state->bson, _state->fname);
}
static void
jbson_object_end(void *state)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
if (_state->lex->lex_level == 0)
return;
bson_append_finish_object(_state->bson);
}
static void
jbson_array_start(void *state)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
if (_state->lex->lex_level == 0)
return;
if (_state->fname == NULL)
elog(ERROR, "unsupported json structure");
bson_append_start_array(_state->bson, _state->fname);
}
static void
jbson_array_end(void *state)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
if (_state->lex->lex_level == 0)
return;
bson_append_finish_array(_state->bson);
}
static void
jbson_object_field_start(void *state, char *fname, bool isnull)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
if (isnull)
elog(ERROR, "fname cannot be null");
_state->fname = fname;
}
static void
jbson_array_element_start(void *state, bool isnull)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
_state->fname = "";
}
static void
jbson_scalar(void *state, char *token, JsonTokenType tokentype)
{
json_to_bson_state *_state = (json_to_bson_state *) state;
double fval;
if (_state->fname == NULL)
{
elog(ERROR, "unsupported json structure");
}
switch (tokentype)
{
case JSON_TOKEN_STRING:
bson_append_string(_state->bson, _state->fname, token);
break;
case JSON_TOKEN_NUMBER:
sscanf(token, "%lf", &fval);
bson_append_double(_state->bson, _state->fname, fval);
break;
case JSON_TOKEN_TRUE:
bson_append_bool(_state->bson, _state->fname, true);
break;
case JSON_TOKEN_FALSE:
bson_append_bool(_state->bson, _state->fname, false);
break;
case JSON_TOKEN_NULL:
bson_append_null(_state->bson, _state->fname);
break;
default:
elog(ERROR, "unexpected token type: %d", tokentype);
break;
}
_state->fname = NULL;
}
void *
json_to_bson(text *json)
{
bson b;
void *result;
JsonLexContext *lex = makeJsonLexContext(json, true);
jsonSemAction sem;
json_to_bson_state state;
memset(&state, 0, sizeof(json_to_bson_state));
state.bson = &b;
state.lex = lex;
memset(&sem, 0, sizeof(sem));
sem.semstate = (void *) &state;
sem.object_start = jbson_object_start;
sem.object_end = jbson_object_end;
sem.array_start = jbson_array_start;
sem.array_end = jbson_array_end;
sem.array_element_start = jbson_array_element_start;
sem.scalar = jbson_scalar;
sem.object_field_start = jbson_object_field_start;
bson_init(&b);
pg_parse_json(lex, &sem);
pfree(lex->strval->data);
pfree(lex->strval);
pfree(lex);
bson_finish(&b);
result = palloc(bson_size(&b));
memcpy(result, b.data, bson_size(&b));
bson_destroy(&b);
return result;
}