multi_json 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/multi_json.rb CHANGED
@@ -19,6 +19,12 @@ module MultiJson
19
19
  ["json/pure", :json_pure]
20
20
  ]
21
21
 
22
+ # TODO: Remove for 2.0 release (but no sooner)
23
+ def default_engine
24
+ deprecate("MultiJson.default_engine is deprecated and will be removed in the next major version. Use MultiJson.default_adapter instead.")
25
+ self.default_adapter
26
+ end
27
+
22
28
  # The default adapter based on what you currently
23
29
  # have loaded and installed. First checks to see
24
30
  # if any adapters are already loaded, then checks
@@ -70,19 +76,23 @@ module MultiJson
70
76
  # * <tt>:yajl</tt>
71
77
  # * <tt>:nsjsonserialization</tt> (MacRuby only)
72
78
  def use(new_adapter)
79
+ @adapter = load_adapter(new_adapter)
80
+ end
81
+ alias :adapter= :use
82
+
83
+ def load_adapter(new_adapter)
73
84
  case new_adapter
74
85
  when String, Symbol
75
86
  require "multi_json/adapters/#{new_adapter}"
76
- @adapter = MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
87
+ MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
77
88
  when NilClass
78
- @adapter = nil
89
+ nil
79
90
  when Class
80
- @adapter = new_adapter
91
+ new_adapter
81
92
  else
82
93
  raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
83
94
  end
84
95
  end
85
- alias :adapter= :use
86
96
 
87
97
  # TODO: Remove for 2.0 release (but no sooner)
88
98
  def decode(string, options={})
@@ -95,12 +105,22 @@ module MultiJson
95
105
  # <b>Options</b>
96
106
  #
97
107
  # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
108
+ # <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
98
109
  def load(string, options={})
110
+ adapter = current_adapter(options)
99
111
  adapter.load(string, options)
100
112
  rescue adapter::ParseError => exception
101
113
  raise DecodeError.new(exception.message, exception.backtrace, string)
102
114
  end
103
115
 
116
+ def current_adapter(options)
117
+ if new_adapter = (options || {}).delete(:adapter)
118
+ load_adapter(new_adapter)
119
+ else
120
+ adapter
121
+ end
122
+ end
123
+
104
124
  # TODO: Remove for 2.0 release (but no sooner)
105
125
  def encode(object, options={})
106
126
  deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.")
@@ -109,6 +129,7 @@ module MultiJson
109
129
 
110
130
  # Encodes a Ruby object as JSON.
111
131
  def dump(object, options={})
132
+ adapter = current_adapter(options)
112
133
  adapter.dump(object, options)
113
134
  end
114
135
 
@@ -1,3 +1,3 @@
1
1
  module MultiJson
2
- VERSION = "1.3.1"
2
+ VERSION = "1.3.2"
3
3
  end
data/multi_json.gemspec CHANGED
@@ -25,8 +25,11 @@ Gem::Specification.new do |gem|
25
25
  MultiJson.engine is deprecated and will be removed in the next major version.
26
26
  Use MultiJson.adapter instead.
27
27
 
28
- MultiJson.engine= is deprecated and will be removed in the next major version.
29
- Use MultiJson.use instead.
28
+ MultiJson.engine= is deprecated and will be removed in the next major
29
+ version. Use MultiJson.use instead.
30
+
31
+ MultiJson.default_engine is deprecated and will be removed in the next major
32
+ version. Use MultiJson.default_adapter instead.
30
33
 
31
34
  ********************************************************************************
32
35
  eos
@@ -59,6 +59,21 @@ describe 'MultiJson' do
59
59
  MultiJson.use MockDecoder
60
60
  MultiJson.adapter.name.should == 'MockDecoder'
61
61
  end
62
+
63
+ context "using one-shot parser" do
64
+ before(:each) do
65
+ require 'multi_json/adapters/json_pure'
66
+ MultiJson::Adapters::JsonPure.should_receive(:dump).exactly(1).times.and_return('dump_something')
67
+ MultiJson::Adapters::JsonPure.should_receive(:load).exactly(1).times.and_return('load_something')
68
+ end
69
+
70
+ it "should use the defined parser just for the call" do
71
+ MultiJson.use :yajl
72
+ MultiJson.dump('', :adapter => :json_pure).should eql('dump_something')
73
+ MultiJson.load('', :adapter => :json_pure).should eql('load_something')
74
+ MultiJson.adapter.to_s.should eql("MultiJson::Adapters::Yajl")
75
+ end
76
+ end
62
77
  end
63
78
 
64
79
  %w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-04-16 00:00:00.000000000 Z
14
+ date: 2012-04-17 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -117,8 +117,10 @@ post_install_message: ! "*******************************************************
117
117
  \ Use MultiJson.dump instead.\n\n MultiJson.decode is deprecated and will be removed
118
118
  in the next major version.\n Use MultiJson.load instead.\n\n MultiJson.engine
119
119
  is deprecated and will be removed in the next major version.\n Use MultiJson.adapter
120
- instead.\n\n MultiJson.engine= is deprecated and will be removed in the next major
121
- version.\n Use MultiJson.use instead.\n\n********************************************************************************\n"
120
+ instead.\n\n MultiJson.engine= is deprecated and will be removed in the next major\n
121
+ \ version. Use MultiJson.use instead.\n\n MultiJson.default_engine is deprecated
122
+ and will be removed in the next major\n version. Use MultiJson.default_adapter
123
+ instead.\n\n********************************************************************************\n"
122
124
  rdoc_options:
123
125
  - --charset=UTF-8
124
126
  require_paths: