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.3.2"
Class Method Summary collapse
-
.adapter ⇒ Object
Get the current adapter class.
- .current_adapter(options) ⇒ Object
-
.decode(string, options = {}) ⇒ Object
TODO: Remove for 2.0 release (but no sooner).
-
.default_adapter ⇒ Object
The default adapter based on what you currently have loaded and installed.
-
.default_engine ⇒ Object
TODO: Remove for 2.0 release (but no sooner).
-
.deprecate(raw_message) ⇒ Object
Sends of a deprecation warning.
-
.dump(object, options = {}) ⇒ Object
Encodes a Ruby object as JSON.
-
.encode(object, options = {}) ⇒ Object
TODO: Remove for 2.0 release (but no sooner).
-
.engine ⇒ Object
TODO: Remove for 2.0 release (but no sooner).
-
.engine=(new_engine) ⇒ Object
TODO: Remove for 2.0 release (but no sooner).
-
.load(string, options = {}) ⇒ Object
Decode a JSON string into Ruby.
- .load_adapter(new_adapter) ⇒ Object
-
.use(new_adapter) ⇒ Object
(also: adapter=)
Set the JSON parser utilizing a symbol, string, or class.
Class Method Details
.adapter ⇒ Object
Get the current adapter class.
57 58 59 60 61 |
# File 'lib/multi_json.rb', line 57 def adapter return @adapter if @adapter self.use self.default_adapter @adapter end |
.current_adapter(options) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/multi_json.rb', line 116 def current_adapter() if new_adapter = ( || {}).delete(:adapter) load_adapter(new_adapter) else adapter end end |
.decode(string, options = {}) ⇒ Object
TODO: Remove for 2.0 release (but no sooner)
98 99 100 101 |
# File 'lib/multi_json.rb', line 98 def decode(string, ={}) deprecate("MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead.") self.load(string, ) end |
.default_adapter ⇒ Object
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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/multi_json.rb', line 32 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 |
.default_engine ⇒ Object
TODO: Remove for 2.0 release (but no sooner)
23 24 25 26 |
# File 'lib/multi_json.rb', line 23 def default_engine deprecate("MultiJson.default_engine is deprecated and will be removed in the next major version. Use MultiJson.default_adapter instead.") self.default_adapter end |
.deprecate(raw_message) ⇒ Object
Sends of a deprecation warning
137 138 139 140 141 142 143 144 |
# File 'lib/multi_json.rb', line 137 def deprecate() @messages ||= {} = "#{Kernel.caller[1]}: [DEPRECATION] #{}" unless @messages[] @messages[] = true Kernel.warn end end |
.dump(object, options = {}) ⇒ Object
Encodes a Ruby object as JSON.
131 132 133 134 |
# File 'lib/multi_json.rb', line 131 def dump(object, ={}) adapter = current_adapter() adapter.dump(object, ) end |
.encode(object, options = {}) ⇒ Object
TODO: Remove for 2.0 release (but no sooner)
125 126 127 128 |
# File 'lib/multi_json.rb', line 125 def encode(object, ={}) deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.") self.dump(object, ) end |
.engine ⇒ Object
TODO: Remove for 2.0 release (but no sooner)
51 52 53 54 |
# File 'lib/multi_json.rb', line 51 def engine deprecate("MultiJson.engine is deprecated and will be removed in the next major version. Use MultiJson.adapter instead.") self.adapter end |
.engine=(new_engine) ⇒ Object
TODO: Remove for 2.0 release (but no sooner)
64 65 66 67 |
# File 'lib/multi_json.rb', line 64 def engine=(new_engine) deprecate("MultiJson.engine= is deprecated and will be removed in the next major version. Use MultiJson.use instead.") self.use(new_engine) end |
.load(string, options = {}) ⇒ Object
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.
109 110 111 112 113 114 |
# File 'lib/multi_json.rb', line 109 def load(string, ={}) adapter = current_adapter() adapter.load(string, ) rescue adapter::ParseError => exception raise DecodeError.new(exception., exception.backtrace, string) end |
.load_adapter(new_adapter) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/multi_json.rb', line 83 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 nil 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=
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)
78 79 80 |
# File 'lib/multi_json.rb', line 78 def use(new_adapter) @adapter = load_adapter(new_adapter) end |