Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add possibility to use engines per call

  • Loading branch information...
commit 106bbec469d5d0a832bfa31fffcb8c0f0cdc9bd3 1 parent 419359e
@phoet phoet authored
Showing with 34 additions and 4 deletions.
  1. +19 −4 lib/multi_json.rb
  2. +15 −0 spec/multi_json_spec.rb
View
23 lib/multi_json.rb
@@ -70,19 +70,23 @@ def engine=(new_engine)
# * <tt>:yajl</tt>
# * <tt>:nsjsonserialization</tt> (MacRuby only)
def use(new_adapter)
+ @adapter = load_adapter(new_adapter)
+ end
+ alias :adapter= :use
+
+ def load_adapter(new_adapter)
case new_adapter
when String, Symbol
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
- @adapter = nil
+ nil
when Class
- @adapter = new_adapter
+ new_adapter
else
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
end
end
- alias :adapter= :use
# TODO: Remove for 2.0 release (but no sooner)
def decode(string, options={})
@@ -95,12 +99,22 @@ def decode(string, options={})
# <b>Options</b>
#
# <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={})
+ adapter = current_adapter(options)
adapter.load(string, options)
rescue adapter::ParseError => exception
raise DecodeError.new(exception.message, exception.backtrace, string)
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)
def encode(object, options={})
deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.")
@@ -109,6 +123,7 @@ def encode(object, options={})
# Encodes a Ruby object as JSON.
def dump(object, options={})
+ adapter = current_adapter(options)
adapter.dump(object, options)
end
View
15 spec/multi_json_spec.rb
@@ -59,6 +59,21 @@
MultiJson.use MockDecoder
MultiJson.adapter.name.should == 'MockDecoder'
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
%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
Please sign in to comment.
Something went wrong with that request. Please try again.