diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index 94ba896..dcdac17 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -725,9 +725,17 @@ int64: 9223372036854775807 $nestedPsO = [PSCustomObject]@{ Nested = 'NestedValue' } + $nestedHashTable = @{ + "aKey" = $nestedPsO + } + $nestedArray = @( + $nestedPsO + ) $PsO = [PSCustomObject]@{ Name = 'Value' Nested = $nestedPsO + NestedHashTable = $nestedHashTable + NestedArray = $nestedArray NullValue = $null } @@ -746,6 +754,16 @@ int64: 9223372036854775807 $ret["PsO"]["Name"] = "Value" $ret["PsO"]["Nested"] = [System.Collections.Specialized.OrderedDictionary]::new() $ret["PsO"]["Nested"]["Nested"] = "NestedValue" + $ret["PsO"]["NestedHashTable"] = [ordered]@{ + "aKey" = [ordered]@{ + "Nested" = "NestedValue" + } + } + $ret["PsO"]["NestedArray"] = @( + [ordered]@{ + "Nested" = "NestedValue" + } + ) $ret["PsO"]["NullValue"] = $null $ret["Ok"] = "aye" Assert-Equivalent -Options $compareStrictly -Expected $ret -Actual $result diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index 24af47d..ad5b86b 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 f1b8efb..f126f2c 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 6bb4063..09b0616 100644 --- a/src/PowerShellYamlSerializer.cs +++ b/src/PowerShellYamlSerializer.cs @@ -84,8 +84,15 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize continue; } serializer(entry.Key, entry.Key.GetType()); + var objType = entry.Value.GetType(); + var val = entry.Value; if (entry.Value is PSObject nestedObj) { - serializer(nestedObj.BaseObject, nestedObj.BaseObject.GetType()); + var nestedType = nestedObj.BaseObject.GetType(); + if (nestedType != typeof(System.Management.Automation.PSCustomObject)) { + objType = nestedObj.BaseObject.GetType(); + val = nestedObj.BaseObject; + } + serializer(val, objType); } else { serializer(entry.Value, entry.Value.GetType()); }