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
- REQUIREMENT_MAP =
[ ["oj", :oj], ["yajl", :yajl], ["json", :json_gem], ["json/pure", :json_pure] ]
- VERSION =
"1.3.7"
Class Method Summary (collapse)
-
+ (Object) adapter
(also: engine)
Get the current adapter class.
- + (Object) current_adapter(options)
-
+ (Object) default_adapter
(also: default_engine)
The default adapter based on what you currently have loaded and installed.
-
+ (Object) dump(object, options = {})
(also: encode)
Encodes a Ruby object as JSON.
-
+ (Object) load(string, options = {})
(also: decode)
Decode a JSON string into Ruby.
- + (Object) load_adapter(new_adapter)
-
+ (Object) use(new_adapter)
(also: adapter=, engine=)
Set the JSON parser utilizing a symbol, string, or class.
Class Method Details
+ (Object) adapter 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 |
+ (Object) current_adapter(options)
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 |
+ (Object) default_adapter 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 |
+ (Object) dump(object, options = {}) 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 |
+ (Object) load(string, options = {}) 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 |
+ (Object) load_adapter(new_adapter)
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 |
+ (Object) use(new_adapter) 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 |