| | | 1 | | using YamlDotNet.Serialization; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Utilities.Yaml; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Utility class for building YAML serializers with common settings |
| | | 7 | | /// </summary> |
| | | 8 | | public class BuilderUtils |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Builds a YamlDotNet ISerializerBuilder with common settings. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="builder">The serializer builder to configure.</param> |
| | | 14 | | /// <param name="omitNullValues">Whether to omit null values.</param> |
| | | 15 | | /// <param name="useFlowStyle">Whether to use flow style for collections.</param> |
| | | 16 | | /// <param name="useSequenceFlowStyle">Whether to use flow style for sequences.</param> |
| | | 17 | | /// <param name="jsonCompatible">Whether to make the output JSON compatible.</param> |
| | | 18 | | /// <returns>The configured serializer builder.</returns> |
| | | 19 | | public static SerializerBuilder BuildSerializer( |
| | | 20 | | SerializerBuilder builder, |
| | | 21 | | bool omitNullValues = false, |
| | | 22 | | bool useFlowStyle = false, |
| | | 23 | | bool useSequenceFlowStyle = false, |
| | | 24 | | bool jsonCompatible = false) |
| | | 25 | | { |
| | 21 | 26 | | if (jsonCompatible) |
| | | 27 | | { |
| | 0 | 28 | | useFlowStyle = true; |
| | 0 | 29 | | useSequenceFlowStyle = true; |
| | | 30 | | } |
| | | 31 | | |
| | 21 | 32 | | builder = builder |
| | 21 | 33 | | .WithEventEmitter(next => new StringQuotingEmitter(next)) |
| | 21 | 34 | | .WithTypeConverter(new BigIntegerTypeConverter()) |
| | 21 | 35 | | .WithTypeConverter(new IDictionaryTypeConverter(omitNullValues, useFlowStyle)) |
| | 21 | 36 | | .WithTypeConverter(new PSObjectTypeConverter(omitNullValues, useFlowStyle)); |
| | | 37 | | // Use platform newline; tests explicitly reference [Environment]::NewLine for single-line cases and replace it |
| | | 38 | | //.WithNewLine("\n"); |
| | | 39 | | |
| | 21 | 40 | | if (omitNullValues) |
| | | 41 | | { |
| | 2 | 42 | | builder = builder |
| | 7 | 43 | | .WithEmissionPhaseObjectGraphVisitor(args => new NullValueGraphVisitor(args.InnerVisitor)); |
| | | 44 | | } |
| | 21 | 45 | | if (useFlowStyle) |
| | | 46 | | { |
| | 6 | 47 | | builder = builder.WithEventEmitter(next => new FlowStyleAllEmitter(next)); |
| | | 48 | | } |
| | 21 | 49 | | if (useSequenceFlowStyle) |
| | | 50 | | { |
| | 4 | 51 | | builder = builder.WithEventEmitter(next => new FlowStyleSequenceEmitter(next)); |
| | | 52 | | } |
| | | 53 | | |
| | 21 | 54 | | return builder; |
| | | 55 | | } |
| | | 56 | | } |