multi_json 1.3.3 → 1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/multi_json.rb +84 -82
- data/lib/multi_json/version.rb +1 -1
- data/spec/multi_json_spec.rb +2 -2
- metadata +5 -3
data/lib/multi_json.rb
CHANGED
@@ -8,8 +8,6 @@ module MultiJson
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
module_function
|
12
|
-
|
13
11
|
@adapter = nil
|
14
12
|
|
15
13
|
REQUIREMENT_MAP = [
|
@@ -19,98 +17,102 @@ module MultiJson
|
|
19
17
|
["json/pure", :json_pure]
|
20
18
|
]
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
20
|
+
class << self
|
21
|
+
|
22
|
+
# The default adapter based on what you currently
|
23
|
+
# have loaded and installed. First checks to see
|
24
|
+
# if any adapters are already loaded, then checks
|
25
|
+
# to see which are installed if none are loaded.
|
26
|
+
def default_adapter
|
27
|
+
return :oj if defined?(::Oj)
|
28
|
+
return :yajl if defined?(::Yajl)
|
29
|
+
return :json_gem if defined?(::JSON)
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
REQUIREMENT_MAP.each do |(library, adapter)|
|
32
|
+
begin
|
33
|
+
require library
|
34
|
+
return adapter
|
35
|
+
rescue LoadError
|
36
|
+
next
|
37
|
+
end
|
37
38
|
end
|
39
|
+
|
40
|
+
Kernel.warn "[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance."
|
41
|
+
:ok_json
|
38
42
|
end
|
43
|
+
# :nodoc:
|
44
|
+
alias :default_engine :default_adapter
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
46
|
+
# Get the current adapter class.
|
47
|
+
def adapter
|
48
|
+
return @adapter if @adapter
|
49
|
+
self.use self.default_adapter
|
50
|
+
@adapter
|
51
|
+
end
|
52
|
+
# :nodoc:
|
53
|
+
alias :engine :adapter
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
55
|
+
# Set the JSON parser utilizing a symbol, string, or class.
|
56
|
+
# Supported by default are:
|
57
|
+
#
|
58
|
+
# * <tt>:oj</tt>
|
59
|
+
# * <tt>:json_gem</tt>
|
60
|
+
# * <tt>:json_pure</tt>
|
61
|
+
# * <tt>:ok_json</tt>
|
62
|
+
# * <tt>:yajl</tt>
|
63
|
+
# * <tt>:nsjsonserialization</tt> (MacRuby only)
|
64
|
+
def use(new_adapter)
|
65
|
+
@adapter = load_adapter(new_adapter)
|
66
|
+
end
|
67
|
+
alias :adapter= :use
|
68
|
+
# :nodoc:
|
69
|
+
alias :engine= :use
|
54
70
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
# :nodoc:
|
69
|
-
alias :engine= :use
|
71
|
+
def load_adapter(new_adapter)
|
72
|
+
case new_adapter
|
73
|
+
when String, Symbol
|
74
|
+
require "multi_json/adapters/#{new_adapter}"
|
75
|
+
MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
76
|
+
when NilClass
|
77
|
+
nil
|
78
|
+
when Class
|
79
|
+
new_adapter
|
80
|
+
else
|
81
|
+
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
|
82
|
+
end
|
83
|
+
end
|
70
84
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
raise
|
85
|
+
# Decode a JSON string into Ruby.
|
86
|
+
#
|
87
|
+
# <b>Options</b>
|
88
|
+
#
|
89
|
+
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
|
90
|
+
# <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
|
91
|
+
def load(string, options={})
|
92
|
+
adapter = current_adapter(options)
|
93
|
+
adapter.load(string, options)
|
94
|
+
rescue adapter::ParseError => exception
|
95
|
+
raise DecodeError.new(exception.message, exception.backtrace, string)
|
82
96
|
end
|
83
|
-
|
97
|
+
# :nodoc:
|
98
|
+
alias :decode :load
|
84
99
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
adapter = current_adapter(options)
|
93
|
-
adapter.load(string, options)
|
94
|
-
rescue adapter::ParseError => exception
|
95
|
-
raise DecodeError.new(exception.message, exception.backtrace, string)
|
96
|
-
end
|
97
|
-
# :nodoc:
|
98
|
-
alias :decode :load
|
100
|
+
def current_adapter(options)
|
101
|
+
if new_adapter = (options || {}).delete(:adapter)
|
102
|
+
load_adapter(new_adapter)
|
103
|
+
else
|
104
|
+
adapter
|
105
|
+
end
|
106
|
+
end
|
99
107
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
adapter
|
108
|
+
# Encodes a Ruby object as JSON.
|
109
|
+
def dump(object, options={})
|
110
|
+
adapter = current_adapter(options)
|
111
|
+
adapter.dump(object, options)
|
105
112
|
end
|
106
|
-
|
113
|
+
# :nodoc:
|
114
|
+
alias :encode :dump
|
107
115
|
|
108
|
-
# Encodes a Ruby object as JSON.
|
109
|
-
def dump(object, options={})
|
110
|
-
adapter = current_adapter(options)
|
111
|
-
adapter.dump(object, options)
|
112
116
|
end
|
113
|
-
# :nodoc:
|
114
|
-
alias :encode :dump
|
115
117
|
|
116
118
|
end
|
data/lib/multi_json/version.rb
CHANGED
data/spec/multi_json_spec.rb
CHANGED
@@ -68,10 +68,10 @@ describe 'MultiJson' do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should use the defined parser just for the call" do
|
71
|
-
MultiJson.use :
|
71
|
+
MultiJson.use :json_gem
|
72
72
|
MultiJson.dump('', :adapter => :json_pure).should eql('dump_something')
|
73
73
|
MultiJson.load('', :adapter => :json_pure).should eql('load_something')
|
74
|
-
MultiJson.adapter.to_s.should eql("MultiJson::Adapters::
|
74
|
+
MultiJson.adapter.to_s.should eql("MultiJson::Adapters::JsonGem")
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
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.
|
4
|
+
version: 1.3.4
|
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-
|
14
|
+
date: 2012-04-29 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rake
|
@@ -123,6 +123,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
hash: 3342711038323069490
|
126
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
130
|
none: false
|
128
131
|
requirements:
|
@@ -139,4 +142,3 @@ test_files:
|
|
139
142
|
- spec/adapter_shared_example.rb
|
140
143
|
- spec/helper.rb
|
141
144
|
- spec/multi_json_spec.rb
|
142
|
-
has_rdoc:
|