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

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.1"

Class Method Summary collapse

Class Method Details

.adapterObject

Get the current adapter class.


51
52
53
54
55
# File 'lib/multi_json.rb', line 51

def adapter
  return @adapter if @adapter
  self.use self.default_adapter
  @adapter
end

.decode(string, options = {}) ⇒ Object

TODO: Remove for 2.0 release (but no sooner)


88
89
90
91
# File 'lib/multi_json.rb', line 88

def decode(string, options={})
  deprecate("MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead.")
  self.load(string, options)
end

.default_adapterObject

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

.deprecate(raw_message) ⇒ Object

Sends of a deprecation warning


116
117
118
119
120
121
122
123
# File 'lib/multi_json.rb', line 116

def deprecate(raw_message)
  @messages ||= {}
  message = "#{Kernel.caller[1]}: [DEPRECATION] #{raw_message}"
  unless @messages[message]
    @messages[message] = true
    Kernel.warn message
  end
end

.dump(object, options = {}) ⇒ Object

Encodes a Ruby object as JSON.


111
112
113
# File 'lib/multi_json.rb', line 111

def dump(object, options={})
  adapter.dump(object, options)
end

.encode(object, options = {}) ⇒ Object

TODO: Remove for 2.0 release (but no sooner)


105
106
107
108
# File 'lib/multi_json.rb', line 105

def encode(object, options={})
  deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.")
  self.dump(object, options)
end

.engineObject

TODO: Remove for 2.0 release (but no sooner)


45
46
47
48
# File 'lib/multi_json.rb', line 45

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)


58
59
60
61
# File 'lib/multi_json.rb', line 58

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.


98
99
100
101
102
# File 'lib/multi_json.rb', line 98

def load(string, options={})
  adapter.load(string, options)
rescue adapter::ParseError => exception
  raise DecodeError.new(exception.message, exception.backtrace, string)
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)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/multi_json.rb', line 72

def use(new_adapter)
  case new_adapter
  when String, Symbol
    require "multi_json/adapters/#{new_adapter}"
    @adapter = MultiJson::Adapters.const_get("#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when NilClass
    @adapter = nil
  when Class
    @adapter = new_adapter
  else
    raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
  end
end