yajl-ruby 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of yajl-ruby might be problematic. Click here for more details.
- data/CHANGELOG.md +8 -1
- data/MIT-LICENSE +3 -3
- data/README.rdoc +8 -8
- data/VERSION.yml +1 -1
- data/benchmark/subjects/ohai.yml +171 -171
- data/ext/api/yajl_common.h +9 -9
- data/ext/api/yajl_gen.h +10 -10
- data/ext/api/yajl_parse.h +15 -15
- data/ext/yajl.c +8 -8
- data/ext/yajl_alloc.c +6 -6
- data/ext/yajl_alloc.h +6 -6
- data/ext/yajl_buf.c +7 -7
- data/ext/yajl_buf.h +7 -7
- data/ext/yajl_bytestack.h +10 -10
- data/ext/yajl_encode.c +12 -12
- data/ext/yajl_encode.h +6 -6
- data/ext/yajl_ext.c +67 -80
- data/ext/yajl_ext.h +4 -4
- data/ext/yajl_gen.c +16 -16
- data/ext/yajl_lex.c +79 -76
- data/ext/yajl_lex.h +14 -14
- data/ext/yajl_parser.c +34 -34
- data/ext/yajl_parser.h +7 -7
- data/lib/yajl.rb +7 -7
- data/lib/yajl/bzip2/stream_reader.rb +1 -11
- data/lib/yajl/bzip2/stream_writer.rb +1 -1
- data/lib/yajl/deflate/stream_reader.rb +6 -7
- data/lib/yajl/deflate/stream_writer.rb +2 -2
- data/lib/yajl/gzip/stream_reader.rb +0 -10
- data/lib/yajl/http_stream.rb +12 -12
- data/lib/yajl/json_gem/encoding.rb +4 -4
- data/lib/yajl/json_gem/parsing.rb +3 -3
- data/spec/encoding/encoding_spec.rb +23 -23
- data/spec/global/global_spec.rb +8 -8
- data/spec/http/http_delete_spec.rb +13 -13
- data/spec/http/http_error_spec.rb +2 -2
- data/spec/http/http_get_spec.rb +15 -15
- data/spec/http/http_post_spec.rb +12 -12
- data/spec/http/http_put_spec.rb +14 -14
- data/spec/json_gem_compatibility/compatibility_spec.rb +21 -21
- data/spec/parsing/active_support_spec.rb +6 -6
- data/spec/parsing/chunked_spec.rb +12 -12
- data/spec/parsing/fixtures_spec.rb +4 -4
- data/spec/parsing/one_off_spec.rb +9 -9
- data/yajl-ruby.gemspec +3 -3
- metadata +12 -5
data/ext/yajl_bytestack.h
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
/*
|
2
2
|
* Copyright 2009, Lloyd Hilaiel.
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* Redistribution and use in source and binary forms, with or without
|
5
5
|
* modification, are permitted provided that the following conditions are
|
6
6
|
* met:
|
7
|
-
*
|
7
|
+
*
|
8
8
|
* 1. Redistributions of source code must retain the above copyright
|
9
9
|
* notice, this list of conditions and the following disclaimer.
|
10
|
-
*
|
10
|
+
*
|
11
11
|
* 2. Redistributions in binary form must reproduce the above copyright
|
12
12
|
* notice, this list of conditions and the following disclaimer in
|
13
13
|
* the documentation and/or other materials provided with the
|
14
14
|
* distribution.
|
15
|
-
*
|
15
|
+
*
|
16
16
|
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
17
|
* contributors may be used to endorse or promote products derived
|
18
18
|
* from this software without specific prior written permission.
|
19
|
-
*
|
19
|
+
*
|
20
20
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
21
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
22
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
@@ -28,7 +28,7 @@
|
|
28
28
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
29
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
30
|
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
*/
|
31
|
+
*/
|
32
32
|
|
33
33
|
/*
|
34
34
|
* A header only implementation of a simple stack of bytes, used in YAJL
|
@@ -61,7 +61,7 @@ typedef struct yajl_bytestack_t
|
|
61
61
|
|
62
62
|
/* initialize a bytestack */
|
63
63
|
#define yajl_bs_free(obs) \
|
64
|
-
if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
|
64
|
+
if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
|
65
65
|
|
66
66
|
#define yajl_bs_current(obs) \
|
67
67
|
(assert((obs).used > 0), (obs).stack[(obs).used - 1])
|
@@ -74,12 +74,12 @@ typedef struct yajl_bytestack_t
|
|
74
74
|
} \
|
75
75
|
(obs).stack[((obs).used)++] = (byte); \
|
76
76
|
}
|
77
|
-
|
77
|
+
|
78
78
|
/* removes the top item of the stack, returns nothing */
|
79
79
|
#define yajl_bs_pop(obs) { ((obs).used)--; }
|
80
80
|
|
81
81
|
#define yajl_bs_set(obs, byte) \
|
82
|
-
(obs).stack[((obs).used) - 1] = (byte);
|
83
|
-
|
82
|
+
(obs).stack[((obs).used) - 1] = (byte);
|
83
|
+
|
84
84
|
|
85
85
|
#endif
|
data/ext/yajl_encode.c
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
/*
|
2
2
|
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* Redistribution and use in source and binary forms, with or without
|
5
5
|
* modification, are permitted provided that the following conditions are
|
6
6
|
* met:
|
7
|
-
*
|
7
|
+
*
|
8
8
|
* 1. Redistributions of source code must retain the above copyright
|
9
9
|
* notice, this list of conditions and the following disclaimer.
|
10
|
-
*
|
10
|
+
*
|
11
11
|
* 2. Redistributions in binary form must reproduce the above copyright
|
12
12
|
* notice, this list of conditions and the following disclaimer in
|
13
13
|
* the documentation and/or other materials provided with the
|
14
14
|
* distribution.
|
15
|
-
*
|
15
|
+
*
|
16
16
|
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
17
|
* contributors may be used to endorse or promote products derived
|
18
18
|
* from this software without specific prior written permission.
|
19
|
-
*
|
19
|
+
*
|
20
20
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
21
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
22
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
@@ -28,7 +28,7 @@
|
|
28
28
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
29
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
30
|
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
*/
|
31
|
+
*/
|
32
32
|
|
33
33
|
#include "yajl_encode.h"
|
34
34
|
|
@@ -58,7 +58,7 @@ yajl_string_encode2(const yajl_print_t print,
|
|
58
58
|
unsigned int len)
|
59
59
|
{
|
60
60
|
unsigned int beg = 0;
|
61
|
-
unsigned int end = 0;
|
61
|
+
unsigned int end = 0;
|
62
62
|
char hexBuf[7];
|
63
63
|
hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0';
|
64
64
|
hexBuf[6] = 0;
|
@@ -104,7 +104,7 @@ static void hexToDigit(unsigned int * val, const unsigned char * hex)
|
|
104
104
|
}
|
105
105
|
}
|
106
106
|
|
107
|
-
static void Utf32toUtf8(unsigned int codepoint, char * utf8Buf)
|
107
|
+
static void Utf32toUtf8(unsigned int codepoint, char * utf8Buf)
|
108
108
|
{
|
109
109
|
if (codepoint < 0x80) {
|
110
110
|
utf8Buf[0] = (char) codepoint;
|
@@ -134,7 +134,7 @@ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
|
|
134
134
|
unsigned int len)
|
135
135
|
{
|
136
136
|
unsigned int beg = 0;
|
137
|
-
unsigned int end = 0;
|
137
|
+
unsigned int end = 0;
|
138
138
|
|
139
139
|
while (end < len) {
|
140
140
|
if (str[end] == '\\') {
|
@@ -161,8 +161,8 @@ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
|
|
161
161
|
unsigned int surrogate = 0;
|
162
162
|
hexToDigit(&surrogate, str + end + 2);
|
163
163
|
codepoint =
|
164
|
-
(((codepoint & 0x3F) << 10) |
|
165
|
-
((((codepoint >> 6) & 0xF) + 1) << 16) |
|
164
|
+
(((codepoint & 0x3F) << 10) |
|
165
|
+
((((codepoint >> 6) & 0xF) + 1) << 16) |
|
166
166
|
(surrogate & 0x3FF));
|
167
167
|
end += 5;
|
168
168
|
} else {
|
@@ -170,7 +170,7 @@ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
|
|
170
170
|
break;
|
171
171
|
}
|
172
172
|
}
|
173
|
-
|
173
|
+
|
174
174
|
Utf32toUtf8(codepoint, utf8Buf);
|
175
175
|
unescaped = utf8Buf;
|
176
176
|
break;
|
data/ext/yajl_encode.h
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
/*
|
2
2
|
* Copyright 2007-2009, Lloyd Hilaiel.
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* Redistribution and use in source and binary forms, with or without
|
5
5
|
* modification, are permitted provided that the following conditions are
|
6
6
|
* met:
|
7
|
-
*
|
7
|
+
*
|
8
8
|
* 1. Redistributions of source code must retain the above copyright
|
9
9
|
* notice, this list of conditions and the following disclaimer.
|
10
|
-
*
|
10
|
+
*
|
11
11
|
* 2. Redistributions in binary form must reproduce the above copyright
|
12
12
|
* notice, this list of conditions and the following disclaimer in
|
13
13
|
* the documentation and/or other materials provided with the
|
14
14
|
* distribution.
|
15
|
-
*
|
15
|
+
*
|
16
16
|
* 3. Neither the name of Lloyd Hilaiel nor the names of its
|
17
17
|
* contributors may be used to endorse or promote products derived
|
18
18
|
* from this software without specific prior written permission.
|
19
|
-
*
|
19
|
+
*
|
20
20
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
21
21
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
22
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
@@ -28,7 +28,7 @@
|
|
28
28
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
29
29
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
30
|
* POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
*/
|
31
|
+
*/
|
32
32
|
|
33
33
|
#ifndef __YAJL_ENCODE_H__
|
34
34
|
#define __YAJL_ENCODE_H__
|
data/ext/yajl_ext.c
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
* Copyright (c) 2008-2009 Brian Lopez - http://github.com/brianmario
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
* a copy of this software and associated documentation files (the
|
6
6
|
* "Software"), to deal in the Software without restriction, including
|
@@ -8,10 +8,10 @@
|
|
8
8
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
9
9
|
* permit persons to whom the Software is furnished to do so, subject to
|
10
10
|
* the following conditions:
|
11
|
-
*
|
11
|
+
*
|
12
12
|
* The above copyright notice and this permission notice shall be
|
13
13
|
* included in all copies or substantial portions of the Software.
|
14
|
-
*
|
14
|
+
*
|
15
15
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
16
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
17
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
@@ -23,36 +23,11 @@
|
|
23
23
|
|
24
24
|
#include "yajl_ext.h"
|
25
25
|
|
26
|
-
inline const char * strnchr(const char * s, size_t count, char c) {
|
27
|
-
for (; count-- && *s != '\0'; ++s) {
|
28
|
-
if (*s == c) {
|
29
|
-
return s;
|
30
|
-
}
|
31
|
-
}
|
32
|
-
return NULL;
|
33
|
-
}
|
34
|
-
|
35
|
-
inline double strntod(const char * s, size_t count) {
|
36
|
-
char buf[count+1];
|
37
|
-
memcpy(buf, s, count);
|
38
|
-
buf[count] = 0;
|
39
|
-
|
40
|
-
return strtod(buf, NULL);
|
41
|
-
}
|
42
|
-
|
43
|
-
inline VALUE rb_cstrn2inum(const char * s, size_t count) {
|
44
|
-
char buf[count+1];
|
45
|
-
memcpy(buf, s, count);
|
46
|
-
buf[count] = 0;
|
47
|
-
|
48
|
-
return rb_cstr2inum(buf, 10);
|
49
|
-
}
|
50
|
-
|
51
26
|
/* Helpers for building objects */
|
52
27
|
inline void yajl_check_and_fire_callback(void * ctx) {
|
53
28
|
yajl_parser_wrapper * wrapper;
|
54
29
|
GetParser((VALUE)ctx, wrapper);
|
55
|
-
|
30
|
+
|
56
31
|
/* No need to do any of this if the callback isn't even setup */
|
57
32
|
if (wrapper->parse_complete_callback != Qnil) {
|
58
33
|
int len = RARRAY_LEN(wrapper->builderStack);
|
@@ -74,9 +49,9 @@ inline void yajl_set_static_value(void * ctx, VALUE val) {
|
|
74
49
|
yajl_parser_wrapper * wrapper;
|
75
50
|
VALUE lastEntry, hash;
|
76
51
|
int len;
|
77
|
-
|
52
|
+
|
78
53
|
GetParser((VALUE)ctx, wrapper);
|
79
|
-
|
54
|
+
|
80
55
|
len = RARRAY_LEN(wrapper->builderStack);
|
81
56
|
if (len > 0) {
|
82
57
|
lastEntry = rb_ary_entry(wrapper->builderStack, len-1);
|
@@ -132,7 +107,7 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
|
|
132
107
|
const unsigned char * buffer;
|
133
108
|
const char * cptr;
|
134
109
|
unsigned int len;
|
135
|
-
|
110
|
+
|
136
111
|
if (io != Qnil || w->on_progress_callback != Qnil) {
|
137
112
|
status = yajl_gen_get_buf(w->encoder, &buffer, &len);
|
138
113
|
if (len >= WRITE_BUFSIZE) {
|
@@ -145,11 +120,11 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
|
|
145
120
|
yajl_gen_clear(w->encoder);
|
146
121
|
}
|
147
122
|
}
|
148
|
-
|
123
|
+
|
149
124
|
switch (TYPE(obj)) {
|
150
125
|
case T_HASH:
|
151
126
|
status = yajl_gen_map_open(w->encoder);
|
152
|
-
|
127
|
+
|
153
128
|
/* TODO: itterate through keys in the hash */
|
154
129
|
VALUE keys = rb_funcall(obj, intern_keys, 0);
|
155
130
|
VALUE entry, keyStr;
|
@@ -161,7 +136,7 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
|
|
161
136
|
/* the value */
|
162
137
|
yajl_encode_part(w, rb_hash_aref(obj, entry), io);
|
163
138
|
}
|
164
|
-
|
139
|
+
|
165
140
|
status = yajl_gen_map_close(w->encoder);
|
166
141
|
break;
|
167
142
|
case T_ARRAY:
|
@@ -186,23 +161,29 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
|
|
186
161
|
case T_BIGNUM:
|
187
162
|
str = rb_funcall(obj, intern_to_s, 0);
|
188
163
|
cptr = RSTRING_PTR(str);
|
164
|
+
len = RSTRING_LEN(str);
|
189
165
|
if (strcmp(cptr, "NaN") == 0 || strcmp(cptr, "Infinity") == 0 || strcmp(cptr, "-Infinity") == 0) {
|
190
166
|
rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
|
191
167
|
}
|
192
|
-
status = yajl_gen_number(w->encoder, cptr,
|
168
|
+
status = yajl_gen_number(w->encoder, cptr, len);
|
193
169
|
break;
|
194
170
|
case T_STRING:
|
195
171
|
cptr = RSTRING_PTR(obj);
|
196
|
-
|
172
|
+
len = RSTRING_LEN(obj);
|
173
|
+
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
|
197
174
|
break;
|
198
175
|
default:
|
199
176
|
if (rb_respond_to(obj, intern_to_json)) {
|
200
177
|
str = rb_funcall(obj, intern_to_json, 0);
|
178
|
+
cptr = RSTRING_PTR(str);
|
179
|
+
len = RSTRING_LEN(str);
|
180
|
+
status = yajl_gen_number(w->encoder, cptr, len);
|
201
181
|
} else {
|
202
182
|
str = rb_funcall(obj, intern_to_s, 0);
|
183
|
+
cptr = RSTRING_PTR(str);
|
184
|
+
len = RSTRING_LEN(str);
|
185
|
+
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
|
203
186
|
}
|
204
|
-
cptr = RSTRING_PTR(str);
|
205
|
-
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, (unsigned int)strlen(cptr));
|
206
187
|
break;
|
207
188
|
}
|
208
189
|
}
|
@@ -225,9 +206,9 @@ void yajl_parser_wrapper_mark(void * wrapper) {
|
|
225
206
|
|
226
207
|
void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser) {
|
227
208
|
yajl_status stat;
|
228
|
-
|
209
|
+
|
229
210
|
stat = yajl_parse(parser, chunk, len);
|
230
|
-
|
211
|
+
|
231
212
|
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
232
213
|
unsigned char * str = yajl_get_error(parser, 1, chunk, len);
|
233
214
|
rb_raise(cParseError, "%s", (const char *) str);
|
@@ -249,13 +230,16 @@ static int yajl_found_boolean(void * ctx, int boolean) {
|
|
249
230
|
}
|
250
231
|
|
251
232
|
static int yajl_found_number(void * ctx, const char * numberVal, unsigned int numberLen) {
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
233
|
+
char buf[numberLen+1];
|
234
|
+
buf[numberLen] = 0;
|
235
|
+
memcpy(buf, numberVal, numberLen);
|
236
|
+
|
237
|
+
if (memchr(buf, '.', numberLen) ||
|
238
|
+
memchr(buf, 'e', numberLen) ||
|
239
|
+
memchr(buf, 'E', numberLen)) {
|
240
|
+
yajl_set_static_value(ctx, rb_float_new(strtod(buf, NULL)));
|
241
|
+
} else {
|
242
|
+
yajl_set_static_value(ctx, rb_cstr2inum(buf, 10));
|
259
243
|
}
|
260
244
|
return 1;
|
261
245
|
}
|
@@ -339,7 +323,7 @@ static int yajl_found_end_array(void * ctx) {
|
|
339
323
|
* The only basic requirment currently is that the IO object respond to #read(len) and #eof?
|
340
324
|
* The IO is parsed until a complete JSON object has been read and a ruby object will be returned.
|
341
325
|
*/
|
342
|
-
|
326
|
+
|
343
327
|
/*
|
344
328
|
* Document-method: new
|
345
329
|
*
|
@@ -356,11 +340,11 @@ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE klass) {
|
|
356
340
|
yajl_parser_config cfg;
|
357
341
|
VALUE opts, obj;
|
358
342
|
int allowComments = 1, checkUTF8 = 1, symbolizeKeys = 0;
|
359
|
-
|
343
|
+
|
360
344
|
/* Scan off config vars */
|
361
345
|
if (rb_scan_args(argc, argv, "01", &opts) == 1) {
|
362
346
|
Check_Type(opts, T_HASH);
|
363
|
-
|
347
|
+
|
364
348
|
if (rb_hash_aref(opts, sym_allow_comments) == Qfalse) {
|
365
349
|
allowComments = 0;
|
366
350
|
}
|
@@ -372,7 +356,7 @@ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE klass) {
|
|
372
356
|
}
|
373
357
|
}
|
374
358
|
cfg = (yajl_parser_config){allowComments, checkUTF8};
|
375
|
-
|
359
|
+
|
376
360
|
obj = Data_Make_Struct(klass, yajl_parser_wrapper, yajl_parser_wrapper_mark, yajl_parser_wrapper_free, wrapper);
|
377
361
|
wrapper->parser = yajl_alloc(&callbacks, &cfg, NULL, (void *)obj);
|
378
362
|
wrapper->nestedArrayLevel = 0;
|
@@ -425,10 +409,11 @@ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) {
|
|
425
409
|
yajl_status stat;
|
426
410
|
yajl_parser_wrapper * wrapper;
|
427
411
|
VALUE rbufsize, input, blk;
|
412
|
+
unsigned int len;
|
428
413
|
const char * cptr;
|
429
|
-
|
414
|
+
|
430
415
|
GetParser(self, wrapper);
|
431
|
-
|
416
|
+
|
432
417
|
/* setup our parameters */
|
433
418
|
rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk);
|
434
419
|
if (NIL_P(rbufsize)) {
|
@@ -439,24 +424,25 @@ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) {
|
|
439
424
|
if (!NIL_P(blk)) {
|
440
425
|
rb_yajl_parser_set_complete_cb(self, blk);
|
441
426
|
}
|
442
|
-
|
427
|
+
|
443
428
|
if (TYPE(input) == T_STRING) {
|
444
429
|
cptr = RSTRING_PTR(input);
|
445
|
-
|
446
|
-
|
430
|
+
len = RSTRING_LEN(input);
|
431
|
+
yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser);
|
432
|
+
} else if (rb_respond_to(input, intern_io_read)) {
|
447
433
|
VALUE parsed = rb_str_new("", READ_BUFSIZE);
|
448
|
-
while (rb_funcall(input,
|
449
|
-
parsed = rb_funcall(input, intern_io_read, 1, rbufsize);
|
434
|
+
while ((parsed = rb_funcall(input, intern_io_read, 1, rbufsize)) != Qnil) {
|
450
435
|
cptr = RSTRING_PTR(parsed);
|
451
|
-
|
436
|
+
len = RSTRING_LEN(parsed);
|
437
|
+
yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser);
|
452
438
|
}
|
453
439
|
} else {
|
454
440
|
rb_raise(cParseError, "input must be a string or IO");
|
455
441
|
}
|
456
|
-
|
442
|
+
|
457
443
|
/* parse any remaining buffered data */
|
458
444
|
stat = yajl_parse_complete(wrapper->parser);
|
459
|
-
|
445
|
+
|
460
446
|
if (wrapper->parse_complete_callback != Qnil) {
|
461
447
|
yajl_check_and_fire_callback((void *)self);
|
462
448
|
return Qnil;
|
@@ -478,16 +464,18 @@ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) {
|
|
478
464
|
*/
|
479
465
|
static VALUE rb_yajl_parser_parse_chunk(VALUE self, VALUE chunk) {
|
480
466
|
yajl_parser_wrapper * wrapper;
|
481
|
-
|
467
|
+
unsigned int len;
|
468
|
+
|
482
469
|
GetParser(self, wrapper);
|
483
470
|
if (NIL_P(chunk)) {
|
484
471
|
rb_raise(cParseError, "Can't parse a nil string.");
|
485
472
|
return Qnil;
|
486
473
|
}
|
487
|
-
|
474
|
+
|
488
475
|
if (wrapper->parse_complete_callback != Qnil) {
|
489
476
|
const char * cptr = RSTRING_PTR(chunk);
|
490
|
-
|
477
|
+
len = RSTRING_LEN(chunk);
|
478
|
+
yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser);
|
491
479
|
} else {
|
492
480
|
rb_raise(cParseError, "The on_parse_complete callback isn't setup, parsing useless.");
|
493
481
|
}
|
@@ -540,11 +528,11 @@ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
|
|
540
528
|
VALUE opts, obj, indent;
|
541
529
|
const char * indentString = " ";
|
542
530
|
int beautify = 0;
|
543
|
-
|
531
|
+
|
544
532
|
/* Scan off config vars */
|
545
533
|
if (rb_scan_args(argc, argv, "01", &opts) == 1) {
|
546
534
|
Check_Type(opts, T_HASH);
|
547
|
-
|
535
|
+
|
548
536
|
if (rb_hash_aref(opts, sym_pretty) == Qtrue) {
|
549
537
|
beautify = 1;
|
550
538
|
indent = rb_hash_aref(opts, sym_indent);
|
@@ -558,7 +546,7 @@ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
|
|
558
546
|
}
|
559
547
|
}
|
560
548
|
cfg = (yajl_gen_config){beautify, indentString};
|
561
|
-
|
549
|
+
|
562
550
|
obj = Data_Make_Struct(klass, yajl_encoder_wrapper, yajl_encoder_wrapper_mark, yajl_encoder_wrapper_free, wrapper);
|
563
551
|
wrapper->encoder = yajl_gen_alloc(&cfg, NULL);
|
564
552
|
wrapper->on_progress_callback = Qnil;
|
@@ -616,15 +604,15 @@ static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
|
|
616
604
|
const unsigned char * buffer;
|
617
605
|
unsigned int len;
|
618
606
|
VALUE obj, io, blk, outBuff;
|
619
|
-
|
607
|
+
|
620
608
|
GetEncoder(self, wrapper);
|
621
|
-
|
609
|
+
|
622
610
|
rb_scan_args(argc, argv, "11&", &obj, &io, &blk);
|
623
|
-
|
611
|
+
|
624
612
|
if (blk != Qnil) {
|
625
613
|
wrapper->on_progress_callback = blk;
|
626
614
|
}
|
627
|
-
|
615
|
+
|
628
616
|
/* begin encode process */
|
629
617
|
yajl_encode_part(wrapper, obj, io);
|
630
618
|
|
@@ -635,7 +623,7 @@ static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
|
|
635
623
|
rb_enc_associate_index(outBuff, utf8Encoding);
|
636
624
|
#endif
|
637
625
|
yajl_gen_clear(wrapper->encoder);
|
638
|
-
|
626
|
+
|
639
627
|
if (io != Qnil) {
|
640
628
|
rb_io_write(io, outBuff);
|
641
629
|
if (wrapper->terminator != 0 && wrapper->terminator != Qnil) {
|
@@ -871,10 +859,10 @@ static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass) {
|
|
871
859
|
/* Ruby Extension initializer */
|
872
860
|
void Init_yajl_ext() {
|
873
861
|
mYajl = rb_define_module("Yajl");
|
874
|
-
|
862
|
+
|
875
863
|
cParseError = rb_define_class_under(mYajl, "ParseError", rb_eStandardError);
|
876
864
|
cEncodeError = rb_define_class_under(mYajl, "EncodeError", rb_eStandardError);
|
877
|
-
|
865
|
+
|
878
866
|
cParser = rb_define_class_under(mYajl, "Parser", rb_cObject);
|
879
867
|
rb_define_singleton_method(cParser, "new", rb_yajl_parser_new, -1);
|
880
868
|
rb_define_method(cParser, "initialize", rb_yajl_parser_init, -1);
|
@@ -882,24 +870,23 @@ void Init_yajl_ext() {
|
|
882
870
|
rb_define_method(cParser, "parse_chunk", rb_yajl_parser_parse_chunk, -1);
|
883
871
|
rb_define_method(cParser, "<<", rb_yajl_parser_parse_chunk, 1);
|
884
872
|
rb_define_method(cParser, "on_parse_complete=", rb_yajl_parser_set_complete_cb, 1);
|
885
|
-
|
873
|
+
|
886
874
|
cEncoder = rb_define_class_under(mYajl, "Encoder", rb_cObject);
|
887
875
|
rb_define_singleton_method(cEncoder, "new", rb_yajl_encoder_new, -1);
|
888
876
|
rb_define_method(cEncoder, "initialize", rb_yajl_encoder_init, -1);
|
889
877
|
rb_define_method(cEncoder, "encode", rb_yajl_encoder_encode, -1);
|
890
878
|
rb_define_method(cEncoder, "on_progress=", rb_yajl_encoder_set_progress_cb, 1);
|
891
|
-
|
879
|
+
|
892
880
|
rb_define_singleton_method(cEncoder, "enable_json_gem_compatability", rb_yajl_encoder_enable_json_gem_ext, 0);
|
893
|
-
|
881
|
+
|
894
882
|
intern_io_read = rb_intern("read");
|
895
|
-
intern_eof = rb_intern("eof?");
|
896
883
|
intern_call = rb_intern("call");
|
897
884
|
intern_keys = rb_intern("keys");
|
898
885
|
intern_to_s = rb_intern("to_s");
|
899
886
|
intern_to_json = rb_intern("to_json");
|
900
887
|
intern_to_sym = rb_intern("to_sym");
|
901
888
|
intern_has_key = rb_intern("has_key?");
|
902
|
-
|
889
|
+
|
903
890
|
sym_allow_comments = ID2SYM(rb_intern("allow_comments"));
|
904
891
|
sym_check_utf8 = ID2SYM(rb_intern("check_utf8"));
|
905
892
|
sym_pretty = ID2SYM(rb_intern("pretty"));
|