diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index b6c72b5..5adca39 100644 Binary files a/lib/net47/PowerShellYamlSerializer.dll and b/lib/net47/PowerShellYamlSerializer.dll differ diff --git a/lib/netstandard2.1/PowerShellYamlSerializer.dll b/lib/netstandard2.1/PowerShellYamlSerializer.dll index 605ed3f..8f4f602 100644 Binary files a/lib/netstandard2.1/PowerShellYamlSerializer.dll and b/lib/netstandard2.1/PowerShellYamlSerializer.dll differ diff --git a/powershell-yaml.psm1 b/powershell-yaml.psm1 index 6bd374c..87398b4 100644 --- a/powershell-yaml.psm1 +++ b/powershell-yaml.psm1 @@ -23,6 +23,8 @@ enum SerializationOptions { DefaultToStaticType = 16 WithIndentedSequences = 32 OmitNullValues = 64 + UseFlowStyle = 128 + UseSequenceFlowStyle = 256 } $here = Split-Path -Parent $MyInvocation.MyCommand.Path $infinityRegex = [regex]::new('^[-+]?(\.inf|\.Inf|\.INF)$', "Compiled, CultureInvariant"); @@ -412,9 +414,11 @@ function Get-Serializer { } $omitNull = $Options.HasFlag([SerializationOptions]::OmitNullValues) + $useFlowStyle = $Options.HasFlag([SerializationOptions]::UseFlowStyle) + $useSequenceFlowStyle = $Options.HasFlag([SerializationOptions]::UseSequenceFlowStyle) - $stringQuoted = $stringQuotedAssembly.GetType("StringQuotingEmitter") - $builder = $stringQuoted::Add($builder, $omitNull) + $stringQuoted = $stringQuotedAssembly.GetType("BuilderUtils") + $builder = $stringQuoted::BuildSerializer($builder, $omitNull, $useFlowStyle, $useSequenceFlowStyle) return $builder.Build() } @@ -432,6 +436,7 @@ function ConvertTo-Yaml { [Parameter(ParameterSetName = 'NoOptions')] [switch]$JsonCompatible, + [switch]$UseFlowStyle, [switch]$KeepArray, diff --git a/src/PowerShellYamlSerializer.cs b/src/PowerShellYamlSerializer.cs index 1a9df4f..e3b27bf 100644 --- a/src/PowerShellYamlSerializer.cs +++ b/src/PowerShellYamlSerializer.cs @@ -117,8 +117,37 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter) { base.Emit(eventInfo, emitter); } - // objectGraphVisitor, w => w.OnTop() - public static SerializerBuilder Add(SerializerBuilder builder, bool omitNullValues = false) { +} + +public class FlowStyleAllEmitter: ChainedEventEmitter { + public FlowStyleAllEmitter(IEventEmitter next): base(next) {} + + public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter) { + eventInfo.Style = MappingStyle.Flow; + base.Emit(eventInfo, emitter); + } + + public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter){ + eventInfo.Style = SequenceStyle.Flow; + nextEmitter.Emit(eventInfo, emitter); + } +} + +public class FlowStyleSequenceEmitter: ChainedEventEmitter { + public FlowStyleSequenceEmitter(IEventEmitter next): base(next) {} + + public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter){ + eventInfo.Style = SequenceStyle.Flow; + nextEmitter.Emit(eventInfo, emitter); + } +} + +class BuilderUtils { + public static SerializerBuilder BuildSerializer( + SerializerBuilder builder, + bool omitNullValues = false, + bool useFlowStyle = false, + bool useSequenceFlowStyle = false) { builder = builder .WithEventEmitter(next => new StringQuotingEmitter(next)) .WithTypeConverter(new BigIntegerTypeConverter()) @@ -127,6 +156,13 @@ public static SerializerBuilder Add(SerializerBuilder builder, bool omitNullValu builder = builder .WithEmissionPhaseObjectGraphVisitor(args => new NullValueGraphVisitor(args.InnerVisitor)); } + if (useFlowStyle == true) { + builder = builder.WithEventEmitter(next => new FlowStyleAllEmitter(next)); + } + if (useSequenceFlowStyle == true) { + builder = builder.WithEventEmitter(next => new FlowStyleSequenceEmitter(next)); + } + return builder; } }