Skip to content

Commit

Permalink
Merge 2943345 into 2163770
Browse files Browse the repository at this point in the history
  • Loading branch information
guyboertje committed Mar 31, 2013
2 parents 2163770 + 2943345 commit 4a4e8df
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

platforms :jruby do
gem 'gson', '>= 0.6', :require => nil
gem 'jrjackson', :require => nil
gem 'jrjackson', '~> 0.1.0', :require => nil
end

group :development do
Expand Down
3 changes: 2 additions & 1 deletion lib/multi_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def default_options=(value)
['yajl', :yajl],
['json/ext', :json_gem],
['gson', :gson],
['jrjackson_r', :jr_jackson],
['jrjackson', :jr_jackson],
['json/pure', :json_pure]
]

Expand All @@ -48,6 +48,7 @@ def default_adapter
return :oj if defined?(::Oj)
return :yajl if defined?(::Yajl)
return :json_gem if defined?(::JSON)
return :jr_jackson if defined?(::JrJackson)
return :gson if defined?(::Gson)

REQUIREMENT_MAP.each do |(library, adapter)|
Expand Down
2 changes: 1 addition & 1 deletion lib/multi_json/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def collect_options(method, overrides, args)

end
end
end
end
17 changes: 7 additions & 10 deletions lib/multi_json/adapters/jr_jackson.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
require 'jrjackson_r' unless defined?(::JrJackson)
require 'jrjackson' unless defined?(::JrJackson)
require 'multi_json/adapter'
require 'multi_json/convertible_hash_keys'

module MultiJson
module Adapters
# Use the jrjackson.rb library to dump/load.
class JrJackson < Adapter
include ConvertibleHashKeys
ParseError = ::Java::OrgCodehausJackson::JsonParseException
ParseError = ::JrJackson::ParseError

def load(string, options={})
string = string.read if string.respond_to?(:read)
result = ::JrJackson::Json.parse(string)
options[:symbolize_keys] ? symbolize_keys(result) : result
def load(string, options={}) #:nodoc:
::JrJackson::Json.load(string, options)
end

def dump(object, options={})
::JrJackson::Json.generate(stringify_keys(object))
def dump(object, options={}) #:nodoc:
::JrJackson::Json.dump(object)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/multi_json/adapters/json_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def load(string, options={})

if string.respond_to?(:force_encoding)
string = string.dup.force_encoding(::Encoding::ASCII_8BIT)
end
end

options[:symbolize_names] = true if options.delete(:symbolize_keys)
::JSON.parse(string, options)
Expand All @@ -19,7 +19,7 @@ def load(string, options={})
def dump(object, options={})
options.merge!(::JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
object.to_json(options)
end
end
end
end
end
3 changes: 2 additions & 1 deletion spec/adapter_shared_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def to_json(*)

it 'stringifys symbol keys when encoding' do
dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
expect(MultiJson.load(dumped_json)).to eq({'a' => 1, 'b' => {'c' => 2}})
loaded_json = MultiJson.load(dumped_json)
expect(loaded_json).to eq({'a' => 1, 'b' => {'c' => 2}})
end

it 'properly loads valid JSON in StringIOs' do
Expand Down
2 changes: 1 addition & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def macruby?

def jruby?
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
end
end
7 changes: 5 additions & 2 deletions spec/multi_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
@old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
@old_gson = Object.const_get :Gson if Object.const_defined?(:Gson)
@old_jrjackson = Object.const_get :JrJackson if Object.const_defined?(:JrJackson)

MultiJson::REQUIREMENT_MAP.each_with_index do |(library, adapter), index|
MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", adapter]
end
Object.send :remove_const, :JSON if @old_json
Object.send :remove_const, :Oj if @old_oj
Object.send :remove_const, :Yajl if @old_yajl
Object.send :remove_const, :Gson if @old_gson
Object.send :remove_const, :JrJackson if @old_jrjackson
end

after do
Expand All @@ -32,6 +35,7 @@
Object.const_set :Oj, @old_oj if @old_oj
Object.const_set :Yajl, @old_yajl if @old_yajl
Object.const_set :Gson, @old_gson if @old_gson
Object.const_set :JrJackson, @old_jrjackson if @old_jrjackson
end

it 'defaults to ok_json if no other json implementions are available' do
Expand Down Expand Up @@ -165,12 +169,11 @@
end

it_behaves_like 'has options', MultiJson

%w(gson jr_jackson json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
next if !jruby? && %w(gson jr_jackson).include?(adapter)
next if !macruby? && adapter == 'nsjsonserialization'
next if jruby? && %w(oj yajl).include?(adapter)

context adapter do
it_behaves_like 'an adapter', adapter
end
Expand Down

0 comments on commit 4a4e8df

Please sign in to comment.