Module: Lutaml::Json::Type::Serializers

Defined in:
lib/lutaml/json/type/serializers.rb

Overview

Registers JSON-specific type serializers for types that need custom to_json / from_json behavior beyond the default.

Class Method Summary collapse

Class Method Details

.register_all!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lutaml/json/type/serializers.rb', line 11

def register_all!
  v = Lutaml::Model::Type::Value

  # String — from_json delegates to cast
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::String,
    from: ->(val) { Lutaml::Model::Type::String.cast(val) }
  )

  # Time — ISO8601 with fractional seconds handling
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::Time,
    to: lambda { |inst|
      return nil unless inst.value

      if inst.value.subsec.zero?
        inst.value.iso8601
      else
        inst.value.iso8601(6).sub(/(\.\d{3})0{3}([+-])/, '\1\2')
      end
    }
  )

  # DateTime — RFC3339 (ISO8601 with timezone)
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::DateTime,
    to: lambda { |inst|
      return nil unless inst.value

      Lutaml::Model::Type::DateTime.format_datetime_iso8601(inst.value)
    }
  )

  # Date — ISO8601 with optional timezone
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::Date,
    to: ->(inst) { Lutaml::Model::Type::Date.serialize(inst.value) }
  )

  # TimeWithoutDate — HH:MM:SS format
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::TimeWithoutDate,
    to: ->(inst) { Lutaml::Model::Type::TimeWithoutDate.serialize(inst.value) }
  )

  # Symbol — :symbol: format
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::Symbol,
    to: ->(inst) { ":#{inst.value}:" }
  )

  # Reference — key
  v.register_format_type_serializer(
    :json, Lutaml::Model::Type::Reference,
    to: lambda(&:key),
    from: ->(val) { Lutaml::Model::Type::Reference.cast(val) }
  )
end