module KYAML::Serializable

Overview

Mixin module providing de/serialization mirroring YAML::Serializable. Include it in a struct/class to get #to_kyaml and #from_kyaml methods. Fields map to mapping entries in the order declared.

Defined in:

kyaml/serializable.cr

Macro Summary

Instance Method Summary

Macro Detail

macro use_kyaml_discriminator(field, mapping) #

Enables polymorphic deserialization (choosing a runtime type from an imported data field) via use_kyaml_discriminator.

Call it in an abstract base type that includes KYAML::Serializable. field is the discriminator key, and mapping maps each discriminator value to the concrete subtype to generate.

abstract class Shape
  include KYAML::Serializable
  use_kyaml_discriminator "kind", {circle: Circle, rectangle: Rectangle}
  property kind : String
end

[View source]

Instance Method Detail

def kyaml_presence #

Set of KYAML doc keys that were present in the parsed input for fields marked with @[KYAML::Field(presence: true)] and populated during deserialization. Declared default keeps this auto-initialized for every constructor.

Annotated with ignore: true so that this meta-field is never considered as a de/serializable.


[View source]
def kyaml_present?(key : String) : Bool #

Returns true if KYAML doc key (post-rename) was present in the parsed input. Only meaningful for fields declared with @[KYAML::Field(presence: true)], otherwise returns false.

Deviation from upstream YAML due to type scope and macro timing, so presence is exposed through this predicate.


[View source]
def to_kyaml(builder : KYAML::Builder) : Nil #

Emits this object as a KYAML mapping into builder. Honors @[KYAML::Field(key:)] renames, skips ignore: true and nil fields.


[View source]
def to_kyaml(io : IO) : Nil #

Emits this object as KYAML to io


[View source]
def to_kyaml : String #

Emits this object as KYAML and returns it as a String.


[View source]