diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index db55a59..0c21647 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -28,6 +28,21 @@ Import-Module $modulePath InModuleScope $moduleName { $compareStrictly = Get-EquivalencyOption -Comparator Equality + Describe "Test PSCustomObject wrapped values are serialized correctly" { + Context "Wrapped values" { + It "Should serialize correctly" { + $expectBigInt = [System.Numerics.BigInteger]::Parse("9999999999999999999999999999999999999999999999999") + $obj = [PSCustomObject]@{a = Write-Output 'string'; b = Write-Output 1; c = Write-Output @{nested = $true};d = [pscustomobject]$expectBigInt} + $asYaml = ConvertTo-Yaml $obj + $fromYaml = ConvertFrom-Yaml $asYaml + + Assert-Equivalent -Options $compareStrictly -Expected "string" -Actual $fromYaml["a"] + Assert-Equivalent -Options $compareStrictly -Expected 1 -Actual $fromYaml["b"] + Assert-Equivalent -Options $compareStrictly -Expected $expectBigInt -Actual $fromYaml["d"] + } + } + } + Describe "Test encode-decode symmetry." { Context "Simple-Items" { diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index 5adca39..e0d04d9 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 8f4f602..cd9dc97 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 e3b27bf..2f0b4c3 100644 --- a/src/PowerShellYamlSerializer.cs +++ b/src/PowerShellYamlSerializer.cs @@ -81,7 +81,17 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize emitter.Emit(new Scalar(AnchorName.Empty, "tag:yaml.org,2002:null", "", ScalarStyle.Plain, true, false)); } else { serializer(prop.Name, prop.Name.GetType()); - serializer(prop.Value, prop.Value.GetType()); + var objType = prop.Value.GetType(); + var val = prop.Value; + if (prop.Value is PSObject nestedPsObj) { + var nestedType = nestedPsObj.BaseObject.GetType(); + if (nestedType != typeof(System.Management.Automation.PSCustomObject)) { + objType = nestedPsObj.BaseObject.GetType(); + val = nestedPsObj.BaseObject; + } + } + serializer(val, objType); + } } emitter.Emit(new MappingEnd());