brianmario-yajl-ruby 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +10 -0
- data/README.rdoc +25 -23
- data/Rakefile +11 -1
- data/VERSION.yml +2 -2
- data/benchmark/encode.rb +1 -1
- data/benchmark/encode_json_and_marshal.rb +1 -1
- data/benchmark/encode_json_and_yaml.rb +1 -1
- data/benchmark/parse.rb +1 -1
- data/benchmark/parse_json_and_marshal.rb +1 -1
- data/benchmark/parse_json_and_yaml.rb +1 -1
- data/benchmark/subjects/unicode.json +1 -3315
- data/ext/api/yajl_common.h +85 -0
- data/ext/api/yajl_gen.h +123 -0
- data/ext/api/yajl_parse.h +179 -0
- data/ext/extconf.rb +2 -8
- data/ext/yajl.c +128 -260
- data/ext/yajl_alloc.c +65 -0
- data/ext/yajl_alloc.h +50 -0
- data/ext/yajl_buf.c +119 -0
- data/ext/yajl_buf.h +73 -0
- data/ext/yajl_bytestack.h +85 -0
- data/ext/yajl_encode.c +179 -0
- data/ext/yajl_encode.h +44 -0
- data/ext/yajl_ext.c +283 -0
- data/ext/{yajl.h → yajl_ext.h} +2 -2
- data/ext/yajl_gen.c +295 -0
- data/ext/yajl_lex.c +737 -0
- data/ext/yajl_lex.h +133 -0
- data/ext/yajl_parser.c +445 -0
- data/ext/yajl_parser.h +79 -0
- data/lib/yajl/bzip2.rb +1 -1
- data/lib/yajl/deflate.rb +1 -1
- data/lib/yajl/gzip.rb +1 -1
- data/lib/yajl/http_stream.rb +1 -1
- data/lib/yajl.rb +1 -1
- data/spec/encoding/encoding_spec.rb +23 -0
- data/spec/http/{http.bzip2.dump → fixtures/http.bzip2.dump} +0 -0
- data/spec/http/{http.deflate.dump → fixtures/http.deflate.dump} +0 -0
- data/spec/http/{http.gzip.dump → fixtures/http.gzip.dump} +0 -0
- data/spec/http/{http.raw.dump → fixtures/http.raw.dump} +0 -0
- data/spec/http/http_spec.rb +98 -0
- data/spec/{active_support_spec.rb → parsing/active_support_spec.rb} +1 -1
- data/spec/{fixtures → parsing/fixtures}/fail.15.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail.16.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail.17.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail.26.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail11.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail12.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail13.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail14.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail19.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail20.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail21.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail22.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail23.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail24.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail25.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail27.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail28.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail3.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail4.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail5.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail6.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/fail9.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.array.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.codepoints_from_unicode_org.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.contacts.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.db100.xml.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.db1000.xml.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.dc_simple_with_comments.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.deep_arrays.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.difficult_json_c_test_case.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.difficult_json_c_test_case_with_comments.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.doubles.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.empty_array.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.empty_string.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.escaped_bulgarian.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.escaped_foobar.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.item.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample1.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample2.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample3.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample4-nows.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample4.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.json-org-sample5.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.map-spain.xml.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.ns-invoice100.xml.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.ns-soap.xml.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.numbers-fp-4k.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.numbers-fp-64k.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.numbers-int-4k.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.numbers-int-64k.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.twitter-search.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.twitter-search2.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.unicode.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass.yelp.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass1.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass2.json +0 -0
- data/spec/{fixtures → parsing/fixtures}/pass3.json +0 -0
- data/spec/{fixtures_spec.rb → parsing/fixtures_spec.rb} +1 -1
- data/spec/{one_off_spec.rb → parsing/one_off_spec.rb} +1 -1
- data/yajl-ruby.gemspec +91 -72
- metadata +91 -71
- data/benchmark/subjects/yelp.json +0 -1
data/ext/yajl.c
CHANGED
@@ -1,284 +1,152 @@
|
|
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
|
-
int objLen;
|
50
|
-
int idx = 0;
|
51
|
-
const unsigned char * buffer;
|
52
|
-
unsigned int len;
|
53
|
-
yajl_gen_get_buf(hand, &buffer, &len);
|
54
|
-
outBuff = rb_str_new((const char *)buffer, len);
|
55
|
-
rb_io_write(io, outBuff);
|
56
|
-
yajl_gen_clear(hand);
|
57
|
-
|
58
|
-
switch (TYPE(obj)) {
|
59
|
-
case T_HASH:
|
60
|
-
yajl_gen_map_open(hand);
|
61
|
-
|
62
|
-
// TODO: itterate through keys in the hash
|
63
|
-
VALUE keys = rb_funcall(obj, intern_keys, 0);
|
64
|
-
VALUE entry;
|
65
|
-
for(idx=0; idx<RARRAY_LEN(keys); idx++) {
|
66
|
-
entry = rb_ary_entry(keys, idx);
|
67
|
-
// the key
|
68
|
-
encode_part(hand, entry, io);
|
69
|
-
// the value
|
70
|
-
encode_part(hand, rb_hash_aref(obj, entry), io);
|
71
|
-
}
|
72
|
-
|
73
|
-
yajl_gen_map_close(hand);
|
74
|
-
break;
|
75
|
-
case T_ARRAY:
|
76
|
-
yajl_gen_array_open(hand);
|
77
|
-
for(idx=0; idx<RARRAY_LEN(obj); idx++) {
|
78
|
-
otherObj = rb_ary_entry(obj, idx);
|
79
|
-
encode_part(hand, otherObj, io);
|
80
|
-
}
|
81
|
-
yajl_gen_array_close(hand);
|
82
|
-
break;
|
83
|
-
case T_NIL:
|
84
|
-
yajl_gen_null(hand);
|
1
|
+
/*
|
2
|
+
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
5
|
+
* modification, are permitted provided that the following conditions are
|
6
|
+
* met:
|
7
|
+
*
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer in
|
13
|
+
* the documentation and/or other materials provided with the
|
14
|
+
* distribution.
|
15
|
+
*
|
16
|
+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived
|
18
|
+
* from this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
|
+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
24
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
27
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
28
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
|
+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
#include "api/yajl_parse.h"
|
34
|
+
#include "yajl_lex.h"
|
35
|
+
#include "yajl_parser.h"
|
36
|
+
#include "yajl_alloc.h"
|
37
|
+
|
38
|
+
#include <stdlib.h>
|
39
|
+
#include <string.h>
|
40
|
+
#include <assert.h>
|
41
|
+
|
42
|
+
const char *
|
43
|
+
yajl_status_to_string(yajl_status stat)
|
44
|
+
{
|
45
|
+
const char * statStr = "unknown";
|
46
|
+
switch (stat) {
|
47
|
+
case yajl_status_ok:
|
48
|
+
statStr = "ok, no error";
|
85
49
|
break;
|
86
|
-
case
|
87
|
-
|
50
|
+
case yajl_status_client_canceled:
|
51
|
+
statStr = "client canceled parse";
|
88
52
|
break;
|
89
|
-
case
|
90
|
-
|
53
|
+
case yajl_status_insufficient_data:
|
54
|
+
statStr = "eof was met before the parse could complete";
|
91
55
|
break;
|
92
|
-
case
|
93
|
-
|
94
|
-
case T_BIGNUM:
|
95
|
-
str = rb_funcall(obj, intern_to_s, 0);
|
96
|
-
objLen = RSTRING_LEN(str);
|
97
|
-
yajl_gen_number(hand, RSTRING_PTR(str), (unsigned int)objLen);
|
98
|
-
break;
|
99
|
-
default:
|
100
|
-
str = rb_funcall(obj, intern_to_s, 0);
|
101
|
-
objLen = RSTRING_LEN(str);
|
102
|
-
yajl_gen_string(hand, (const unsigned char *)RSTRING_PTR(str), (unsigned int)objLen);
|
56
|
+
case yajl_status_error:
|
57
|
+
statStr = "parse error";
|
103
58
|
break;
|
104
59
|
}
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
60
|
+
return statStr;
|
61
|
+
}
|
62
|
+
|
63
|
+
yajl_handle
|
64
|
+
yajl_alloc(const yajl_callbacks * callbacks,
|
65
|
+
const yajl_parser_config * config,
|
66
|
+
const yajl_alloc_funcs * afs,
|
67
|
+
void * ctx)
|
68
|
+
{
|
69
|
+
unsigned int allowComments = 0;
|
70
|
+
unsigned int validateUTF8 = 0;
|
71
|
+
yajl_handle hand = NULL;
|
72
|
+
yajl_alloc_funcs afsBuffer;
|
73
|
+
|
74
|
+
/* first order of business is to set up memory allocation routines */
|
75
|
+
if (afs != NULL) {
|
76
|
+
if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
|
77
|
+
{
|
78
|
+
return NULL;
|
79
|
+
}
|
123
80
|
} else {
|
124
|
-
|
81
|
+
yajl_set_default_alloc_funcs(&afsBuffer);
|
82
|
+
afs = &afsBuffer;
|
125
83
|
}
|
126
|
-
check_and_fire_callback(ctx);
|
127
|
-
return 1;
|
128
|
-
}
|
129
84
|
|
130
|
-
|
131
|
-
set_static_value(ctx, rb_str_new((const char *)stringVal, stringLen));
|
132
|
-
check_and_fire_callback(ctx);
|
133
|
-
return 1;
|
134
|
-
}
|
85
|
+
hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
|
135
86
|
|
136
|
-
|
137
|
-
|
138
|
-
return 1;
|
139
|
-
}
|
87
|
+
/* copy in pointers to allocation routines */
|
88
|
+
memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
|
140
89
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
}
|
145
|
-
|
146
|
-
static int found_end_hash(void * ctx) {
|
147
|
-
if (RARRAY_LEN((VALUE)ctx) > 1) {
|
148
|
-
rb_ary_pop((VALUE)ctx);
|
90
|
+
if (config != NULL) {
|
91
|
+
allowComments = config->allowComments;
|
92
|
+
validateUTF8 = config->checkUTF8;
|
149
93
|
}
|
150
|
-
check_and_fire_callback(ctx);
|
151
|
-
return 1;
|
152
|
-
}
|
153
94
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
95
|
+
hand->callbacks = callbacks;
|
96
|
+
hand->ctx = ctx;
|
97
|
+
hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
|
98
|
+
hand->errorOffset = 0;
|
99
|
+
hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
|
100
|
+
yajl_bs_init(hand->stateStack, &(hand->alloc));
|
158
101
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
}
|
163
|
-
check_and_fire_callback(ctx);
|
164
|
-
return 1;
|
102
|
+
yajl_bs_push(hand->stateStack, yajl_state_start);
|
103
|
+
|
104
|
+
return hand;
|
165
105
|
}
|
166
106
|
|
167
|
-
|
168
|
-
|
169
|
-
|
107
|
+
void
|
108
|
+
yajl_free(yajl_handle handle)
|
109
|
+
{
|
110
|
+
yajl_bs_free(handle->stateStack);
|
111
|
+
yajl_buf_free(handle->decodeBuf);
|
112
|
+
yajl_lex_free(handle->lexer);
|
113
|
+
YA_FREE(&(handle->alloc), handle);
|
170
114
|
}
|
171
115
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
if (parse_complete_callback != Qnil) {
|
181
|
-
if (context == Qnil) {
|
182
|
-
context = rb_ary_new();
|
183
|
-
}
|
184
|
-
if (chunkedParser == NULL) {
|
185
|
-
// allocate our parser
|
186
|
-
chunkedParser = yajl_alloc(&callbacks, &cfg, NULL, (void *)context);
|
187
|
-
}
|
188
|
-
|
189
|
-
stat = yajl_parse(chunkedParser, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string));
|
190
|
-
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
191
|
-
unsigned char * str = yajl_get_error(chunkedParser, 1, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string));
|
192
|
-
rb_raise(cParseError, "%s", (const char *) str);
|
193
|
-
yajl_free_error(chunkedParser, str);
|
194
|
-
}
|
195
|
-
} else {
|
196
|
-
rb_raise(cParseError, "%s", "The on_parse_complete callback isn't setup, parsing useless.");
|
197
|
-
}
|
198
|
-
|
199
|
-
if (RARRAY_LEN(context) == 0) {
|
200
|
-
yajl_free(chunkedParser);
|
201
|
-
}
|
202
|
-
|
203
|
-
return Qnil;
|
116
|
+
yajl_status
|
117
|
+
yajl_parse(yajl_handle hand, const unsigned char * jsonText,
|
118
|
+
unsigned int jsonTextLen)
|
119
|
+
{
|
120
|
+
unsigned int offset = 0;
|
121
|
+
yajl_status status;
|
122
|
+
status = yajl_do_parse(hand, &offset, jsonText, jsonTextLen);
|
123
|
+
return status;
|
204
124
|
}
|
205
125
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
while (rb_funcall(io, intern_eof, 0) != Qtrue) {
|
218
|
-
rb_funcall(io, intern_io_read, 2, rbufsize, parsed);
|
219
|
-
|
220
|
-
stat = yajl_parse(streamParser, (const unsigned char *)RSTRING_PTR(parsed), RSTRING_LEN(parsed));
|
221
|
-
|
222
|
-
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
223
|
-
unsigned char * str = yajl_get_error(streamParser, 1, (const unsigned char *)RSTRING_PTR(parsed), RSTRING_LEN(parsed));
|
224
|
-
rb_raise(cParseError, "%s", (const char *) str);
|
225
|
-
yajl_free_error(streamParser, str);
|
226
|
-
break;
|
227
|
-
}
|
228
|
-
}
|
229
|
-
|
230
|
-
// parse any remaining buffered data
|
231
|
-
stat = yajl_parse_complete(streamParser);
|
232
|
-
yajl_free(streamParser);
|
233
|
-
|
234
|
-
if (parse_complete_callback != Qnil) {
|
235
|
-
check_and_fire_callback((void *)context);
|
236
|
-
return Qnil;
|
237
|
-
}
|
126
|
+
yajl_status
|
127
|
+
yajl_parse_complete(yajl_handle hand)
|
128
|
+
{
|
129
|
+
/* The particular case we want to handle is a trailing number.
|
130
|
+
* Further input consisting of digits could cause our interpretation
|
131
|
+
* of the number to change (buffered "1" but "2" comes in).
|
132
|
+
* A very simple approach to this is to inject whitespace to terminate
|
133
|
+
* any number in the lex buffer.
|
134
|
+
*/
|
135
|
+
return yajl_parse(hand, (const unsigned char *)" ", 1);
|
136
|
+
}
|
238
137
|
|
239
|
-
|
138
|
+
unsigned char *
|
139
|
+
yajl_get_error(yajl_handle hand, int verbose,
|
140
|
+
const unsigned char * jsonText, unsigned int jsonTextLen)
|
141
|
+
{
|
142
|
+
return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
|
240
143
|
}
|
241
144
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
unsigned int len;
|
248
|
-
VALUE outBuff;
|
249
|
-
|
250
|
-
hand = yajl_gen_alloc(&conf, NULL);
|
251
|
-
encode_part(hand, obj, io);
|
252
|
-
|
253
|
-
// just make sure we output the remaining buffer
|
254
|
-
yajl_gen_get_buf(hand, &buffer, &len);
|
255
|
-
outBuff = rb_str_new((const char *)buffer, len);
|
256
|
-
rb_io_write(io, outBuff);
|
257
|
-
|
258
|
-
yajl_gen_clear(hand);
|
259
|
-
yajl_gen_free(hand);
|
260
|
-
return Qnil;
|
145
|
+
void
|
146
|
+
yajl_free_error(yajl_handle hand, unsigned char * str)
|
147
|
+
{
|
148
|
+
/* use memory allocation functions if set */
|
149
|
+
YA_FREE(&(hand->alloc), str);
|
261
150
|
}
|
262
151
|
|
263
|
-
|
264
|
-
mYajl = rb_define_module("Yajl");
|
265
|
-
|
266
|
-
mStream = rb_define_module_under(mYajl, "Stream");
|
267
|
-
rb_define_module_function(mStream, "parse", t_parse, 1);
|
268
|
-
rb_define_module_function(mStream, "encode", t_encode, 2);
|
269
|
-
|
270
|
-
mChunked = rb_define_module_under(mYajl, "Chunked");
|
271
|
-
rb_define_module_function(mChunked, "parse_some", t_parseSome, 1);
|
272
|
-
rb_define_module_function(mChunked, "<<", t_parseSome, 1);
|
273
|
-
rb_define_module_function(mChunked, "on_parse_complete=", t_setParseComplete, 1);
|
274
|
-
|
275
|
-
VALUE rb_cStandardError = rb_const_get(rb_cObject, rb_intern("StandardError"));
|
276
|
-
cParseError = rb_define_class_under(mYajl, "ParseError", rb_cStandardError);
|
277
|
-
|
278
|
-
intern_io_read = rb_intern("read");
|
279
|
-
intern_eof = rb_intern("eof?");
|
280
|
-
intern_respond_to = rb_intern("respond_to?");
|
281
|
-
intern_call = rb_intern("call");
|
282
|
-
intern_keys = rb_intern("keys");
|
283
|
-
intern_to_s = rb_intern("to_s");
|
284
|
-
}
|
152
|
+
/* XXX: add utility routines to parse from file */
|
data/ext/yajl_alloc.c
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
5
|
+
* modification, are permitted provided that the following conditions are
|
6
|
+
* met:
|
7
|
+
*
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer in
|
13
|
+
* the documentation and/or other materials provided with the
|
14
|
+
* distribution.
|
15
|
+
*
|
16
|
+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived
|
18
|
+
* from this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
|
+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
24
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
27
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
28
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
|
+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
/**
|
34
|
+
* \file yajl_alloc.h
|
35
|
+
* default memory allocation routines for yajl which use malloc/realloc and
|
36
|
+
* free
|
37
|
+
*/
|
38
|
+
|
39
|
+
#include "yajl_alloc.h"
|
40
|
+
#include <stdlib.h>
|
41
|
+
|
42
|
+
static void * yajl_internal_malloc(void *ctx, unsigned int sz)
|
43
|
+
{
|
44
|
+
return malloc(sz);
|
45
|
+
}
|
46
|
+
|
47
|
+
static void * yajl_internal_realloc(void *ctx, void * previous,
|
48
|
+
unsigned int sz)
|
49
|
+
{
|
50
|
+
return realloc(previous, sz);
|
51
|
+
}
|
52
|
+
|
53
|
+
static void yajl_internal_free(void *ctx, void * ptr)
|
54
|
+
{
|
55
|
+
free(ptr);
|
56
|
+
}
|
57
|
+
|
58
|
+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf)
|
59
|
+
{
|
60
|
+
yaf->malloc = yajl_internal_malloc;
|
61
|
+
yaf->free = yajl_internal_free;
|
62
|
+
yaf->realloc = yajl_internal_realloc;
|
63
|
+
yaf->ctx = NULL;
|
64
|
+
}
|
65
|
+
|
data/ext/yajl_alloc.h
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
5
|
+
* modification, are permitted provided that the following conditions are
|
6
|
+
* met:
|
7
|
+
*
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer in
|
13
|
+
* the documentation and/or other materials provided with the
|
14
|
+
* distribution.
|
15
|
+
*
|
16
|
+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived
|
18
|
+
* from this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
|
+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
24
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
27
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
28
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
|
+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
/**
|
34
|
+
* \file yajl_alloc.h
|
35
|
+
* default memory allocation routines for yajl which use malloc/realloc and
|
36
|
+
* free
|
37
|
+
*/
|
38
|
+
|
39
|
+
#ifndef __YAJL_ALLOC_H__
|
40
|
+
#define __YAJL_ALLOC_H__
|
41
|
+
|
42
|
+
#include "api/yajl_common.h"
|
43
|
+
|
44
|
+
#define YA_MALLOC(afs, sz) (afs)->malloc((afs)->ctx, (sz))
|
45
|
+
#define YA_FREE(afs, ptr) (afs)->free((afs)->ctx, (ptr))
|
46
|
+
#define YA_REALLOC(afs, ptr, sz) (afs)->realloc((afs)->ctx, (ptr), (sz))
|
47
|
+
|
48
|
+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf);
|
49
|
+
|
50
|
+
#endif
|
data/ext/yajl_buf.c
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
+
*
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
5
|
+
* modification, are permitted provided that the following conditions are
|
6
|
+
* met:
|
7
|
+
*
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer in
|
13
|
+
* the documentation and/or other materials provided with the
|
14
|
+
* distribution.
|
15
|
+
*
|
16
|
+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived
|
18
|
+
* from this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
|
+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
24
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
27
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
28
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
|
+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
#include "yajl_buf.h"
|
34
|
+
|
35
|
+
#include <assert.h>
|
36
|
+
#include <stdlib.h>
|
37
|
+
#include <string.h>
|
38
|
+
|
39
|
+
#define YAJL_BUF_INIT_SIZE 2048
|
40
|
+
|
41
|
+
struct yajl_buf_t {
|
42
|
+
unsigned int len;
|
43
|
+
unsigned int used;
|
44
|
+
unsigned char * data;
|
45
|
+
yajl_alloc_funcs * alloc;
|
46
|
+
};
|
47
|
+
|
48
|
+
static
|
49
|
+
void yajl_buf_ensure_available(yajl_buf buf, unsigned int want)
|
50
|
+
{
|
51
|
+
unsigned int need;
|
52
|
+
|
53
|
+
assert(buf != NULL);
|
54
|
+
|
55
|
+
/* first call */
|
56
|
+
if (buf->data == NULL) {
|
57
|
+
buf->len = YAJL_BUF_INIT_SIZE;
|
58
|
+
buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
|
59
|
+
buf->data[0] = 0;
|
60
|
+
}
|
61
|
+
|
62
|
+
need = buf->len;
|
63
|
+
|
64
|
+
while (want >= (need - buf->used)) need <<= 1;
|
65
|
+
|
66
|
+
if (need != buf->len) {
|
67
|
+
buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
|
68
|
+
buf->len = need;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
|
73
|
+
{
|
74
|
+
yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
|
75
|
+
memset((void *) b, 0, sizeof(struct yajl_buf_t));
|
76
|
+
b->alloc = alloc;
|
77
|
+
return b;
|
78
|
+
}
|
79
|
+
|
80
|
+
void yajl_buf_free(yajl_buf buf)
|
81
|
+
{
|
82
|
+
assert(buf != NULL);
|
83
|
+
if (buf->data) YA_FREE(buf->alloc, buf->data);
|
84
|
+
YA_FREE(buf->alloc, buf);
|
85
|
+
}
|
86
|
+
|
87
|
+
void yajl_buf_append(yajl_buf buf, const void * data, unsigned int len)
|
88
|
+
{
|
89
|
+
yajl_buf_ensure_available(buf, len);
|
90
|
+
if (len > 0) {
|
91
|
+
assert(data != NULL);
|
92
|
+
memcpy(buf->data + buf->used, data, len);
|
93
|
+
buf->used += len;
|
94
|
+
buf->data[buf->used] = 0;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
void yajl_buf_clear(yajl_buf buf)
|
99
|
+
{
|
100
|
+
buf->used = 0;
|
101
|
+
if (buf->data) buf->data[buf->used] = 0;
|
102
|
+
}
|
103
|
+
|
104
|
+
const unsigned char * yajl_buf_data(yajl_buf buf)
|
105
|
+
{
|
106
|
+
return buf->data;
|
107
|
+
}
|
108
|
+
|
109
|
+
unsigned int yajl_buf_len(yajl_buf buf)
|
110
|
+
{
|
111
|
+
return buf->used;
|
112
|
+
}
|
113
|
+
|
114
|
+
void
|
115
|
+
yajl_buf_truncate(yajl_buf buf, unsigned int len)
|
116
|
+
{
|
117
|
+
assert(len <= buf->used);
|
118
|
+
buf->used = len;
|
119
|
+
}
|