Module: MultiJson

Defined in:
lib/multi_json.rb,
lib/multi_json/engines/yajl.rb,
lib/multi_json/engines/json_gem.rb,
lib/multi_json/engines/json_pure.rb,
lib/multi_json/engines/active_support.rb

Defined Under Namespace

Modules: Engines

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.



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

def decode(string, options = {})
  engine.decode(string, options)
end

.default_engineObject

The default engine based on what you currently have loaded. Tries Yajl first, then JSON gem, then ActiveSupport and JSON pure.



14
15
16
17
18
19
# File 'lib/multi_json.rb', line 14

def default_engine
  return :yajl if defined?(::Yajl)
  return :json_gem if defined?(::JSON)
  return :active_support if defined?(::ActiveSupport::JSON)
  :json_pure
end

.encode(object) ⇒ Object

Encodes a Ruby object as JSON.



50
51
52
# File 'lib/multi_json.rb', line 50

def encode(object)
  engine.encode(object)
end

.engineObject

Get the current engine class.



5
6
7
8
9
# File 'lib/multi_json.rb', line 5

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

  • :active_support (useful for inside Rails apps)

  • :yajl



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/multi_json.rb', line 28

def engine=(new_engine)
  case new_engine
    when String, Symbol
      require "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