diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index 59e9cde..1dc1d74 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -1,4 +1,4 @@ -# Copyright 2016-2024 Cloudbase Solutions Srl +# Copyright 2016-2024 Cloudbase Solutions Srl # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -190,6 +190,22 @@ children: } Describe "Test PSCustomObject wrapped values are serialized correctly" { + Context "A PSCustomObject that contains an array of PSObjects" { + It "Should serialize correctly" { + $expected = @" +yamlList: +- item1 +- item2 + +"@ + $data = ConvertFrom-YAml "yamlList: []" | ConvertTo-JSON -Depth 3 | ConvertFrom-Json + $jsData = '["item1", "item2"]' + $data.yamlList = $jsData | ConvertFrom-Json + + $asYaml = ConvertTo-Yaml $data + Assert-Equivalent -Options $compareStrictly -Expected $expected -Actual $asYaml + } + } Context "A PSCustomObject containing nested PSCustomObjects" { It "Should serialize correctly" { $expectBigInt = [System.Numerics.BigInteger]::Parse("9999999999999999999999999999999999999999999999999") diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index ad5b86b..904b86b 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 f126f2c..a2c4acf 100644 Binary files a/lib/netstandard2.1/PowerShellYamlSerializer.dll and b/lib/netstandard2.1/PowerShellYamlSerializer.dll differ diff --git a/src/PowerShellYamlSerializer.cs b/src/PowerShellYamlSerializer.cs index 09b0616..dbb4fb2 100644 --- a/src/PowerShellYamlSerializer.cs +++ b/src/PowerShellYamlSerializer.cs @@ -124,6 +124,10 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) { var psObj = (PSObject)value; + if (!typeof(IDictionary).IsAssignableFrom(psObj.BaseObject.GetType()) && !typeof(PSCustomObject).IsAssignableFrom(psObj.BaseObject.GetType())) { + serializer(psObj.BaseObject, psObj.BaseObject.GetType()); + return; + } var mappingStyle = this.useFlowStyle ? MappingStyle.Flow : MappingStyle.Block; emitter.Emit(new MappingStart(AnchorName.Empty, TagName.Empty, true, mappingStyle)); foreach (var prop in psObj.Properties) {