Class: Superstore::Types::JsonType

Inherits:
BaseType
  • Object
show all
Defined in:
lib/superstore/types/json_type.rb

Instance Attribute Summary

Attributes inherited from BaseType

#options

Instance Method Summary collapse

Methods inherited from BaseType

#default, #initialize

Constructor Details

This class inherits a constructor from Superstore::Types::BaseType

Instance Method Details

#decode(str) ⇒ Object



8
9
10
# File 'lib/superstore/types/json_type.rb', line 8

def decode(str)
  ActiveSupport::JSON.decode(str)
end

#encode(data) ⇒ Object



4
5
6
# File 'lib/superstore/types/json_type.rb', line 4

def encode(data)
  ActiveSupport::JSON.encode(data)
end

#typecast(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/superstore/types/json_type.rb', line 12

def typecast(data)
  return data if ActiveSupport.parse_json_times

  if data.acts_like?(:time) || data.acts_like?(:date)
    data.as_json
  elsif data.is_a?(Array)
    data.map { |d| typecast(d) }
  elsif data.is_a?(Hash)
    data.each_with_object({}) { |(key, value), hash| hash[key] = typecast(value) }
  else
    data
  end
end