module MultiJson

Constants

ALIASES
REQUIREMENT_MAP

Public Instance Methods

Get the current adapter class.

# File lib/multi_json.rb, line 60
def adapter
  return @adapter if defined?(@adapter) && @adapter

  use nil # load default adapter

  @adapter
end
Also aliased as: engine
# File lib/multi_json.rb, line 118
def current_adapter(options = {})
  if (new_adapter = options[:adapter])
    load_adapter(new_adapter)
  else
    adapter
  end
end

The default adapter based on what you currently have loaded and installed.

# 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
Also aliased as: default_engine
# 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
# 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

Encodes a Ruby object as JSON.

# File lib/multi_json.rb, line 127
def dump(object, options = {})
  current_adapter(options).dump(object, options)
end
Also aliased as: encode
Alias for: adapter

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.

# 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
Also aliased as: decode
# 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

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)

# File lib/multi_json.rb, line 79
def use(new_adapter)
  @adapter = load_adapter(new_adapter)
ensure
  OptionsCache.reset
end
Also aliased as: adapter=, engine=

Executes passed block using specified adapter.

# 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
Also aliased as: with_engine