Module: Codecs::UnionCodec

Defined in:
lib/emery/codecs.rb

Class Method Summary collapse

Class Method Details

.applicable?(type) ⇒ Boolean

Returns:



77
78
79
# File 'lib/emery/codecs.rb', line 77

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

.deserialize(type, json_value) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
# File 'lib/emery/codecs.rb', line 80

def self.deserialize(type, json_value)
  type.types.each do |t|
    begin
      return Jsoner.deserialize(t, json_value)
    rescue JsonerError
    end
  end
  raise JsonerError.new("Value '#{json_value.inspect.to_s}' can not be deserialized as any of #{type.types.map { |t| t.to_s}.join(', ')}")
end

.serialize(type, value) ⇒ Object



89
90
91
92
93
# File 'lib/emery/codecs.rb', line 89

def self.serialize(type, value)
  T.check(type, value)
  t = type.types.find {|t| T.instance_of?(t, value) }
  Jsoner.serialize(t, value)
end