Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to just use a parser on a per call basis #35

Merged
merged 1 commit into from Apr 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/multi_json.rb
Expand Up @@ -70,19 +70,23 @@ def engine=(new_engine)
# * <tt>:yajl</tt> # * <tt>:yajl</tt>
# * <tt>:nsjsonserialization</tt> (MacRuby only) # * <tt>:nsjsonserialization</tt> (MacRuby only)
def use(new_adapter) def use(new_adapter)
@adapter = load_adapter(new_adapter)
end
alias :adapter= :use

def load_adapter(new_adapter)
case new_adapter case new_adapter
when String, Symbol when String, Symbol
require "multi_json/adapters/#{new_adapter}" require "multi_json/adapters/#{new_adapter}"
@adapter = MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}") MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
when NilClass when NilClass
@adapter = nil nil
when Class when Class
@adapter = new_adapter new_adapter
else else
raise "Did not recognize your adapter specification. Please specify either a symbol or a class." raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
end end
end end
alias :adapter= :use


# TODO: Remove for 2.0 release (but no sooner) # TODO: Remove for 2.0 release (but no sooner)
def decode(string, options={}) def decode(string, options={})
Expand All @@ -95,12 +99,22 @@ def decode(string, options={})
# <b>Options</b> # <b>Options</b>
# #
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys. # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
# <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
def load(string, options={}) def load(string, options={})
adapter = current_adapter(options)
adapter.load(string, options) adapter.load(string, options)
rescue adapter::ParseError => exception rescue adapter::ParseError => exception
raise DecodeError.new(exception.message, exception.backtrace, string) raise DecodeError.new(exception.message, exception.backtrace, string)
end end


def current_adapter(options)
if new_adapter = (options || {}).delete(:adapter)
load_adapter(new_adapter)
else
adapter
end
end

# TODO: Remove for 2.0 release (but no sooner) # TODO: Remove for 2.0 release (but no sooner)
def encode(object, options={}) def encode(object, options={})
deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.") deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.")
Expand All @@ -109,6 +123,7 @@ def encode(object, options={})


# Encodes a Ruby object as JSON. # Encodes a Ruby object as JSON.
def dump(object, options={}) def dump(object, options={})
adapter = current_adapter(options)
adapter.dump(object, options) adapter.dump(object, options)
end end


Expand Down
15 changes: 15 additions & 0 deletions spec/multi_json_spec.rb
Expand Up @@ -59,6 +59,21 @@
MultiJson.use MockDecoder MultiJson.use MockDecoder
MultiJson.adapter.name.should == 'MockDecoder' MultiJson.adapter.name.should == 'MockDecoder'
end end

context "using one-shot parser" do
before(:each) do
require 'multi_json/adapters/json_pure'
MultiJson::Adapters::JsonPure.should_receive(:dump).exactly(1).times.and_return('dump_something')
MultiJson::Adapters::JsonPure.should_receive(:load).exactly(1).times.and_return('load_something')
end

it "should use the defined parser just for the call" do
MultiJson.use :yajl
MultiJson.dump('', :adapter => :json_pure).should eql('dump_something')
MultiJson.load('', :adapter => :json_pure).should eql('load_something')
MultiJson.adapter.to_s.should eql("MultiJson::Adapters::Yajl")
end
end
end end


%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter| %w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
Expand Down