Path: redb-route/redb.Route.DataFormats.Yaml/YamlDataFormat.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.DataFormats.Yaml/YamlDataFormat.cs ---- using System.Collections; using System.Text; using redb.Route.Abstractions; using YamlDotNet.Core; using YamlDotNet.Serialization; namespace redb.Route.DataFormats.Yaml; /// /// YAML (YamlDotNet). Marshals any object; unmarshals to a POCO, or to a /// dynamic tree (Dictionary<string, object?> / List<object?> / scalars) when the target /// is object. Errors name the format. /// public sealed class YamlDataFormat : IMessageSerializer { /// Content type the format is registered under. public const string DefaultContentType = "application/yaml"; /// The name the format answers to in markup, <marshal format="yaml"/>, as Apache Camel names it; AddYamlDataFormat() registers it. public const string Name = "yaml"; private readonly YamlDataFormatOptions _options; private readonly ISerializer _serializer; private readonly IDeserializer _deserializer; /// Creates the format with the given options (defaults when null). public YamlDataFormat(YamlDataFormatOptions? options = null) { _options = options ?? new YamlDataFormatOptions(); _serializer = new SerializerBuilder().WithNamingConvention(_options.NamingConvention).Build(); var builder = new DeserializerBuilder().WithNamingConvention(_options.NamingConvention); if (_options.IgnoreUnmatchedProperties) builder = builder.IgnoreUnmatchedProperties(); _deserializer = builder.Build(); } /// The options in effect. public YamlDataFormatOptions Options => _options; /// public string ContentType => DefaultContentType; /// public IReadOnlyCollection MediaTypes => [DefaultContentType, "application/x-yaml", "text/yaml", "text/x-yaml"]; /// public byte[] Serialize(T value) { switch (value) { case null: return []; case byte[] bytes: return bytes; case string text: return Encoding.UTF8.GetBytes(text); } try { return Encoding.UTF8.GetBytes(_serializer.Serialize(value)); } catch (YamlException ex) { throw new InvalidOperationException($"YAML data format: cannot marshal {value.GetType().Name} ({ex.Message}).", ex); } } /// public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T)); /// public object? Deserialize(byte[] data, Type type) { ArgumentNullException.ThrowIfNull(data); ArgumentNullException.ThrowIfNull(type); var text = Encoding.UTF8.GetString(data); if (type == typeof(string)) return text; try { return type == typeof(object) ? Normalize(_deserializer.Deserialize(text)) : _deserializer.Deserialize(text, type); } catch (YamlException ex) { throw new InvalidOperationException($"YAML data format: the body is not valid YAML for {type.Name} ({ex.Message}).", ex); } } /// YamlDotNet's dynamic tree uses Dictionary<object, object>; expressions and templates want string keys. private static object? Normalize(object? node) => node switch { IDictionary dictionary => NormalizeMap(dictionary), string s => s, IEnumerable list => list.Cast().Select(Normalize).ToList(), _ => node, }; private static Dictionary NormalizeMap(IDictionary dictionary) { var result = new Dictionary(StringComparer.Ordinal); foreach (DictionaryEntry entry in dictionary) result[entry.Key?.ToString() ?? string.Empty] = Normalize(entry.Value); return result; } }