Module: MultiJson
- Defined in:
- lib/multi_json/vendor/okjson.rb,
lib/multi_json.rb,
lib/multi_json/version.rb,
lib/multi_json/adapters/oj.rb,
lib/multi_json/adapters/yajl.rb,
lib/multi_json/adapters/ok_json.rb,
lib/multi_json/adapters/json_gem.rb,
lib/multi_json/adapters/json_pure.rb,
lib/multi_json/adapters/json_common.rb,
lib/multi_json/adapters/nsjsonserialization.rb
Overview
Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Defined Under Namespace
Modules: Adapters, OkJson Classes: DecodeError
Constant Summary collapse
- REQUIREMENT_MAP =
[ ["oj", :oj], ["yajl", :yajl], ["json", :json_gem], ["json/pure", :json_pure] ]
- VERSION =
"1.5.0"
Class Method Summary collapse
-
.adapter ⇒ Object
(also: engine)
Get the current adapter class.
- .current_adapter(options) ⇒ Object
-
.default_adapter ⇒ Object
(also: default_engine)
The default adapter based on what you currently have loaded and installed.
-
.dump(object, options = {}) ⇒ Object
(also: encode)
Encodes a Ruby object as JSON.
-
.load(string, options = {}) ⇒ Object
(also: decode)
Decode a JSON string into Ruby.
- .load_adapter(new_adapter) ⇒ Object
-
.use(new_adapter) ⇒ Object
(also: adapter=, engine=)
Set the JSON parser utilizing a symbol, string, or class.
- .with_adapter(new_adapter) ⇒ Object (also: with_engine)
Class Method Details
.adapter ⇒ Object Also known as: engine
Get the current adapter class.
47 48 49 50 51 |
# File 'lib/multi_json.rb', line 47 def adapter return @adapter if @adapter self.use self.default_adapter @adapter end |
.current_adapter(options) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/multi_json.rb', line 104 def current_adapter() if new_adapter = ( || {}).delete(:adapter) load_adapter(new_adapter) else adapter end end |
.default_adapter ⇒ Object Also known as: default_engine
The default adapter based on what you currently have loaded and installed. First checks to see if any adapters are already loaded, then checks to see which are installed if none are loaded.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/multi_json.rb', line 26 def default_adapter return :oj if defined?(::Oj) return :yajl if defined?(::Yajl) return :json_gem if defined?(::JSON) REQUIREMENT_MAP.each do |(library, adapter)| begin require library return adapter rescue LoadError next end end Kernel.warn "[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance." :ok_json end |
.dump(object, options = {}) ⇒ Object Also known as: encode
Encodes a Ruby object as JSON.
113 114 115 116 |
# File 'lib/multi_json.rb', line 113 def dump(object, ={}) adapter = current_adapter() adapter.dump(object, ) end |
.load(string, options = {}) ⇒ Object Also known as: decode
Decode a JSON string into Ruby.
Options
:symbolize_keys
-
If true, will use symbols instead of strings for the keys.
:adapter
-
If set, the selected engine will be used just for the call.
93 94 95 96 97 98 99 100 |
# File 'lib/multi_json.rb', line 93 def load(string, ={}) adapter = current_adapter() begin adapter.load(string, ) rescue adapter::ParseError => exception raise DecodeError.new(exception., exception.backtrace, string) end end |
.load_adapter(new_adapter) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/multi_json.rb', line 71 def load_adapter(new_adapter) case new_adapter when String, Symbol require "multi_json/adapters/#{new_adapter}" MultiJson::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}") when NilClass, FalseClass default_adapter = self.default_adapter require "multi_json/adapters/#{default_adapter}" MultiJson::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}") when Class new_adapter else raise "Did not recognize your adapter specification. Please specify either a symbol or a class." end end |
.use(new_adapter) ⇒ Object Also known as: adapter=, engine=
Set the JSON parser utilizing a symbol, string, or class. Supported by default are:
-
:oj
-
:json_gem
-
:json_pure
-
:ok_json
-
:yajl
-
:nsjsonserialization
(MacRuby only)
64 65 66 |
# File 'lib/multi_json.rb', line 64 def use(new_adapter) @adapter = load_adapter(new_adapter) end |
.with_adapter(new_adapter) ⇒ Object Also known as: with_engine
120 121 122 123 124 125 |
# File 'lib/multi_json.rb', line 120 def with_adapter(new_adapter) old_adapter, self.adapter = adapter, new_adapter yield ensure self.adapter = old_adapter end |