oneyaml C# cookbook
Short recipes for common YAML tasks in C# with OneYaml, each a complete program that was compiled and run against OneYaml 1.2.1.
Recipes
Parse YAML into C# classesRead a YAML file into C# classes, records and enums with OneYaml’s YamlSerializer, with naming policies, defaults, required keys and an error for unknown keys.Read YAML without a classRead values from YAML by JSON Pointer in C#, walk mappings and sequences with OneYaml’s YamlNode like a JsonNode, and get each value’s line and column.Edit YAML and keep its commentsChange a value, remove a list item and add keys and list items in a YAML file from C# with OneYaml’s YamlDocument, with every other byte and comment kept.Merge or patch YAMLApply a Merge Patch (RFC 7396) as an environment overlay and a guarded JSON Patch (RFC 6902) built with YamlPatch to a YAML file in C#, keeping its comments.Convert YAML to JSON and backConvert YAML to JSON in C# by reading it into a System.Text.Json JsonNode with OneYaml, and convert JSON back to YAML with YamlSerializer.Read Kubernetes manifests and multi-document YAMLRead every document of a multi-document YAML file such as a Kubernetes manifest into C# records with DeserializeAll, then edit one document and keep the rest byte for byte.Report errors with line and columnCatch OneYaml’s YamlException in C# and report the line, column, category and path of a syntax error, a duplicate key or a value that doesn’t fit its type.Anchors, aliases and merge keysRead YAML anchors and aliases in C#, turn on << merge keys for docker-compose-style files with MergeKeys, and follow an alias to its anchor in a YamlDocument.Read untrusted YAML safelyRead YAML from the network in C# with YamlLimits.Untrusted, refuse billion-laughs alias bombs and deep nesting, and size the limits for your input.Use YAML in a native AOT appRead and write YAML in a trimmed or native AOT .NET app with OneYaml’s source generator, using a YamlSerializerContext instead of reflection.Switch from YamlDotNet with one packageMove C# code from YamlDotNet to OneYaml with the OneYaml.Compat.YamlDotNet package, keeping DeserializerBuilder, SerializerBuilder and naming conventions.