summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-07-09 10:40:37 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2015-07-09 10:40:37 -0700
commit8563dd4bb03a12bc41a7bf9b3e1ab4865a2a0110 (patch)
tree3548b0d9446ae45e68894129330c090172c1bffa /lib
parent6d427a893b28ee71ddcdbb3a6cd64cd01c7a690f (diff)
parentba9c90591239f0dbc082df70d9785e082a737e29 (diff)
downloadffi-yajl-8563dd4bb03a12bc41a7bf9b3e1ab4865a2a0110.tar.gz
Merge pull request #66 from chef/lcg/rubocop
Lcg/rubocop
Diffstat (limited to 'lib')
-rw-r--r--lib/ffi_yajl/benchmark.rb1
-rw-r--r--lib/ffi_yajl/benchmark/encode.rb19
-rw-r--r--lib/ffi_yajl/benchmark/encode_profile.rb2
-rw-r--r--lib/ffi_yajl/benchmark/http.rb4
-rw-r--r--lib/ffi_yajl/benchmark/parse.rb23
-rw-r--r--lib/ffi_yajl/benchmark/parse_json_and_marshal.rb4
-rw-r--r--lib/ffi_yajl/benchmark/parse_json_and_yaml.rb4
-rw-r--r--lib/ffi_yajl/benchmark/parse_profile.rb2
-rw-r--r--lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb2
-rw-r--r--lib/ffi_yajl/benchmark/parse_stream.rb4
-rw-r--r--lib/ffi_yajl/encoder.rb2
-rw-r--r--lib/ffi_yajl/ffi.rb47
-rw-r--r--lib/ffi_yajl/ffi/encoder.rb45
-rw-r--r--lib/ffi_yajl/ffi/parser.rb57
-rw-r--r--lib/ffi_yajl/map_library_name.rb1
-rw-r--r--lib/ffi_yajl/parser.rb8
16 files changed, 100 insertions, 125 deletions
diff --git a/lib/ffi_yajl/benchmark.rb b/lib/ffi_yajl/benchmark.rb
index 6d50c0f..e8944ca 100644
--- a/lib/ffi_yajl/benchmark.rb
+++ b/lib/ffi_yajl/benchmark.rb
@@ -25,4 +25,3 @@ require 'ffi_yajl/benchmark/encode_profile.rb'
require 'ffi_yajl/benchmark/parse.rb'
require 'ffi_yajl/benchmark/parse_profile.rb'
require 'ffi_yajl/benchmark/parse_profile_ruby_prof.rb'
-
diff --git a/lib/ffi_yajl/benchmark/encode.rb b/lib/ffi_yajl/benchmark/encode.rb
index d3f969c..eda4fe7 100644
--- a/lib/ffi_yajl/benchmark/encode.rb
+++ b/lib/ffi_yajl/benchmark/encode.rb
@@ -42,16 +42,14 @@ end
module FFI_Yajl
class Benchmark
class Encode
-
def run
- #filename = ARGV[0] || 'benchmark/subjects/ohai.json'
+ # filename = ARGV[0] || 'benchmark/subjects/ohai.json'
filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
hash = File.open(filename, 'rb') { |f| FFI_Yajl::Parser.parse(f.read) }
times = ARGV[1] ? ARGV[1].to_i : 1000
puts "Starting benchmark encoding #{filename} #{times} times\n\n"
::Benchmark.bmbm { |x|
-
x.report("FFI_Yajl::Encoder.encode (to a String)") {
times.times {
output = FFI_Yajl::Encoder.encode(hash)
@@ -125,16 +123,15 @@ module FFI_Yajl
}
end
end
-# if defined?(ActiveSupport::JSON)
-# x.report("ActiveSupport::JSON.encode") {
-# times.times {
-# ActiveSupport::JSON.encode(hash)
-# }
-# }
-# end
+ # if defined?(ActiveSupport::JSON)
+ # x.report("ActiveSupport::JSON.encode") {
+ # times.times {
+ # ActiveSupport::JSON.encode(hash)
+ # }
+ # }
+ # end
}
end
end
end
-
end
diff --git a/lib/ffi_yajl/benchmark/encode_profile.rb b/lib/ffi_yajl/benchmark/encode_profile.rb
index ce98a3c..f7ac7c2 100644
--- a/lib/ffi_yajl/benchmark/encode_profile.rb
+++ b/lib/ffi_yajl/benchmark/encode_profile.rb
@@ -14,7 +14,6 @@ ENV['CPUPROFILE_FREQUENCY'] = "4000"
module FFI_Yajl
class Benchmark
class EncodeProfile
-
def run
if defined?(PerfTools)
filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
@@ -32,7 +31,6 @@ module FFI_Yajl
system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out")
end
end
-
end
end
end
diff --git a/lib/ffi_yajl/benchmark/http.rb b/lib/ffi_yajl/benchmark/http.rb
index 551c987..4c61df5 100644
--- a/lib/ffi_yajl/benchmark/http.rb
+++ b/lib/ffi_yajl/benchmark/http.rb
@@ -15,7 +15,7 @@ uri = URI.parse('http://search.twitter.com/search.json?q=github')
# uri = URI.parse('http://localhost/yajl-ruby.git/benchmark/subjects/contacts.json')
times = ARGV[0] ? ARGV[0].to_i : 1
-puts "Starting benchmark parsing #{uri.to_s} #{times} times\n\n"
+puts "Starting benchmark parsing #{uri} #{times} times\n\n"
Benchmark.bmbm { |x|
x.report {
puts "Yajl::HttpStream.get"
@@ -29,4 +29,4 @@ Benchmark.bmbm { |x|
JSON.parse(Net::HTTP.get_response(uri).body, :max_nesting => false)
}
}
-} \ No newline at end of file
+}
diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb
index 02ad740..7e1f3a6 100644
--- a/lib/ffi_yajl/benchmark/parse.rb
+++ b/lib/ffi_yajl/benchmark/parse.rb
@@ -34,7 +34,6 @@ rescue LoadError
end
class FFI_Yajl::Benchmark::Parse
-
def run
filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json"))
json = File.new(filename, 'r')
@@ -49,14 +48,14 @@ class FFI_Yajl::Benchmark::Parse
FFI_Yajl::Parser.parse(json_str)
}
}
-# ffi_parser = FFI_Yajl::Parser.new
-# x.report {
-# puts "FFI_Yajl::Parser#parse (from a String)"
-# times.times {
-# json.rewind
-# ffi_parser.parse(json.read)
-# }
-# }
+ # ffi_parser = FFI_Yajl::Parser.new
+ # x.report {
+ # puts "FFI_Yajl::Parser#parse (from a String)"
+ # times.times {
+ # json.rewind
+ # ffi_parser.parse(json.read)
+ # }
+ # }
if defined?(Yajl::Parser)
x.report {
puts "Yajl::Parser.parse (from a String)"
@@ -65,7 +64,7 @@ class FFI_Yajl::Benchmark::Parse
}
}
io_parser = Yajl::Parser.new
- io_parser.on_parse_complete = lambda {|obj|} if times > 1
+ io_parser.on_parse_complete = lambda { |obj| } if times > 1
x.report {
puts "Yajl::Parser#parse (from an IO)"
times.times {
@@ -74,7 +73,7 @@ class FFI_Yajl::Benchmark::Parse
}
}
string_parser = Yajl::Parser.new
- string_parser.on_parse_complete = lambda {|obj|} if times > 1
+ string_parser.on_parse_complete = lambda { |obj| } if times > 1
x.report {
puts "Yajl::Parser#parse (from a String)"
times.times {
@@ -140,7 +139,5 @@ class FFI_Yajl::Benchmark::Parse
end
}
json.close
-
end
end
-
diff --git a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb
index 06fe70b..163f559 100644
--- a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb
+++ b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb
@@ -23,7 +23,7 @@ Benchmark.bmbm { |x|
x.report {
puts "Yajl::Parser#parse"
yajl = Yajl::Parser.new
- yajl.on_parse_complete = lambda {|obj|} if times > 1
+ yajl.on_parse_complete = lambda { |obj| } if times > 1
times.times {
json.rewind
hash = yajl.parse(json)
@@ -47,4 +47,4 @@ Benchmark.bmbm { |x|
}
}
json.close
-marshal_file.close \ No newline at end of file
+marshal_file.close
diff --git a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb
index 0cc509a..4e96e02 100644
--- a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb
+++ b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb
@@ -18,7 +18,7 @@ times = ARGV[0] ? ARGV[0].to_i : 1000
puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
Benchmark.bmbm { |x|
parser = Yajl::Parser.new
- parser.on_parse_complete = lambda {|obj|} if times > 1
+ parser.on_parse_complete = lambda { |obj| } if times > 1
x.report {
puts "Yajl::Parser#parse"
times.times {
@@ -52,4 +52,4 @@ Benchmark.bmbm { |x|
}
}
}
-yaml.close \ No newline at end of file
+yaml.close
diff --git a/lib/ffi_yajl/benchmark/parse_profile.rb b/lib/ffi_yajl/benchmark/parse_profile.rb
index 5efb1e2..39343bc 100644
--- a/lib/ffi_yajl/benchmark/parse_profile.rb
+++ b/lib/ffi_yajl/benchmark/parse_profile.rb
@@ -14,7 +14,6 @@ ENV['CPUPROFILE_FREQUENCY'] = "4000"
module FFI_Yajl
class Benchmark
class ParseProfile
-
def run
if defined?(PerfTools)
filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
@@ -31,7 +30,6 @@ module FFI_Yajl
system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out")
end
end
-
end
end
end
diff --git a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb
index 5b9f73e..8d9dd5d 100644
--- a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb
+++ b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb
@@ -7,7 +7,6 @@ require 'ffi_yajl'
module FFI_Yajl
class Benchmark
class ParseProfileRubyProf
-
def run
begin
require 'ruby-prof'
@@ -33,7 +32,6 @@ module FFI_Yajl
end
end
-
end
end
end
diff --git a/lib/ffi_yajl/benchmark/parse_stream.rb b/lib/ffi_yajl/benchmark/parse_stream.rb
index 94c5ac3..955f22f 100644
--- a/lib/ffi_yajl/benchmark/parse_stream.rb
+++ b/lib/ffi_yajl/benchmark/parse_stream.rb
@@ -20,7 +20,7 @@ times = ARGV[0] ? ARGV[0].to_i : 100
puts "Starting benchmark parsing JSON stream (#{File.size(filename)} bytes of JSON data with 430 JSON separate strings) #{times} times\n\n"
Benchmark.bmbm { |x|
parser = Yajl::Parser.new
- parser.on_parse_complete = lambda {|obj|}
+ parser.on_parse_complete = lambda { |obj| }
x.report {
puts "Yajl::Parser#parse"
times.times {
@@ -51,4 +51,4 @@ Benchmark.bmbm { |x|
}
end
}
-json.close \ No newline at end of file
+json.close
diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb
index a678221..9f2e564 100644
--- a/lib/ffi_yajl/encoder.rb
+++ b/lib/ffi_yajl/encoder.rb
@@ -54,7 +54,7 @@ module FFI_Yajl
@opts ||= {}
end
- def self.raise_error_for_status(status, token=nil)
+ def self.raise_error_for_status(status, token = nil)
# scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token
token = token.to_s.encode("utf-8", "binary", :undef => :replace)
case status
diff --git a/lib/ffi_yajl/ffi.rb b/lib/ffi_yajl/ffi.rb
index c56ea10..3b83bae 100644
--- a/lib/ffi_yajl/ffi.rb
+++ b/lib/ffi_yajl/ffi.rb
@@ -42,16 +42,16 @@ module FFI_Yajl
class YajlCallbacks < ::FFI::Struct
layout :yajl_null, :pointer,
- :yajl_boolean, :pointer,
- :yajl_integer, :pointer,
- :yajl_double, :pointer,
- :yajl_number, :pointer,
- :yajl_string, :pointer,
- :yajl_start_map, :pointer,
- :yajl_map_key, :pointer,
- :yajl_end_map, :pointer,
- :yajl_start_array, :pointer,
- :yajl_end_array, :pointer
+ :yajl_boolean, :pointer,
+ :yajl_integer, :pointer,
+ :yajl_double, :pointer,
+ :yajl_number, :pointer,
+ :yajl_string, :pointer,
+ :yajl_start_map, :pointer,
+ :yajl_map_key, :pointer,
+ :yajl_end_map, :pointer,
+ :yajl_start_array, :pointer,
+ :yajl_end_array, :pointer
end
enum :yajl_status, [
@@ -61,30 +61,30 @@ module FFI_Yajl
:yajl_status_error,
]
-# FFI::Enums are slow, should remove the rest
-# enum :yajl_gen_status, [
-# :yajl_gen_status_ok,
-# :yajl_gen_keys_must_be_strings,
-# :yajl_max_depth_exceeded,
-# :yajl_gen_in_error_state,
-# :yajl_gen_generation_complete,
-# :yajl_gen_invalid_number,
-# :yajl_gen_no_buf,
-# ]
+ # FFI::Enums are slow, should remove the rest
+ # enum :yajl_gen_status, [
+ # :yajl_gen_status_ok,
+ # :yajl_gen_keys_must_be_strings,
+ # :yajl_max_depth_exceeded,
+ # :yajl_gen_in_error_state,
+ # :yajl_gen_generation_complete,
+ # :yajl_gen_invalid_number,
+ # :yajl_gen_no_buf,
+ # ]
enum :yajl_option, [
:yajl_allow_comments, 0x01,
:yajl_dont_validate_strings, 0x02,
:yajl_allow_trailing_garbage, 0x04,
:yajl_allow_multiple_values, 0x08,
- :yajl_allow_partial_values, 0x10,
+ :yajl_allow_partial_values, 0x10
]
enum :yajl_gen_option, [
:yajl_gen_beautify, 0x01,
:yajl_gen_indent_string, 0x02,
:yajl_gen_print_callback, 0x04,
- :yajl_gen_validate_utf8, 0x08,
+ :yajl_gen_validate_utf8, 0x08
]
typedef :pointer, :yajl_handle
@@ -132,10 +132,9 @@ module FFI_Yajl
attach_function :yajl_gen_array_open, [:yajl_gen], :int
attach_function :yajl_gen_array_close, [:yajl_gen], :int
# yajl_gen_status yajl_gen_get_buf (yajl_gen hand, const unsigned char **buf, unsigned int *len)
- attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer ,:pointer], :int
+ attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer, :pointer], :int
# void yajl_gen_clear (yajl_gen hand)
attach_function :yajl_gen_clear, [:yajl_gen], :void
-
end
require 'ffi_yajl/encoder'
diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb
index 71b631e..250ed68 100644
--- a/lib/ffi_yajl/ffi/encoder.rb
+++ b/lib/ffi_yajl/ffi/encoder.rb
@@ -27,7 +27,7 @@ module FFI_Yajl
module FFI
module Encoder
def do_yajl_encode(obj, yajl_gen_opts, opts)
- yajl_gen = FFI_Yajl.yajl_gen_alloc(nil);
+ yajl_gen = FFI_Yajl.yajl_gen_alloc(nil)
# configure the yajl encoder
if yajl_gen_opts[:yajl_gen_beautify]
@@ -58,9 +58,8 @@ module FFI_Yajl
FFI_Yajl.yajl_gen_free(yajl_gen)
- return string
+ string
end
-
end
end
end
@@ -68,7 +67,7 @@ end
class Hash
def ffi_yajl(yajl_gen, state)
if state[:processing_key]
- str = self.to_s
+ str = to_s
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -76,7 +75,7 @@ class Hash
if ( status = FFI_Yajl.yajl_gen_map_open(yajl_gen) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, '{')
end
- self.each do |key, value|
+ each do |key, value|
# Perf Fix: mutate state hash rather than creating new copy
state[:processing_key] = true
key.ffi_yajl(yajl_gen, state)
@@ -93,7 +92,7 @@ end
class Array
def ffi_yajl(yajl_gen, state)
if state[:processing_key]
- str = self.to_s
+ str = to_s
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -101,7 +100,7 @@ class Array
if ( status = FFI_Yajl.yajl_gen_array_open(yajl_gen) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, '[')
end
- self.each do |value|
+ each do |value|
value.ffi_yajl(yajl_gen, state)
end
if ( status = FFI_Yajl.yajl_gen_array_close(yajl_gen) ) != 0
@@ -113,7 +112,7 @@ end
class NilClass
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if state[:processing_key]
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
@@ -128,7 +127,7 @@ end
class TrueClass
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if state[:processing_key]
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
@@ -143,7 +142,7 @@ end
class FalseClass
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if state[:processing_key]
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
@@ -158,7 +157,7 @@ end
class Fixnum
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if str == "NaN" || str == "Infinity" || str == "-Infinity"
raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
end
@@ -176,7 +175,7 @@ end
class Bignum
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if str == "NaN" || str == "Infinity" || str == "-Infinity"
raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
end
@@ -194,7 +193,7 @@ end
class Float
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if str == "NaN" || str == "Infinity" || str == "-Infinity"
raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number")
end
@@ -212,7 +211,7 @@ end
class Symbol
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -221,7 +220,7 @@ end
class String
def ffi_yajl(yajl_gen, state)
- if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, self.bytesize) ) != 0
+ if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, self)
end
end
@@ -229,7 +228,7 @@ end
class StringIO
def ffi_yajl(yajl_gen, state)
- str = self.read
+ str = read
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -238,7 +237,7 @@ end
class Date
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -247,7 +246,7 @@ end
class Time
def ffi_yajl(yajl_gen, state)
- str = self.strftime "%Y-%m-%d %H:%M:%S %z"
+ str = strftime "%Y-%m-%d %H:%M:%S %z"
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -256,7 +255,7 @@ end
class DateTime
def ffi_yajl(yajl_gen, state)
- str = self.to_s
+ str = to_s
if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status, str)
end
@@ -267,15 +266,13 @@ end
class Object
def ffi_yajl(yajl_gen, state)
if !state[:processing_key] && self.respond_to?(:to_json)
- str = self.to_json(state[:json_opts])
+ str = to_json(state[:json_opts])
# #yajl_gen_number outputs a string without quotes around it
status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize)
else
- str = self.to_s
+ str = to_s
status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize)
end
- if ( status ) != 0
- FFI_Yajl::Encoder.raise_error_for_status(status, str)
- end
+ FFI_Yajl::Encoder.raise_error_for_status(status, str) if ( status ) != 0
end
end
diff --git a/lib/ffi_yajl/ffi/parser.rb b/lib/ffi_yajl/ffi/parser.rb
index 77d2389..e3226c0 100644
--- a/lib/ffi_yajl/ffi/parser.rb
+++ b/lib/ffi_yajl/ffi/parser.rb
@@ -23,36 +23,32 @@
module FFI_Yajl
module FFI
module Parser
-
- def set_value(val)
- case stack.last
- when Hash
- raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil?
- if @opts[:unique_key_checking] && stack.last.has_key?(key)
- raise FFI_Yajl::ParseError.new("repeated key: #{key}")
- end
- stack.last[key] = val
- when Array
- stack.last.push(val)
- else
- stack.push(val)
- end
- end
-
- def stack_pop
- if stack.length > 1
- set_value( stack.pop )
+ def set_value(val)
+ case stack.last
+ when Hash
+ raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil?
+ if @opts[:unique_key_checking] && stack.last.key?(key)
+ raise FFI_Yajl::ParseError.new("repeated key: #{key}")
end
+ stack.last[key] = val
+ when Array
+ stack.last.push(val)
+ else
+ stack.push(val)
end
+ end
- def key_push
- key_stack.push(key)
- end
+ def stack_pop
+ set_value( stack.pop ) if stack.length > 1
+ end
- def key_pop
- @key = key_stack.pop()
- end
+ def key_push
+ key_stack.push(key)
+ end
+ def key_pop
+ @key = key_stack.pop
+ end
def setup_callbacks
@null_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
@@ -68,7 +64,7 @@ module FFI_Yajl
1
end
@number_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t ]) do |ctx, stringval, stringlen|
- s = stringval.slice(0,stringlen)
+ s = stringval.slice(0, stringlen)
s.force_encoding('UTF-8') if defined? Encoding
# 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.
v = ( s =~ /[\.eE]/ ) ? s.to_f : s.to_i
@@ -80,18 +76,18 @@ module FFI_Yajl
1
end
@string_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, stringval, stringlen|
- s = stringval.slice(0,stringlen)
+ s = stringval.slice(0, stringlen)
s.force_encoding('UTF-8') if defined? Encoding
set_value(s)
1
end
@start_map_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
key_push # for key => { } case, save the key
- stack.push(Hash.new)
+ stack.push({})
1
end
@map_key_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, key, keylen|
- s = key.slice(0,keylen)
+ s = key.slice(0, keylen)
s.force_encoding('UTF-8') if defined? Encoding
self.key = @opts[:symbolize_keys] ? s.to_sym : s
1
@@ -103,7 +99,7 @@ module FFI_Yajl
end
@start_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
key_push # for key => [ ] case, save the key
- stack.push(Array.new)
+ stack.push([])
1
end
@end_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx|
@@ -113,7 +109,6 @@ module FFI_Yajl
end
end
-
def do_yajl_parse(str, yajl_opts = {})
setup_callbacks
callback_ptr = ::FFI::MemoryPointer.new(::FFI_Yajl::YajlCallbacks)
diff --git a/lib/ffi_yajl/map_library_name.rb b/lib/ffi_yajl/map_library_name.rb
index 8723424..249116e 100644
--- a/lib/ffi_yajl/map_library_name.rb
+++ b/lib/ffi_yajl/map_library_name.rb
@@ -29,7 +29,6 @@ require 'libyajl2'
module FFI_Yajl
module MapLibraryName
-
private
# Stub for tests to override the host_os
diff --git a/lib/ffi_yajl/parser.rb b/lib/ffi_yajl/parser.rb
index 9008a3e..6029691 100644
--- a/lib/ffi_yajl/parser.rb
+++ b/lib/ffi_yajl/parser.rb
@@ -32,14 +32,14 @@ module FFI_Yajl
# stack used to build up our complex object
#
def stack
- @stack ||= Array.new
+ @stack ||= []
end
#
# stack to keep track of keys as we create nested hashes
#
def key_stack
- @key_stack ||= Array.new
+ @key_stack ||= []
end
def self.parse(obj, *args)
@@ -81,9 +81,7 @@ module FFI_Yajl
# XXX: bug-compat with ruby-yajl
return nil if str == ""
- if str.respond_to?(:read)
- str = str.read()
- end
+ str = str.read if str.respond_to?(:read)
# call either the ext or ffi hook
do_yajl_parse(str, yajl_opts)