ffi-yajl 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/Rakefile +11 -12
- data/bin/ffi-yajl-bench +4 -5
- data/ext/ffi_yajl/ext/dlopen/extconf.rb +1 -0
- data/ext/ffi_yajl/ext/encoder/encoder.c +9 -4
- data/ext/ffi_yajl/ext/encoder/extconf.rb +2 -1
- data/ext/ffi_yajl/ext/parser/extconf.rb +2 -1
- data/ext/ffi_yajl/ext/parser/parser.c +1 -0
- data/lib/ffi_yajl/benchmark/encode.rb +32 -90
- data/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +14 -14
- data/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +13 -19
- data/lib/ffi_yajl/benchmark/encode_profile.rb +11 -15
- data/lib/ffi_yajl/benchmark/http.rb +9 -13
- data/lib/ffi_yajl/benchmark/parse.rb +71 -124
- data/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +17 -17
- data/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +19 -19
- data/lib/ffi_yajl/benchmark/parse_profile.rb +10 -14
- data/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +11 -16
- data/lib/ffi_yajl/benchmark/parse_stream.rb +17 -17
- data/lib/ffi_yajl/benchmark.rb +0 -1
- data/lib/ffi_yajl/encoder.rb +3 -3
- data/lib/ffi_yajl/ffi/encoder.rb +29 -32
- data/lib/ffi_yajl/ffi/parser.rb +30 -37
- data/lib/ffi_yajl/ffi.rb +23 -24
- data/lib/ffi_yajl/map_library_name.rb +0 -1
- data/lib/ffi_yajl/parser.rb +5 -6
- data/lib/ffi_yajl/version.rb +1 -1
- data/spec/ffi_yajl/encoder_spec.rb +24 -25
- data/spec/ffi_yajl/map_library_name_spec.rb +0 -2
- data/spec/ffi_yajl/parser_spec.rb +54 -56
- data/spec/spec_helper.rb +8 -9
- metadata +2 -2
data/lib/ffi_yajl/ffi/encoder.rb
CHANGED
@@ -20,13 +20,14 @@
|
|
20
20
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
|
23
|
-
require '
|
23
|
+
require 'date'
|
24
|
+
require 'stringio'
|
24
25
|
|
25
26
|
module FFI_Yajl
|
26
27
|
module FFI
|
27
28
|
module Encoder
|
28
29
|
def do_yajl_encode(obj, yajl_gen_opts, opts)
|
29
|
-
yajl_gen = FFI_Yajl.yajl_gen_alloc(nil)
|
30
|
+
yajl_gen = FFI_Yajl.yajl_gen_alloc(nil)
|
30
31
|
|
31
32
|
# configure the yajl encoder
|
32
33
|
if yajl_gen_opts[:yajl_gen_beautify]
|
@@ -40,8 +41,8 @@ module FFI_Yajl
|
|
40
41
|
|
41
42
|
# setup our own state
|
42
43
|
state = {
|
43
|
-
:
|
44
|
-
:
|
44
|
+
json_opts: opts,
|
45
|
+
processing_key: false,
|
45
46
|
}
|
46
47
|
|
47
48
|
# do the encoding
|
@@ -53,14 +54,12 @@ module FFI_Yajl
|
|
53
54
|
if ( status = FFI_Yajl.yajl_gen_get_buf(yajl_gen, string_ptr, length_ptr) ) != 0
|
54
55
|
FFI_Yajl::Encoder.raise_error_for_status(status)
|
55
56
|
end
|
56
|
-
length = length_ptr.read_int
|
57
57
|
string = string_ptr.get_pointer(0).read_string
|
58
58
|
|
59
59
|
FFI_Yajl.yajl_gen_free(yajl_gen)
|
60
60
|
|
61
|
-
|
61
|
+
string
|
62
62
|
end
|
63
|
-
|
64
63
|
end
|
65
64
|
end
|
66
65
|
end
|
@@ -68,7 +67,7 @@ end
|
|
68
67
|
class Hash
|
69
68
|
def ffi_yajl(yajl_gen, state)
|
70
69
|
if state[:processing_key]
|
71
|
-
str =
|
70
|
+
str = to_s
|
72
71
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
73
72
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
74
73
|
end
|
@@ -76,7 +75,7 @@ class Hash
|
|
76
75
|
if ( status = FFI_Yajl.yajl_gen_map_open(yajl_gen) ) != 0
|
77
76
|
FFI_Yajl::Encoder.raise_error_for_status(status, '{')
|
78
77
|
end
|
79
|
-
|
78
|
+
each do |key, value|
|
80
79
|
# Perf Fix: mutate state hash rather than creating new copy
|
81
80
|
state[:processing_key] = true
|
82
81
|
key.ffi_yajl(yajl_gen, state)
|
@@ -93,7 +92,7 @@ end
|
|
93
92
|
class Array
|
94
93
|
def ffi_yajl(yajl_gen, state)
|
95
94
|
if state[:processing_key]
|
96
|
-
str =
|
95
|
+
str = to_s
|
97
96
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
98
97
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
99
98
|
end
|
@@ -101,7 +100,7 @@ class Array
|
|
101
100
|
if ( status = FFI_Yajl.yajl_gen_array_open(yajl_gen) ) != 0
|
102
101
|
FFI_Yajl::Encoder.raise_error_for_status(status, '[')
|
103
102
|
end
|
104
|
-
|
103
|
+
each do |value|
|
105
104
|
value.ffi_yajl(yajl_gen, state)
|
106
105
|
end
|
107
106
|
if ( status = FFI_Yajl.yajl_gen_array_close(yajl_gen) ) != 0
|
@@ -113,7 +112,7 @@ end
|
|
113
112
|
|
114
113
|
class NilClass
|
115
114
|
def ffi_yajl(yajl_gen, state)
|
116
|
-
str =
|
115
|
+
str = to_s
|
117
116
|
if state[:processing_key]
|
118
117
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
119
118
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
@@ -128,7 +127,7 @@ end
|
|
128
127
|
|
129
128
|
class TrueClass
|
130
129
|
def ffi_yajl(yajl_gen, state)
|
131
|
-
str =
|
130
|
+
str = to_s
|
132
131
|
if state[:processing_key]
|
133
132
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
134
133
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
@@ -143,7 +142,7 @@ end
|
|
143
142
|
|
144
143
|
class FalseClass
|
145
144
|
def ffi_yajl(yajl_gen, state)
|
146
|
-
str =
|
145
|
+
str = to_s
|
147
146
|
if state[:processing_key]
|
148
147
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
149
148
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
@@ -158,9 +157,9 @@ end
|
|
158
157
|
|
159
158
|
class Fixnum
|
160
159
|
def ffi_yajl(yajl_gen, state)
|
161
|
-
str =
|
160
|
+
str = to_s
|
162
161
|
if str == "NaN" || str == "Infinity" || str == "-Infinity"
|
163
|
-
raise ::FFI_Yajl::EncodeError
|
162
|
+
raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number"
|
164
163
|
end
|
165
164
|
if state[:processing_key]
|
166
165
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
@@ -176,9 +175,9 @@ end
|
|
176
175
|
|
177
176
|
class Bignum
|
178
177
|
def ffi_yajl(yajl_gen, state)
|
179
|
-
str =
|
178
|
+
str = to_s
|
180
179
|
if str == "NaN" || str == "Infinity" || str == "-Infinity"
|
181
|
-
raise ::FFI_Yajl::EncodeError
|
180
|
+
raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number"
|
182
181
|
end
|
183
182
|
if state[:processing_key]
|
184
183
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
@@ -194,9 +193,9 @@ end
|
|
194
193
|
|
195
194
|
class Float
|
196
195
|
def ffi_yajl(yajl_gen, state)
|
197
|
-
str =
|
196
|
+
str = to_s
|
198
197
|
if str == "NaN" || str == "Infinity" || str == "-Infinity"
|
199
|
-
raise ::FFI_Yajl::EncodeError
|
198
|
+
raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number"
|
200
199
|
end
|
201
200
|
if state[:processing_key]
|
202
201
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
@@ -212,7 +211,7 @@ end
|
|
212
211
|
|
213
212
|
class Symbol
|
214
213
|
def ffi_yajl(yajl_gen, state)
|
215
|
-
str =
|
214
|
+
str = to_s
|
216
215
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
217
216
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
218
217
|
end
|
@@ -221,7 +220,7 @@ end
|
|
221
220
|
|
222
221
|
class String
|
223
222
|
def ffi_yajl(yajl_gen, state)
|
224
|
-
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self,
|
223
|
+
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, bytesize) ) != 0
|
225
224
|
FFI_Yajl::Encoder.raise_error_for_status(status, self)
|
226
225
|
end
|
227
226
|
end
|
@@ -229,7 +228,7 @@ end
|
|
229
228
|
|
230
229
|
class StringIO
|
231
230
|
def ffi_yajl(yajl_gen, state)
|
232
|
-
str =
|
231
|
+
str = read
|
233
232
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
234
233
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
235
234
|
end
|
@@ -238,7 +237,7 @@ end
|
|
238
237
|
|
239
238
|
class Date
|
240
239
|
def ffi_yajl(yajl_gen, state)
|
241
|
-
str =
|
240
|
+
str = to_s
|
242
241
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
243
242
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
244
243
|
end
|
@@ -247,16 +246,16 @@ end
|
|
247
246
|
|
248
247
|
class Time
|
249
248
|
def ffi_yajl(yajl_gen, state)
|
250
|
-
str =
|
249
|
+
str = strftime "%Y-%m-%d %H:%M:%S %z"
|
251
250
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
252
251
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
253
252
|
end
|
254
253
|
end
|
255
254
|
end
|
256
255
|
|
257
|
-
class DateTime
|
256
|
+
class DateTime
|
258
257
|
def ffi_yajl(yajl_gen, state)
|
259
|
-
str =
|
258
|
+
str = to_s
|
260
259
|
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
|
261
260
|
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
262
261
|
end
|
@@ -267,15 +266,13 @@ end
|
|
267
266
|
class Object
|
268
267
|
def ffi_yajl(yajl_gen, state)
|
269
268
|
if !state[:processing_key] && self.respond_to?(:to_json)
|
270
|
-
str =
|
269
|
+
str = to_json(state[:json_opts])
|
271
270
|
# #yajl_gen_number outputs a string without quotes around it
|
272
271
|
status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize)
|
273
272
|
else
|
274
|
-
str =
|
273
|
+
str = to_s
|
275
274
|
status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize)
|
276
275
|
end
|
277
|
-
if ( status ) != 0
|
278
|
-
FFI_Yajl::Encoder.raise_error_for_status(status, str)
|
279
|
-
end
|
276
|
+
FFI_Yajl::Encoder.raise_error_for_status(status, str) if ( status ) != 0
|
280
277
|
end
|
281
278
|
end
|
data/lib/ffi_yajl/ffi/parser.rb
CHANGED
@@ -20,41 +20,35 @@
|
|
20
20
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
|
23
|
-
require 'ffi_yajl/ffi'
|
24
|
-
|
25
23
|
module FFI_Yajl
|
26
24
|
module FFI
|
27
25
|
module Parser
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
raise FFI_Yajl::ParseError.new("repeated key: #{key}")
|
35
|
-
end
|
36
|
-
stack.last[key] = val
|
37
|
-
when Array
|
38
|
-
stack.last.push(val)
|
39
|
-
else
|
40
|
-
stack.push(val)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def stack_pop
|
45
|
-
if stack.length > 1
|
46
|
-
set_value( stack.pop )
|
26
|
+
def set_value(val) # rubocop:disable Style/AccessorMethodName
|
27
|
+
case stack.last
|
28
|
+
when Hash
|
29
|
+
raise FFI_Yajl::ParseError, "internal error: missing key in parse" if key.nil?
|
30
|
+
if @opts[:unique_key_checking] && stack.last.key?(key)
|
31
|
+
raise FFI_Yajl::ParseError, "repeated key: #{key}"
|
47
32
|
end
|
33
|
+
stack.last[key] = val
|
34
|
+
when Array
|
35
|
+
stack.last.push(val)
|
36
|
+
else
|
37
|
+
stack.push(val)
|
48
38
|
end
|
39
|
+
end
|
49
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
41
|
+
def stack_pop
|
42
|
+
set_value( stack.pop ) if stack.length > 1
|
43
|
+
end
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
45
|
+
def key_push
|
46
|
+
key_stack.push(key)
|
47
|
+
end
|
57
48
|
|
49
|
+
def key_pop
|
50
|
+
@key = key_stack.pop
|
51
|
+
end
|
58
52
|
|
59
53
|
def setup_callbacks
|
60
54
|
@null_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
|
@@ -70,7 +64,7 @@ module FFI_Yajl
|
|
70
64
|
1
|
71
65
|
end
|
72
66
|
@number_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t ]) do |ctx, stringval, stringlen|
|
73
|
-
s = stringval.slice(0,stringlen)
|
67
|
+
s = stringval.slice(0, stringlen)
|
74
68
|
s.force_encoding('UTF-8') if defined? Encoding
|
75
69
|
# XXX: I can't think of a better way to do this right now. need to call to_f if and only if its a float.
|
76
70
|
v = ( s =~ /[\.eE]/ ) ? s.to_f : s.to_i
|
@@ -82,18 +76,18 @@ module FFI_Yajl
|
|
82
76
|
1
|
83
77
|
end
|
84
78
|
@string_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, stringval, stringlen|
|
85
|
-
s = stringval.slice(0,stringlen)
|
79
|
+
s = stringval.slice(0, stringlen)
|
86
80
|
s.force_encoding('UTF-8') if defined? Encoding
|
87
81
|
set_value(s)
|
88
82
|
1
|
89
83
|
end
|
90
84
|
@start_map_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
|
91
85
|
key_push # for key => { } case, save the key
|
92
|
-
stack.push(
|
86
|
+
stack.push({})
|
93
87
|
1
|
94
88
|
end
|
95
89
|
@map_key_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, key, keylen|
|
96
|
-
s = key.slice(0,keylen)
|
90
|
+
s = key.slice(0, keylen)
|
97
91
|
s.force_encoding('UTF-8') if defined? Encoding
|
98
92
|
self.key = @opts[:symbolize_keys] ? s.to_sym : s
|
99
93
|
1
|
@@ -105,7 +99,7 @@ module FFI_Yajl
|
|
105
99
|
end
|
106
100
|
@start_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
|
107
101
|
key_push # for key => [ ] case, save the key
|
108
|
-
stack.push(
|
102
|
+
stack.push([])
|
109
103
|
1
|
110
104
|
end
|
111
105
|
@end_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
|
@@ -115,7 +109,6 @@ module FFI_Yajl
|
|
115
109
|
end
|
116
110
|
end
|
117
111
|
|
118
|
-
|
119
112
|
def do_yajl_parse(str, yajl_opts = {})
|
120
113
|
setup_callbacks
|
121
114
|
callback_ptr = ::FFI::MemoryPointer.new(::FFI_Yajl::YajlCallbacks)
|
@@ -150,15 +143,15 @@ module FFI_Yajl
|
|
150
143
|
::FFI_Yajl.yajl_config(yajl_handle, :yajl_allow_partial_values, :int, 1)
|
151
144
|
end
|
152
145
|
|
153
|
-
if (
|
146
|
+
if ( ::FFI_Yajl.yajl_parse(yajl_handle, str, str.bytesize) != :yajl_status_ok )
|
154
147
|
# FIXME: dup the error and call yajl_free_error?
|
155
148
|
error = ::FFI_Yajl.yajl_get_error(yajl_handle, 1, str, str.bytesize)
|
156
|
-
raise ::FFI_Yajl::ParseError
|
149
|
+
raise ::FFI_Yajl::ParseError, error
|
157
150
|
end
|
158
|
-
if (
|
151
|
+
if ( ::FFI_Yajl.yajl_complete_parse(yajl_handle) != :yajl_status_ok )
|
159
152
|
# FIXME: dup the error and call yajl_free_error?
|
160
153
|
error = ::FFI_Yajl.yajl_get_error(yajl_handle, 1, str, str.bytesize)
|
161
|
-
raise ::FFI_Yajl::ParseError
|
154
|
+
raise ::FFI_Yajl::ParseError, error
|
162
155
|
end
|
163
156
|
stack.pop
|
164
157
|
ensure
|
data/lib/ffi_yajl/ffi.rb
CHANGED
@@ -42,16 +42,16 @@ module FFI_Yajl
|
|
42
42
|
|
43
43
|
class YajlCallbacks < ::FFI::Struct
|
44
44
|
layout :yajl_null, :pointer,
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
45
|
+
:yajl_boolean, :pointer,
|
46
|
+
:yajl_integer, :pointer,
|
47
|
+
:yajl_double, :pointer,
|
48
|
+
:yajl_number, :pointer,
|
49
|
+
:yajl_string, :pointer,
|
50
|
+
:yajl_start_map, :pointer,
|
51
|
+
:yajl_map_key, :pointer,
|
52
|
+
:yajl_end_map, :pointer,
|
53
|
+
:yajl_start_array, :pointer,
|
54
|
+
:yajl_end_array, :pointer
|
55
55
|
end
|
56
56
|
|
57
57
|
enum :yajl_status, [
|
@@ -61,30 +61,30 @@ module FFI_Yajl
|
|
61
61
|
:yajl_status_error,
|
62
62
|
]
|
63
63
|
|
64
|
-
# FFI::Enums are slow, should remove the rest
|
65
|
-
# enum :yajl_gen_status, [
|
66
|
-
# :yajl_gen_status_ok,
|
67
|
-
# :yajl_gen_keys_must_be_strings,
|
68
|
-
# :yajl_max_depth_exceeded,
|
69
|
-
# :yajl_gen_in_error_state,
|
70
|
-
# :yajl_gen_generation_complete,
|
71
|
-
# :yajl_gen_invalid_number,
|
72
|
-
# :yajl_gen_no_buf,
|
73
|
-
# ]
|
64
|
+
# FFI::Enums are slow, should remove the rest
|
65
|
+
# enum :yajl_gen_status, [
|
66
|
+
# :yajl_gen_status_ok,
|
67
|
+
# :yajl_gen_keys_must_be_strings,
|
68
|
+
# :yajl_max_depth_exceeded,
|
69
|
+
# :yajl_gen_in_error_state,
|
70
|
+
# :yajl_gen_generation_complete,
|
71
|
+
# :yajl_gen_invalid_number,
|
72
|
+
# :yajl_gen_no_buf,
|
73
|
+
# ]
|
74
74
|
|
75
75
|
enum :yajl_option, [
|
76
76
|
:yajl_allow_comments, 0x01,
|
77
77
|
:yajl_dont_validate_strings, 0x02,
|
78
78
|
:yajl_allow_trailing_garbage, 0x04,
|
79
79
|
:yajl_allow_multiple_values, 0x08,
|
80
|
-
:yajl_allow_partial_values, 0x10
|
80
|
+
:yajl_allow_partial_values, 0x10
|
81
81
|
]
|
82
82
|
|
83
83
|
enum :yajl_gen_option, [
|
84
84
|
:yajl_gen_beautify, 0x01,
|
85
85
|
:yajl_gen_indent_string, 0x02,
|
86
86
|
:yajl_gen_print_callback, 0x04,
|
87
|
-
:yajl_gen_validate_utf8, 0x08
|
87
|
+
:yajl_gen_validate_utf8, 0x08
|
88
88
|
]
|
89
89
|
|
90
90
|
typedef :pointer, :yajl_handle
|
@@ -132,10 +132,9 @@ module FFI_Yajl
|
|
132
132
|
attach_function :yajl_gen_array_open, [:yajl_gen], :int
|
133
133
|
attach_function :yajl_gen_array_close, [:yajl_gen], :int
|
134
134
|
# yajl_gen_status yajl_gen_get_buf (yajl_gen hand, const unsigned char **buf, unsigned int *len)
|
135
|
-
attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer
|
135
|
+
attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer, :pointer], :int
|
136
136
|
# void yajl_gen_clear (yajl_gen hand)
|
137
137
|
attach_function :yajl_gen_clear, [:yajl_gen], :void
|
138
|
-
|
139
138
|
end
|
140
139
|
|
141
140
|
require 'ffi_yajl/encoder'
|
data/lib/ffi_yajl/parser.rb
CHANGED
@@ -23,7 +23,8 @@
|
|
23
23
|
module FFI_Yajl
|
24
24
|
class ParseError < StandardError; end
|
25
25
|
class Parser
|
26
|
-
|
26
|
+
attr_writer :stack, :key_stack
|
27
|
+
attr_accessor :key, :finished
|
27
28
|
|
28
29
|
attr_accessor :opts
|
29
30
|
|
@@ -31,14 +32,14 @@ module FFI_Yajl
|
|
31
32
|
# stack used to build up our complex object
|
32
33
|
#
|
33
34
|
def stack
|
34
|
-
@stack ||=
|
35
|
+
@stack ||= []
|
35
36
|
end
|
36
37
|
|
37
38
|
#
|
38
39
|
# stack to keep track of keys as we create nested hashes
|
39
40
|
#
|
40
41
|
def key_stack
|
41
|
-
@key_stack ||=
|
42
|
+
@key_stack ||= []
|
42
43
|
end
|
43
44
|
|
44
45
|
def self.parse(obj, *args)
|
@@ -80,9 +81,7 @@ module FFI_Yajl
|
|
80
81
|
# XXX: bug-compat with ruby-yajl
|
81
82
|
return nil if str == ""
|
82
83
|
|
83
|
-
if str.respond_to?(:read)
|
84
|
-
str = str.read()
|
85
|
-
end
|
84
|
+
str = str.read if str.respond_to?(:read)
|
86
85
|
|
87
86
|
# call either the ext or ffi hook
|
88
87
|
do_yajl_parse(str, yajl_opts)
|
data/lib/ffi_yajl/version.rb
CHANGED
@@ -25,18 +25,17 @@ require 'spec_helper'
|
|
25
25
|
require 'date'
|
26
26
|
|
27
27
|
describe "FFI_Yajl::Encoder" do
|
28
|
-
|
29
28
|
let(:options) { {} }
|
30
29
|
|
31
30
|
let(:encoder) { FFI_Yajl::Encoder.new(options) }
|
32
31
|
|
33
|
-
it "encodes hashes in keys as strings", :
|
34
|
-
ruby = { {'a' => 'b'} => 2 }
|
32
|
+
it "encodes hashes in keys as strings", ruby_gte_193: true do
|
33
|
+
ruby = { { 'a' => 'b' } => 2 }
|
35
34
|
expect(encoder.encode(ruby)).to eq('{"{\"a\"=>\"b\"}":2}')
|
36
35
|
end
|
37
36
|
|
38
|
-
it "encodes arrays in keys as strings", :
|
39
|
-
ruby = { [0,1] => 2 }
|
37
|
+
it "encodes arrays in keys as strings", ruby_gte_193: true do
|
38
|
+
ruby = { [0, 1] => 2 }
|
40
39
|
expect(encoder.encode(ruby)).to eq('{"[0, 1]":2}')
|
41
40
|
end
|
42
41
|
|
@@ -66,14 +65,14 @@ describe "FFI_Yajl::Encoder" do
|
|
66
65
|
end
|
67
66
|
|
68
67
|
it "encodes bignums in keys as strings" do
|
69
|
-
ruby = {
|
68
|
+
ruby = { 12_345_678_901_234_567_890 => 2 }
|
70
69
|
expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}')
|
71
70
|
end
|
72
71
|
|
73
72
|
it "encodes objects in keys as strings" do
|
74
73
|
o = Object.new
|
75
74
|
ruby = { o => 2 }
|
76
|
-
expect(encoder.encode(ruby)).to eq(%
|
75
|
+
expect(encoder.encode(ruby)).to eq(%{{"#{o}":2}})
|
77
76
|
end
|
78
77
|
|
79
78
|
it "encodes an object in a key which has a #to_json method as strings" do
|
@@ -84,7 +83,7 @@ describe "FFI_Yajl::Encoder" do
|
|
84
83
|
end
|
85
84
|
o = Thing.new
|
86
85
|
ruby = { o => 2 }
|
87
|
-
expect(encoder.encode(ruby)).to eq(%
|
86
|
+
expect(encoder.encode(ruby)).to eq(%{{"#{o}":2}})
|
88
87
|
end
|
89
88
|
|
90
89
|
# XXX: 127 == YAJL_MAX_DEPTH hardcodedness, zero control for us, it isn't even a twiddleable #define
|
@@ -92,18 +91,18 @@ describe "FFI_Yajl::Encoder" do
|
|
92
91
|
root = []
|
93
92
|
a = root
|
94
93
|
127.times { |_| a << []; a = a[0] }
|
95
|
-
expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
|
94
|
+
expect { encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
|
96
95
|
end
|
97
96
|
|
98
97
|
it "raises an exception for deeply nested hashes" do
|
99
98
|
root = {}
|
100
99
|
a = root
|
101
|
-
127.times {|_| a["a"] = {}; a = a["a"] }
|
102
|
-
expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
|
100
|
+
127.times { |_| a["a"] = {}; a = a["a"] }
|
101
|
+
expect { encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
|
103
102
|
end
|
104
103
|
|
105
104
|
it "encodes symbols in keys as strings" do
|
106
|
-
ruby = { :
|
105
|
+
ruby = { thing: 1 }
|
107
106
|
expect(encoder.encode(ruby)).to eq('{"thing":1}')
|
108
107
|
end
|
109
108
|
|
@@ -113,7 +112,7 @@ describe "FFI_Yajl::Encoder" do
|
|
113
112
|
end
|
114
113
|
|
115
114
|
it "can encode 32-bit unsigned ints" do
|
116
|
-
ruby = { "gid"=>
|
115
|
+
ruby = { "gid" => 4_294_967_294 }
|
117
116
|
expect(encoder.encode(ruby)).to eq('{"gid":4294967294}')
|
118
117
|
end
|
119
118
|
|
@@ -128,7 +127,7 @@ describe "FFI_Yajl::Encoder" do
|
|
128
127
|
|
129
128
|
it "can encode Date objects" do
|
130
129
|
ruby = Date.parse('2001-02-03')
|
131
|
-
expect(encoder.encode(ruby)).to eq(
|
130
|
+
expect(encoder.encode(ruby)).to eq( '"2001-02-03"' )
|
132
131
|
end
|
133
132
|
|
134
133
|
it "can encode StringIOs" do
|
@@ -148,13 +147,13 @@ describe "FFI_Yajl::Encoder" do
|
|
148
147
|
|
149
148
|
it "encodes them correctly" do
|
150
149
|
ruby = Time.local(2001, 02, 02, 21, 05, 06)
|
151
|
-
expect(encoder.encode(ruby)).to eq(
|
150
|
+
expect(encoder.encode(ruby)).to eq( '"2001-02-02 21:05:06 +0000"' )
|
152
151
|
end
|
153
152
|
end
|
154
153
|
|
155
154
|
it "can encode DateTime objects" do
|
156
155
|
ruby = DateTime.parse('2001-02-03T04:05:06.1+07:00')
|
157
|
-
expect(encoder.encode(ruby)).to eq(
|
156
|
+
expect(encoder.encode(ruby)).to eq( '"2001-02-03T04:05:06+07:00"' )
|
158
157
|
end
|
159
158
|
|
160
159
|
describe "testing .to_json for Objects" do
|
@@ -176,25 +175,25 @@ describe "FFI_Yajl::Encoder" do
|
|
176
175
|
|
177
176
|
context "when encoding invalid utf-8" do
|
178
177
|
ruby = {
|
179
|
-
"automatic"=>{
|
180
|
-
"etc"=>{
|
181
|
-
"passwd"=>{
|
182
|
-
"root"=>{"dir"=>"/root", "gid"=>0, "uid"=>0, "shell"=>"/bin/sh", "gecos"=>"Elan Ruusam\xc3\xa4e"},
|
183
|
-
"glen"=>{"dir"=>"/home/glen", "gid"=>500, "uid"=>500, "shell"=>"/bin/bash", "gecos"=>"Elan Ruusam\xE4e"},
|
184
|
-
}
|
178
|
+
"automatic" => {
|
179
|
+
"etc" => {
|
180
|
+
"passwd" => {
|
181
|
+
"root" => { "dir" => "/root", "gid" => 0, "uid" => 0, "shell" => "/bin/sh", "gecos" => "Elan Ruusam\xc3\xa4e" },
|
182
|
+
"glen" => { "dir" => "/home/glen", "gid" => 500, "uid" => 500, "shell" => "/bin/bash", "gecos" => "Elan Ruusam\xE4e" },
|
183
|
+
},
|
185
184
|
},
|
186
185
|
},
|
187
186
|
}
|
188
187
|
|
189
188
|
it "raises an error on invalid json" do
|
190
|
-
expect{ encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, /Invalid UTF-8 string 'Elan Ruusam.e': cannot encode to UTF-8/)
|
189
|
+
expect { encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, /Invalid UTF-8 string 'Elan Ruusam.e': cannot encode to UTF-8/)
|
191
190
|
end
|
192
191
|
|
193
192
|
context "when validate_utf8 is off" do
|
194
|
-
let(:options) { { :
|
193
|
+
let(:options) { { validate_utf8: false } }
|
195
194
|
|
196
195
|
it "does not raise an error" do
|
197
|
-
expect{ encoder.encode(ruby) }.not_to raise_error
|
196
|
+
expect { encoder.encode(ruby) }.not_to raise_error
|
198
197
|
end
|
199
198
|
|
200
199
|
it "returns utf8" do
|
@@ -47,7 +47,6 @@ describe "FFI_Yajl::MapLibraryName" do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
host_os_library_name_mapping.each do |host_os, library_names|
|
50
|
-
|
51
50
|
context "#library_names" do
|
52
51
|
it "maps #{host_os} correctly" do
|
53
52
|
allow(Test).to receive(:host_os).and_return(host_os)
|
@@ -112,6 +111,5 @@ describe "FFI_Yajl::MapLibraryName" do
|
|
112
111
|
Test.send(:ffi_open_yajl_library)
|
113
112
|
end
|
114
113
|
end
|
115
|
-
|
116
114
|
end
|
117
115
|
end
|