Skip to content


Subversion checkout URL

You can clone with
Download ZIP


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

merged 1 commit into from

2 participants


No description provided.


What's the use case of per-call JSON? I'm not aware of considerations that would vary within the same app...


we are using couch_potato and tire in our application. couch_potato uses JSON.create_id = 'ruby_class' to store the type of documents in the couch. tire on the other hand uses the ruby_class attribute in an incompatibile way to store index metadata. so currently we are swapping the json engine for specific calls like this:

    def state
      # WORKAROUND (ps) json-gem in combination with JSON.create_id = 'ruby_class' fails in here
      engine = MultiJson.engine
      MultiJson.engine = :yajl
      @response = Tire::Configuration.client.get("#{Tire::Configuration.url}/_cluster/state")
      MultiJson.engine = engine

i know that we could also use the Yajl::Parser directly, but i thought it would be neat to do it on a per call basis.


That makes sense, and an :engine option seems to be the right way to do it. Can you update your fork to be compatible with the latest HEAD?


hope i resolved the conflicts properly :]


@mbleigh do you still want to pull this?


Yes I do, can you fix it to latest once more? Sorry for the delay.


started from scratch on latest master

@mbleigh mbleigh merged commit 25b8deb into intridea:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2012
  1. @phoet
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 4 deletions.
  1. +19 −4 lib/multi_json.rb
  2. +15 −0 spec/multi_json_spec.rb
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
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
- 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, exception.backtrace, string)
+ 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)
15 spec/multi_json_spec.rb
@@ -59,6 +59,21 @@
MultiJson.use MockDecoder == 'MockDecoder'
+ 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
%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
Something went wrong with that request. Please try again.