Serialization

edit

YamlSerializer converts .NET objects to YAML and YAML to .NET objects. It writes the public properties of a class as a mapping, collections as sequences, and the common .NET types as scalars. YamlSerializerOptions and attributes on your types control the result.

  • Serialize and deserialize: strings, files and streams, reusable options, reading without exceptions.
  • Supported types: the scalar types, collections and dictionaries, object, constructors and records.
  • Members and names: naming policies, renamed, ignored, ordered and non-public members, enum values.
  • Output format: indentation, flow collections, sequence layout, string styles, key order.
  • Reading behavior: unknown keys, required values, numbers in strings, filling existing collections, extra keys, callbacks.
  • Custom converters: read and write a type your own way.
  • Polymorphism: serialize a class hierarchy with a discriminator key or a YAML tag.
  • C# unions: serialize a union as its selected case.
  • Anchors, aliases and merge keys: shared references, cycles and <<.
  • Schemas: how untagged scalars such as yes, 0x10 or ~ are resolved.

The serializer discovers your types with reflection. To serialize without reflection, in a NativeAOT or trimmed application, generate the metadata at build time: see Source generation.