Skip to content

Commit

Permalink
Fix detection for the json_gem adapter
Browse files Browse the repository at this point in the history
* JSON::JSON_LOADED is also set by json/pure and so it is not enough to
  detect if the JSON native extension is loaded.
* JSON::Ext is not enough either, because json_pure includes json/ext.rb.
* JSON::Ext::Parser is only defined if the native extension is loaded so
  that is reliable.
* Fixes #196
* See that issue for details.
  • Loading branch information
eregon committed Jun 30, 2020
1 parent 6686481 commit 401362d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/multi_json.rb
Expand Up @@ -47,7 +47,7 @@ def default_adapter
return :oj if defined?(::Oj)
return :yajl if defined?(::Yajl)
return :jr_jackson if defined?(::JrJackson)
return :json_gem if defined?(::JSON::JSON_LOADED)
return :json_gem if defined?(::JSON::Ext::Parser)
return :gson if defined?(::Gson)

REQUIREMENT_MAP.each do |adapter, library|
Expand Down
18 changes: 18 additions & 0 deletions spec/multi_json_spec.rb
Expand Up @@ -30,6 +30,24 @@
end
end

context 'when JSON pure is already loaded' do
it 'default_adapter tries to require each adapter in turn and does not assume :json_gem is already loaded' do
require 'json/pure'
expect(JSON::JSON_LOADED).to be_truthy

undefine_constants :Oj, :Yajl, :Gson, :JrJackson do
# simulate that the json_gem is not loaded
ext = defined?(JSON::Ext::Parser) ? JSON::Ext.send(:remove_const, :Parser) : nil
begin
expect(MultiJson).to receive(:require)
MultiJson.default_adapter
ensure
JSON::Ext::Parser = ext if ext
end
end
end
end

context 'caching' do
before { MultiJson.use adapter }
let(:adapter) { MultiJson::Adapters::JsonGem }
Expand Down

0 comments on commit 401362d

Please sign in to comment.