module MultiJson
Constants
- ALIASES
- DecodeError
- REQUIREMENT_MAP
- VERSION
Public Instance Methods
Source
# File lib/multi_json.rb, line 60 def adapter return @adapter if defined?(@adapter) && @adapter use nil # load default adapter @adapter end
Get the current adapter class.
Also aliased as: engine
Source
# File lib/multi_json.rb, line 118 def current_adapter(options = {}) if (new_adapter = options[:adapter]) load_adapter(new_adapter) else adapter end end
Source
# File lib/multi_json.rb, line 42 def default_adapter adapter = loaded_adapter || installable_adapter return adapter if adapter @default_adapter_warning_shown ||= begin Kernel.warn( "[WARNING] MultiJson is using the default adapter (ok_json). " \ "We recommend loading a different JSON library to improve performance." ) true end :ok_json end
The default adapter based on what you currently have loaded and installed.
Also aliased as: default_engine
Source
# File lib/multi_json.rb, line 17 def default_options Kernel.warn "MultiJson.default_options is deprecated\nUse MultiJson.load_options or MultiJson.dump_options instead" load_options end
Source
# File lib/multi_json.rb, line 11 def default_options=(value) Kernel.warn "MultiJson.default_options setter is deprecated\nUse MultiJson.load_options and MultiJson.dump_options instead" self.load_options = self.dump_options = value end
Source
# File lib/multi_json.rb, line 127 def dump(object, options = {}) current_adapter(options).dump(object, options) end
Encodes a Ruby object as JSON.
Also aliased as: encode
Source
# File lib/multi_json.rb, line 108 def load(string, options = {}) adapter = current_adapter(options) begin adapter.load(string, options) rescue adapter::ParseError => e raise(ParseError.build(e, string), cause: e) end end
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 adapter will be used for this call.
Also aliased as: decode
Source
# File lib/multi_json.rb, line 87 def load_adapter(new_adapter) case new_adapter when String, Symbol load_adapter_from_string_name new_adapter.to_s when NilClass, FalseClass load_adapter default_adapter when Class, Module new_adapter else raise ::LoadError, new_adapter end rescue ::LoadError => e raise(AdapterError.build(e), cause: e) end
Source
# File lib/multi_json.rb, line 79 def use(new_adapter) @adapter = load_adapter(new_adapter) ensure OptionsCache.reset end
Set the JSON parser utilizing a symbol, string, or class. Supported by default are:
-
:oj
-
:json_gem
-
:json_pure
-
:ok_json
-
:yajl
-
:gson
(JRuby only) -
:jr_jackson
(JRuby only)
Source
# File lib/multi_json.rb, line 133 def with_adapter(new_adapter) old_adapter = adapter self.adapter = new_adapter yield ensure self.adapter = old_adapter end
Executes passed block using specified adapter.
Also aliased as: with_engine
Private Instance Methods
Source
# File lib/multi_json.rb, line 156 def installable_adapter REQUIREMENT_MAP.each do |adapter, library| require library return adapter rescue ::LoadError next end nil end
Attempts to load and return the first installable adapter
Source
# File lib/multi_json.rb, line 166 def load_adapter_from_string_name(name) normalized_name = ALIASES.fetch(name, name).to_s require "multi_json/adapters/#{normalized_name.downcase}" klass_name = normalized_name.split("_").map(&:capitalize).join MultiJson::Adapters.const_get(klass_name) end
Source
# File lib/multi_json.rb, line 145 def loaded_adapter return :oj if defined?(::Oj) return :yajl if defined?(::Yajl) return :jr_jackson if defined?(::JrJackson) return :json_gem if defined?(::JSON::Ext::Parser) return :gson if defined?(::Gson) nil end
Checks for already loaded adapters and returns the first match