/
yajler_nif.c
291 lines (245 loc) · 7.75 KB
/
yajler_nif.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
283
284
285
286
287
288
289
290
291
/*
Copyright (c) 2009-2011 Martin Logan, Eric Merritt, Richard Carlsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Copyright (c) 2012 Grzegorz Junka
Change to which native Erlang types JSON arrays and objects are parsed.
License and the original sources based on chapter 12 of repository:
https://github.com/erlware/Erlang-and-OTP-in-Action-Source
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <erl_nif.h>
#include <yajl/yajl_parse.h>
#define ERR_BUF_SIZE 256
/* yajl callback prototypes */
static int handle_null(void *ctx);
static int handle_boolean(void *ctx, int boolVal);
static int handle_integer(void *ctx, long long integerVal);
static int handle_double(void *ctx, double doubleVal);
static int handle_string(void *ctx, const unsigned char *stringVal, size_t stringLen);
static int handle_map_key(void *ctx, const unsigned char *stringVal, size_t stringLen);
static int handle_start_map(void *ctx);
static int handle_end_map(void *ctx);
static int handle_start_array(void *ctx);
static int handle_end_array(void *ctx);
static yajl_callbacks callbacks = {
handle_null,
handle_boolean,
handle_integer, /* note: only handles long integers, not bignums */
handle_double,
NULL, /* any number - if defined, integer/double are not used */
handle_string,
handle_start_map,
handle_map_key,
handle_end_map,
handle_start_array,
handle_end_array
};
typedef struct container_t {
int count; /* number of elements */
int arraysz; /* size of elements array */
ERL_NIF_TERM *array; /* elements array */
int key; /* true if last term was a key */
struct container_t *next;
} container_t;
typedef struct {
ErlNifEnv *env; /* NIF environment */
container_t *c; /* innermost container */
char errmsg[ERR_BUF_SIZE];
} state_t;
static void *alloc_func(void *ctx, size_t sz)
{
return enif_alloc(sz);
}
static void *realloc_func(void *ctx, void *ptr, size_t sz)
{
return enif_realloc(ptr, sz);
}
static void free_func(void *ctx, void *ptr)
{
enif_free(ptr);
}
static yajl_alloc_funcs alloc_funcs = {
alloc_func,
realloc_func,
free_func,
NULL /* must be initialized below */
};
static const char *parse_json(state_t *st, unsigned char *buf, size_t len)
{
yajl_status ys;
const char *err=NULL;
alloc_funcs.ctx = st->env; /* will be passed to alloc funcs */
yajl_handle yh = yajl_alloc(&callbacks, &alloc_funcs, st);
//yajl_config(yh, yajl_allow_comments, 1);
yajl_config(yh, yajl_dont_validate_strings, 1);
ys = yajl_parse(yh, buf, len);
if (ys == yajl_status_ok) {
ys = yajl_complete_parse(yh);
}
if (ys != yajl_status_ok) {
unsigned char *msg = yajl_get_error(yh, 0, NULL, 0);
strncpy(st->errmsg, (char *)msg, ERR_BUF_SIZE-1);
yajl_free_error(yh, msg);
st->errmsg[sizeof(st->errmsg)] = 0;
err = st->errmsg;
}
yajl_free(yh);
return err;
}
static ERL_NIF_TERM decode_1(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
/* initialize the state, with a dummy top level container */
state_t st;
st.env = env; /* keep NIF environment in state */
ERL_NIF_TERM term;
container_t c = { 0, 1, &term, 0, NULL };
st.c = &c;
ErlNifBinary bin;
if (argc != 1 || !enif_is_binary(env, argv[0]))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[0], &bin))
return enif_make_badarg(env);
const char *err;
if ((err = parse_json(&st, bin.data, bin.size)) != NULL) {
return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, err, ERL_NIF_LATIN1));
}
return enif_make_tuple2(env, enif_make_atom(env, "ok"), term);
}
static ErlNifFunc json_parser_NIFs[] = {
{"decode", 1, &decode_1}
};
ERL_NIF_INIT(yajler, json_parser_NIFs, NULL, NULL, NULL, NULL);
/*
* JSON Erlang json() representation
* ---- ----------------------------
* true 'true'
* false 'false'
* null 'undefined'
* number (integers and floats) number()
* string: "..." binary()
* array/list: [ value, ... ] {json()}
* map: { label: value, ... } [{binary(), json()}]
*/
static void add_element(state_t *st, ERL_NIF_TERM t)
{
container_t *c = st->c;
if (c != NULL) {
if (c->count >= c->arraysz) {
c->arraysz *= 2;
c->array = enif_realloc(c->array, c->arraysz*sizeof(ERL_NIF_TERM));
}
if (c->key) {
/* the previous stored element was a key, so replace the entry
with a key/value pair, and don't count it twice */
c->array[c->count-1] = enif_make_tuple2(st->env, c->array[c->count-1], t);
c->key = 0;
} else {
c->array[c->count] = t;
++(c->count);
}
}
}
static int handle_null(void *ctx)
{
state_t *st = (state_t *)ctx;
add_element(st, enif_make_atom(st->env, "undefined"));
return 1;
}
static int handle_boolean(void *ctx, int boolVal)
{
state_t *st = (state_t *)ctx;
if (boolVal) {
add_element(st, enif_make_atom(st->env, "true"));
} else {
add_element(st, enif_make_atom(st->env, "false"));
}
return 1;
}
static int handle_integer(void *ctx, long long integerVal)
{
state_t *st = (state_t *)ctx;
add_element(st, enif_make_long(st->env, (long)integerVal));
return 1;
}
static int handle_double(void *ctx, double doubleVal)
{
state_t *st = (state_t *)ctx;
add_element(st, enif_make_double(st->env, doubleVal));
return 1;
}
static int handle_string(void *ctx, const unsigned char *stringVal, size_t stringLen)
{
state_t *st = (state_t *)ctx;
ErlNifBinary bin;
enif_alloc_binary(stringLen, &bin);
strncpy((char *)bin.data, (char *)stringVal, stringLen);
add_element(st, enif_make_binary(st->env, &bin));
return 1;
}
static int handle_map_key(void *ctx, const unsigned char *stringVal, size_t stringLen)
{
state_t *st = (state_t *)ctx;
ErlNifBinary bin;
enif_alloc_binary(stringLen, &bin);
strncpy((char *)bin.data, (char *)stringVal, stringLen);
add_element(st, enif_make_binary(st->env, &bin));
st->c->key = 1; /* note that the next term will be the value */
return 1;
}
static int handle_start(void *ctx)
{
state_t *st = (state_t *)ctx;
container_t *c = enif_alloc(sizeof(container_t));
/* link and initialize container struct */
c->next = st->c;
st->c = c;
c->key = 0;
c->count = 0;
c->arraysz = 16; /* initial term buffer size */
c->array = enif_alloc(c->arraysz*sizeof(ERL_NIF_TERM));
return 1;
}
static int handle_start_map(void *ctx)
{
return handle_start(ctx);
}
static int handle_start_array(void *ctx)
{
return handle_start(ctx);
}
static int handle_end(void *ctx)
{
state_t *st = (state_t *)ctx;
container_t *c = st->c;
/* unlink container struct from state */
st->c = c->next;
/* create and add container term */
add_element(st, enif_make_list_from_array(st->env, c->array, c->count));
/* decallocate used container struct */
enif_free(c);
return 1;
}
static int handle_end_map(void *ctx)
{
return handle_end(ctx);
}
static int handle_end_array(void *ctx)
{
return handle_end(ctx);
}