Module: MultiJson::Adapters::OkJson

Extended by:
OkJson
Included in:
Nsjsonserialization, OkJson
Defined in:
lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Instance Method Summary collapse

Instance Method Details

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

:nodoc:


16
17
18
# File 'lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb', line 16

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

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

:nodoc:


10
11
12
13
14
# File 'lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb', line 10

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

#prepare_object(object, &key_modifier) ⇒ Object

:nodoc:


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb', line 32

def prepare_object(object, &key_modifier) #:nodoc:
  case object
  when Array
    object.map do |value|
      prepare_object(value, &key_modifier)
    end
  when Hash
    object.inject({}) do |result, (key, value)|
      new_key   = key_modifier.call(key)
      new_value = prepare_object(value, &key_modifier)
      result.merge! new_key => new_value
    end
  when String, Numeric, true, false, nil
    object
  else
    if object.respond_to?(:to_json)
      object
    elsif object.respond_to?(:to_s)
      object.to_s
    else
      object
    end
  end
end

#stringify_keys(object) ⇒ Object

:nodoc:


26
27
28
29
30
# File 'lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb', line 26

def stringify_keys(object) #:nodoc:
  prepare_object(object) do |key|
    key.respond_to?(:to_s) ? key.to_s : key
  end
end

#symbolize_keys(object) ⇒ Object

:nodoc:


20
21
22
23
24
# File 'lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb', line 20

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