Module: Codecs::HashCodec

Defined in:
lib/emery/codecs.rb

Class Method Summary collapse

Class Method Details

.applicable?(type) ⇒ Boolean

Returns:



48
49
50
# File 'lib/emery/codecs.rb', line 48

def self.applicable?(type)
  type.instance_of? T::HashType
end

.deserialize(type, json_value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/emery/codecs.rb', line 51

def self.deserialize(type, json_value)
  T.check_not_nil(type, json_value)
  if type.key_type != String
    raise JsonerError.new("Hash key type #{type.key_type} is not supported for JSON (de)serialization - key should be String")
  end
  if !json_value.is_a?(Hash)
    raise JsonerError.new("JSON value type #{json_value.class} is not Hash")
  end
  json_value.map do |key, value|
    [T.check(type.key_type, key), Jsoner.deserialize(type.value_type, value)]
  end.to_h
end

.serialize(type, value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/emery/codecs.rb', line 63

def self.serialize(type, value)
  if type.key_type != String
    raise JsonerError.new("Hash key type #{type.key_type} is not supported for JSON (de)serialization - key should be String")
  end
  if !value.is_a?(Hash)
    raise JsonerError.new("Value type #{value.class} is not Hash")
  end
  value.map do |key, value|
    [T.check(type.key_type, key), Jsoner.serialize(type.value_type, value)]
  end.to_h
end