module YAML::ArrayConverter(Converter)

Overview

Converter to be used with YAML::Serializable to serialize the elements of an Array(T) with the custom converter.

require "yaml"

class Timestamp
  include YAML::Serializable

  @[YAML::Field(converter: YAML::ArrayConverter(Time::EpochConverter))]
  property values : Array(Time)
end

timestamp = Timestamp.from_yaml(%({"values":[1459859781,1567628762]}))
timestamp.values  # => [2016-04-05 12:36:21 UTC, 2019-09-04 20:26:02 UTC]
timestamp.to_yaml # => "---\nvalues:\n- 1459859781\n- 1567628762\n"

YAML::ArrayConverter.new should be used if the nested converter is also an instance instead of a type.

require "yaml"

class Timestamp
  include YAML::Serializable

  @[YAML::Field(converter: YAML::ArrayConverter.new(Time::Format.new("%b %-d, %Y")))]
  property values : Array(Time)
end

timestamp = Timestamp.from_yaml(%({"values":["Apr 5, 2016","Sep 4, 2019"]}))
timestamp.values  # => [2016-04-05 00:00:00 UTC, 2019-09-04 00:00:00 UTC]
timestamp.to_yaml # => "---\nvalues:\n- Apr 5, 2016\n- Sep 4, 2019\n"

This implies that YAML::ArrayConverter(T) and YAML::ArrayConverter(T.class).new(T) perform the same serializations.

Defined in:

yaml/from_yaml.cr
yaml/to_yaml.cr

Constructors

Class Method Summary

Constructor Detail

def self.new(converter : Converter) #

[View source]

Class Method Detail

def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array #

[View source]
def self.to_yaml(values : Array, yaml : YAML::Nodes::Builder) #

[View source]