Module: MultiJson

Defined in:
lib/json-schema/lib/multi_json/multi_json/vendor/ok_json.rb,
lib/json-schema/lib/multi_json/multi_json.rb,
lib/json-schema/lib/multi_json/multi_json/version.rb,
lib/json-schema/lib/multi_json/multi_json/engines/yajl.rb,
lib/json-schema/lib/multi_json/multi_json/engines/ok_json.rb,
lib/json-schema/lib/multi_json/multi_json/engines/json_gem.rb,
lib/json-schema/lib/multi_json/multi_json/engines/json_pure.rb,
lib/json-schema/lib/multi_json/multi_json/engines/json_common.rb

Overview

Defined Under Namespace

Modules: Engines, OkJson Classes: DecodeError

Constant Summary collapse

REQUIREMENT_MAP =
[
  ["yajl", :yajl],
  ["json", :json_gem],
  ["json/pure", :json_pure]
]
DEFAULT_ENGINE_WARNING =
'Warning: multi_json is using default ok_json engine. Suggested action: require and load an appropriate JSON library.'
VERSION =
"1.0.4"

Class Method Summary collapse

Class Method Details

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

Decode a JSON string into Ruby.

Options

:symbolize_keys

If true, will use symbols instead of strings for the keys.



75
76
77
78
79
# File 'lib/json-schema/lib/multi_json/multi_json.rb', line 75

def decode(string, options = {})
  engine.decode(string, options)
rescue engine::ParseError => exception
  raise DecodeError.new(exception.message, exception.backtrace, string)
end

.default_engineObject

The default engine based on what you currently have loaded and installed. First checks to see if any engines are already loaded, then checks to see which are installed if none are loaded.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/json-schema/lib/multi_json/multi_json.rb', line 34

def default_engine
  return :yajl if defined?(::Yajl)
  return :json_gem if defined?(::JSON)

  REQUIREMENT_MAP.each do |(library, engine)|
    begin
      require library
      return engine
    rescue LoadError
      next
    end
  end

  Kernel.warn DEFAULT_ENGINE_WARNING
  :ok_json
end

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

Encodes a Ruby object as JSON.



82
83
84
# File 'lib/json-schema/lib/multi_json/multi_json.rb', line 82

def encode(object, options = {})
  engine.encode(object, options)
end

.engineObject

Get the current engine class.



16
17
18
19
20
# File 'lib/json-schema/lib/multi_json/multi_json.rb', line 16

def engine
  return @engine if @engine
  self.engine = self.default_engine
  @engine
end

.engine=(new_engine) ⇒ Object

Set the JSON parser utilizing a symbol, string, or class. Supported by default are:

  • :json_gem

  • :json_pure

  • :ok_json

  • :yajl



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/json-schema/lib/multi_json/multi_json.rb', line 58

def engine=(new_engine)
  case new_engine
  when String, Symbol
    require File.join(File.dirname(__FILE__),"multi_json/engines/#{new_engine}")
    @engine = MultiJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when Class
    @engine = new_engine
  else
    raise "Did not recognize your engine specification. Please specify either a symbol or a class."
  end
end