Module: RubyDoozer::Json::Serializer
- Defined in:
- lib/ruby_doozer/json/serializer.rb
Overview
Serialize to JSON for storing in Doozer
Class Method Summary collapse
-
.desymbolize(v) ⇒ Object
Returns the supplied value with symbols converted to a string prefixed with ‘:’.
-
.desymbolize_array(a) ⇒ Object
Returns a new Array with any symbols returned as symbol strings.
-
.desymbolize_hash(hash) ⇒ Object
Returns a new hash with all symbol keys and values as strings starting with ‘:’.
- .desymbolize_symbol(s) ⇒ Object
- .serialize(value) ⇒ Object
Class Method Details
.desymbolize(v) ⇒ Object
Returns the supplied value with symbols converted to a string prefixed with ‘:’
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_doozer/json/serializer.rb', line 19 def self.desymbolize(v) if v.is_a?(Hash) desymbolize_hash(v) elsif v.is_a?(Array) desymbolize_array(v) elsif v.is_a?(Symbol) desymbolize_symbol(v) else v.to_s end end |
.desymbolize_array(a) ⇒ Object
Returns a new Array with any symbols returned as symbol strings
45 46 47 |
# File 'lib/ruby_doozer/json/serializer.rb', line 45 def self.desymbolize_array(a) a.collect {|v| desymbolize(v)} end |
.desymbolize_hash(hash) ⇒ Object
Returns a new hash with all symbol keys and values as strings starting with ‘:’
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_doozer/json/serializer.rb', line 32 def self.desymbolize_hash(hash) h = hash.dup hash.each_pair do |k, v| # Convert values in the hash h[k] = desymbolize(v) # Convert key to a string if it is a symbol h[desymbolize_symbol(k)] = h.delete(k) if k.is_a?(Symbol) end h end |
.desymbolize_symbol(s) ⇒ Object
49 50 51 |
# File 'lib/ruby_doozer/json/serializer.rb', line 49 def self.desymbolize_symbol(s) ":#{s}" end |
.serialize(value) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/ruby_doozer/json/serializer.rb', line 7 def self.serialize(value) if value.is_a?(Hash) || value.is_a?(Array) MultiJson.encode(desymbolize(value)) elsif value.is_a?(Symbol) desymbolize_symbol(value) else value.to_s end end |