Class: MultiJson::Adapters::OkJson

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_json/adapters/ok_json.rb

Direct Known Subclasses

Nsjsonserialization

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Class Method Summary collapse

Class Method Details

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

:nodoc:



14
15
16
# File 'lib/multi_json/adapters/ok_json.rb', line 14

def self.dump(object, options={}) #:nodoc:
  ::MultiJson::OkJson.valenc(stringify_keys(object))
end

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

:nodoc:



8
9
10
11
12
# File 'lib/multi_json/adapters/ok_json.rb', line 8

def self.load(string, options={}) #:nodoc:
  string = string.read if string.respond_to?(:read)
  result = ::MultiJson::OkJson.decode(string)
  options[:symbolize_keys] ? symbolize_keys(result) : result
end

.modify_keys(object, &modifier) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/multi_json/adapters/ok_json.rb', line 30

def self.modify_keys(object, &modifier) #:nodoc:
  case object
  when Array
    object.map do |value|
      modify_keys(value, &modifier)
    end
  when Hash
    object.inject({}) do |result, (key, value)|
      new_key   = modifier.call(key)
      new_value = modify_keys(value, &modifier)
      result.merge! new_key => new_value
    end
  else
    object
  end
end

.stringify_keys(object) ⇒ Object

:nodoc:



24
25
26
27
28
# File 'lib/multi_json/adapters/ok_json.rb', line 24

def self.stringify_keys(object) #:nodoc:
  modify_keys(object) do |key|
    key.is_a?(Symbol) ? key.to_s : key
  end
end

.symbolize_keys(object) ⇒ Object

:nodoc:



18
19
20
21
22
# File 'lib/multi_json/adapters/ok_json.rb', line 18

def self.symbolize_keys(object) #:nodoc:
  modify_keys(object) do |key|
    key.is_a?(String) ? key.to_sym : key
  end
end