Module: MultiJson
- Extended by:
- AdapterSelector, Options
- Defined in:
- lib/multi_json.rb,
lib/multi_json/adapter.rb,
lib/multi_json/options.rb,
lib/multi_json/version.rb,
lib/multi_json/adapters/oj.rb,
lib/multi_json/parse_error.rb,
lib/multi_json/adapter_error.rb,
lib/multi_json/adapters/gson.rb,
lib/multi_json/adapters/yajl.rb,
lib/multi_json/options_cache.rb,
lib/multi_json/vendor/okjson.rb,
lib/multi_json/adapter_selector.rb,
lib/multi_json/adapters/ok_json.rb,
lib/multi_json/adapters/json_gem.rb,
lib/multi_json/adapters/oj_common.rb,
lib/multi_json/adapters/jr_jackson.rb,
lib/multi_json/convertible_hash_keys.rb,
lib/multi_json/adapters/fast_jsonparser.rb
Overview
A unified interface for JSON libraries in Ruby
MultiJson allows swapping between JSON backends without changing your code. It auto-detects available JSON libraries and uses the fastest one available.
Defined Under Namespace
Modules: AdapterSelector, Adapters, ConvertibleHashKeys, OkJson, Options, OptionsCache Classes: Adapter, AdapterError, ParseError, Version
Configuration collapse
- ALIASES =
Legacy alias for adapter name mappings
AdapterSelector::ALIASES
- REQUIREMENT_MAP =
Maps adapter symbols to their require paths for auto-loading
{ fast_jsonparser: "fast_jsonparser", oj: "oj", yajl: "yajl", jr_jackson: "jrjackson", json_gem: "json", gson: "gson" }.freeze
Constant Summary collapse
- VERSION =
Current version string in semver format
Version.to_s.freeze
- DecodeError =
Legacy aliases for backward compatibility
LoadError = ParseError
Constants included from AdapterSelector
Configuration collapse
-
.default_options ⇒ Hash
deprecated
private
Deprecated.
Use load_options or dump_options instead
-
.default_options=(value) ⇒ Hash
deprecated
private
Deprecated.
Use load_options= and dump_options= instead
Adapter Management collapse
-
.adapter ⇒ Class
(also: engine)
private
Returns the current adapter class.
-
.use(new_adapter) ⇒ Class
(also: adapter=, engine=)
private
Sets the adapter to use for JSON operations.
JSON Operations collapse
-
.current_adapter(options = {}) ⇒ Class
private
Returns the adapter to use for the given options.
-
.dump(object, options = {}) ⇒ String
(also: encode)
private
Serializes a Ruby object to a JSON string.
-
.load(string, options = {}) ⇒ Object
(also: decode)
private
Parses a JSON string into a Ruby object.
-
.with_adapter(new_adapter) { ... } ⇒ Object
(also: with_engine)
private
Executes a block using the specified adapter.
Methods included from Options
default_dump_options, default_load_options, dump_options, dump_options=, load_options, load_options=
Methods included from AdapterSelector
Class Method Details
.adapter ⇒ Class Also known as: engine
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the current adapter class
99 100 101 |
# File 'lib/multi_json.rb', line 99 def adapter @adapter ||= use(nil) end |
.current_adapter(options = {}) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the adapter to use for the given options
178 179 180 181 182 |
# File 'lib/multi_json.rb', line 178 def current_adapter( = {}) ||= {} adapter_override = [:adapter] adapter_override ? load_adapter(adapter_override) : adapter end |
.default_options ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use load_options or dump_options instead
Get the default options
52 53 54 55 56 |
# File 'lib/multi_json.rb', line 52 def Kernel.warn "MultiJson.default_options is deprecated\n" \ "Use MultiJson.load_options or MultiJson.dump_options instead" end |
.default_options=(value) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use load_options= and dump_options= instead
Set default options for both load and dump operations
39 40 41 42 43 |
# File 'lib/multi_json.rb', line 39 def (value) Kernel.warn "MultiJson.default_options setter is deprecated\n" \ "Use MultiJson.load_options and MultiJson.dump_options instead" self. = self. = value end |
.dump(object, options = {}) ⇒ String Also known as: encode
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serializes a Ruby object to a JSON string
192 193 194 |
# File 'lib/multi_json.rb', line 192 def dump(object, = {}) current_adapter().dump(object, ) end |
.load(string, options = {}) ⇒ Object Also known as: decode
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses a JSON string into a Ruby object
156 157 158 159 160 161 |
# File 'lib/multi_json.rb', line 156 def load(string, = {}) adapter_class = current_adapter() adapter_class.load(string, ) rescue adapter_class::ParseError => e raise ParseError.build(e, string) end |
.use(new_adapter) ⇒ Class Also known as: adapter=, engine=
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the adapter to use for JSON operations
119 120 121 122 123 |
# File 'lib/multi_json.rb', line 119 def use(new_adapter) @adapter = load_adapter(new_adapter) ensure OptionsCache.reset end |
.with_adapter(new_adapter) { ... } ⇒ Object Also known as: with_engine
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Executes a block using the specified adapter
212 213 214 215 216 217 218 |
# File 'lib/multi_json.rb', line 212 def with_adapter(new_adapter) previous_adapter = adapter self.adapter = new_adapter yield ensure self.adapter = previous_adapter end |