Module: RHC::Json
- Defined in:
- lib/rhc/json.rb
Class Method Summary collapse
- .decode(string, options = {}) ⇒ Object
- .encode(object, options = {}) ⇒ Object
- .modify_keys(object, &modifier) ⇒ Object
- .stringify_keys(object) ⇒ Object
- .symbolize_keys(object) ⇒ Object
Class Method Details
.decode(string, options = {}) ⇒ Object
7 8 9 10 11 |
# File 'lib/rhc/json.rb', line 7 def self.decode(string, ={}) string = string.read if string.respond_to?(:read) result = RHC::Vendor::OkJson.decode(string) [:symbolize_keys] ? symbolize_keys(result) : result end |
.encode(object, options = {}) ⇒ Object
13 14 15 |
# File 'lib/rhc/json.rb', line 13 def self.encode(object, ={}) RHC::Vendor::OkJson.valenc(stringify_keys(object)) end |
.modify_keys(object, &modifier) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rhc/json.rb', line 29 def self.modify_keys(object, &modifier) 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
23 24 25 26 27 |
# File 'lib/rhc/json.rb', line 23 def self.stringify_keys(object) modify_keys(object) do |key| key.is_a?(Symbol) ? key.to_s : key end end |