| | | 1 | | using YamlDotNet.Serialization; |
| | | 2 | | using YamlDotNet.Serialization.TypeResolvers; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Utilities.Yaml; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Factory for creating YamlDotNet serializers with specified options |
| | | 8 | | /// </summary> |
| | | 9 | | public static class YamlSerializerFactory |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builds a YamlDotNet serializer according to <paramref name="options"/>, |
| | | 13 | | /// then passes it through BuilderUtils.BuildSerializer(...) to apply |
| | | 14 | | /// omit-null/flow-style knobs (as in your PowerShell version). |
| | | 15 | | /// </summary> |
| | | 16 | | public static ISerializer GetSerializer(SerializationOptions options) |
| | | 17 | | { |
| | 21 | 18 | | var builder = new SerializerBuilder(); |
| | | 19 | | |
| | 21 | 20 | | var jsonCompatible = options.HasFlag(SerializationOptions.JsonCompatible); |
| | | 21 | | |
| | 21 | 22 | | if (options.HasFlag(SerializationOptions.Roundtrip)) |
| | | 23 | | { |
| | 0 | 24 | | builder = builder.EnsureRoundtrip(); |
| | | 25 | | } |
| | | 26 | | |
| | 21 | 27 | | if (options.HasFlag(SerializationOptions.DisableAliases)) |
| | | 28 | | { |
| | 6 | 29 | | builder = builder.DisableAliases(); |
| | | 30 | | } |
| | 21 | 31 | | if (options.HasFlag(SerializationOptions.EmitDefaults)) |
| | | 32 | | { |
| | 21 | 33 | | builder = builder.ConfigureDefaultValuesHandling(DefaultValuesHandling.Preserve); |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | | 37 | | // This matches your old OmitNullValues/EmitDefaults combo |
| | 0 | 38 | | builder = builder.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull); |
| | | 39 | | } |
| | | 40 | | |
| | 21 | 41 | | if (jsonCompatible) |
| | | 42 | | { |
| | 0 | 43 | | builder = builder.JsonCompatible(); |
| | | 44 | | } |
| | | 45 | | |
| | 21 | 46 | | if (options.HasFlag(SerializationOptions.DefaultToStaticType)) |
| | | 47 | | { |
| | 0 | 48 | | builder = builder.WithTypeResolver(new StaticTypeResolver()); |
| | | 49 | | } |
| | | 50 | | |
| | 21 | 51 | | if (options.HasFlag(SerializationOptions.WithIndentedSequences)) |
| | | 52 | | { |
| | 7 | 53 | | builder = builder.WithIndentedSequences(); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Extras handled by your helper (same as PS): |
| | 21 | 57 | | var omitNullValues = options.HasFlag(SerializationOptions.OmitNullValues); |
| | 21 | 58 | | var useFlowStyle = options.HasFlag(SerializationOptions.UseFlowStyle); |
| | 21 | 59 | | var useSequenceFlowStyle = options.HasFlag(SerializationOptions.UseSequenceFlowStyle); |
| | | 60 | | |
| | | 61 | | // Apply the common settings via your shared utility method |
| | 21 | 62 | | builder = BuilderUtils.BuildSerializer( |
| | 21 | 63 | | builder, |
| | 21 | 64 | | omitNullValues, |
| | 21 | 65 | | useFlowStyle, |
| | 21 | 66 | | useSequenceFlowStyle, |
| | 21 | 67 | | jsonCompatible |
| | 21 | 68 | | ); |
| | | 69 | | |
| | 21 | 70 | | return builder.Build(); |
| | | 71 | | } |
| | | 72 | | } |