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

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

.adapter=(new_adapter) ⇒ Object

TODO: Remove for 2.0 release (but no sooner)



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

def adapter=(new_adapter)
  Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.adapter= is deprecated and will be removed in the next major version. Use MultiJson.use instead."
  self.use(new_adapter)
end

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

TODO: Remove for 2.0 release (but no sooner)



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

def decode(string, options={})
  Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] 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

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

Encodes a Ruby object as JSON.



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

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

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

TODO: Remove for 2.0 release (but no sooner)



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

def encode(object, options={})
  Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] 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
  Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.engine is deprecated and will be removed in the next major version. Use MultiJson.adapter instead."
  self.adapter
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.



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

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

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