Class: MultiJson::Adapters::OkJson
- Inherits:
-
Object
- Object
- MultiJson::Adapters::OkJson
- Defined in:
- lib/multi_json/adapters/ok_json.rb
Direct Known Subclasses
Constant Summary collapse
Class Method Summary collapse
-
.dump(object, options = {}) ⇒ Object
:nodoc:.
-
.load(string, options = {}) ⇒ Object
:nodoc:.
-
.modify_keys(object, &modifier) ⇒ Object
:nodoc:.
-
.stringify_keys(object) ⇒ Object
:nodoc:.
-
.symbolize_keys(object) ⇒ Object
:nodoc:.
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, ={}) #: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, ={}) #:nodoc: string = string.read if string.respond_to?(:read) result = ::MultiJson::OkJson.decode(string) [: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 |