ffi-yajl 2.2.0-universal-java → 2.2.1-universal-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,37 +18,37 @@ json = File.new(filename, 'r')
18
18
 
19
19
  times = ARGV[0] ? ARGV[0].to_i : 100
20
20
  puts "Starting benchmark parsing JSON stream (#{File.size(filename)} bytes of JSON data with 430 JSON separate strings) #{times} times\n\n"
21
- Benchmark.bmbm { |x|
21
+ Benchmark.bmbm do |x|
22
22
  parser = Yajl::Parser.new
23
- parser.on_parse_complete = lambda {|obj|}
24
- x.report {
23
+ parser.on_parse_complete = ->(obj) {}
24
+ x.report do
25
25
  puts "Yajl::Parser#parse"
26
- times.times {
26
+ times.times do
27
27
  json.rewind
28
28
  parser.parse(json)
29
- }
30
- }
29
+ end
30
+ end
31
31
  if defined?(JSON)
32
- x.report {
32
+ x.report do
33
33
  puts "JSON.parse"
34
- times.times {
34
+ times.times do
35
35
  json.rewind
36
36
  while chunk = json.gets
37
- JSON.parse(chunk, :max_nesting => false)
37
+ JSON.parse(chunk, max_nesting: false)
38
38
  end
39
- }
40
- }
39
+ end
40
+ end
41
41
  end
42
42
  if defined?(ActiveSupport::JSON)
43
- x.report {
43
+ x.report do
44
44
  puts "ActiveSupport::JSON.decode"
45
- times.times {
45
+ times.times do
46
46
  json.rewind
47
47
  while chunk = json.gets
48
48
  ActiveSupport::JSON.decode(chunk)
49
49
  end
50
- }
51
- }
50
+ end
51
+ end
52
52
  end
53
- }
54
- json.close
53
+ end
54
+ json.close
@@ -41,7 +41,7 @@ module FFI_Yajl
41
41
  # call either the ext or ffi hook
42
42
  str = do_yajl_encode(obj, yajl_gen_opts, opts)
43
43
  # we can skip cleaning the whole string for utf-8 issues if we have yajl validate as we go
44
- str.encode!("utf-8", "binary", :undef => :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8]
44
+ str.encode!("utf-8", "binary", undef: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8]
45
45
  str
46
46
  end
47
47
 
@@ -54,9 +54,9 @@ module FFI_Yajl
54
54
  @opts ||= {}
55
55
  end
56
56
 
57
- def self.raise_error_for_status(status, token=nil)
57
+ def self.raise_error_for_status(status, token = nil)
58
58
  # scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token
59
- token = token.to_s.encode("utf-8", "binary", :undef => :replace)
59
+ token = token.to_s.encode("utf-8", "binary", undef: :replace)
60
60
  case status
61
61
  when 1 # yajl_gen_keys_must_be_strings
62
62
  raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key"
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
- :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
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 ,:pointer], :int
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'
@@ -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 'ffi_yajl/ffi'
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
- :json_opts => opts,
44
- :processing_key => false,
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
- return string
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 = self.to_s
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
- self.each do |key, value|
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 = self.to_s
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
- self.each do |value|
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 = self.to_s
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 = self.to_s
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 = self.to_s
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 = self.to_s
160
+ str = to_s
162
161
  if str == "NaN" || str == "Infinity" || str == "-Infinity"
163
- raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
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 = self.to_s
178
+ str = to_s
180
179
  if str == "NaN" || str == "Infinity" || str == "-Infinity"
181
- raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
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 = self.to_s
196
+ str = to_s
198
197
  if str == "NaN" || str == "Infinity" || str == "-Infinity"
199
- raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
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 = self.to_s
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, self.bytesize) ) != 0
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 = self.read
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 = self.to_s
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 = self.strftime "%Y-%m-%d %H:%M:%S %z"
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 < Date
256
+ class DateTime
258
257
  def ffi_yajl(yajl_gen, state)
259
- str = self.to_s
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 = self.to_json(state[:json_opts])
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 = self.to_s
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
@@ -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
- def set_value(val)
30
- case stack.last
31
- when Hash
32
- raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil?
33
- if @opts[:unique_key_checking] && stack.last.has_key?(key)
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
- def key_push
51
- key_stack.push(key)
52
- end
41
+ def stack_pop
42
+ set_value( stack.pop ) if stack.length > 1
43
+ end
53
44
 
54
- def key_pop
55
- @key = key_stack.pop()
56
- end
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(Hash.new)
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(Array.new)
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 ( stat = ::FFI_Yajl.yajl_parse(yajl_handle, str, str.bytesize) != :yajl_status_ok )
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.new(error)
149
+ raise ::FFI_Yajl::ParseError, error
157
150
  end
158
- if ( stat = FFI_Yajl.yajl_complete_parse(yajl_handle) != :yajl_status_ok )
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.new(error)
154
+ raise ::FFI_Yajl::ParseError, error
162
155
  end
163
156
  stack.pop
164
157
  ensure