Class: XToJson::Engines::ActiveSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/x_to_json/engines/active_support.rb

Class Method Summary collapse

Class Method Details

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



6
7
8
9
# File 'lib/x_to_json/engines/active_support.rb', line 6

def self.decode(string, options = {})
  hash = ::ActiveSupport::JSON.decode(string)
  options[:symbolize_keys] ? symbolize_keys(hash) : hash
end

.encode(object) ⇒ Object



11
12
13
# File 'lib/x_to_json/engines/active_support.rb', line 11

def self.encode(object)
  ::ActiveSupport::JSON.encode(object)
end

.symbolize_keys(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/x_to_json/engines/active_support.rb', line 15

def self.symbolize_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = case key
              when String then key.to_sym
              else key
              end
    new_value = case value
                when Hash then symbolize_keys(value)
                else value
                end
    result[new_key] = new_value
    result
  }
end