Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of PSCustomObject wrapped object #158

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@ Import-Module $modulePath
InModuleScope $moduleName {
$compareStrictly = Get-EquivalencyOption -Comparator Equality

Describe "Test PSCustomObject wrapped values are serialized correctly" {
Context "A PSCustomObject containing nested PSCustomObjects" {
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"]
}
}

Context "A hashtable containing nested PSCustomObjects" {
It "Should serialize correctly" {
$expectBigInt = [System.Numerics.BigInteger]::Parse("9999999999999999999999999999999999999999999999999")
$obj = @{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"]
}
}

Context "A generic dictionary containing nested PSCustomObjects" {
It "Should serialize correctly" {
$expectBigInt = [System.Numerics.BigInteger]::Parse("9999999999999999999999999999999999999999999999999")
$obj = [System.Collections.Generic.Dictionary[string, object]]::new()
$obj["a"] = Write-Output 'string'
$obj["b"] = Write-Output 1
$obj["c"] = Write-Output @{nested = $true}
$obj["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" {
Expand Down
Binary file modified lib/net47/PowerShellYamlSerializer.dll
Binary file not shown.
Binary file modified lib/netstandard2.1/PowerShellYamlSerializer.dll
Binary file not shown.
54 changes: 52 additions & 2 deletions src/PowerShellYamlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize
}
}

public class IDictionaryTypeConverter : IYamlTypeConverter {

private bool omitNullValues;

public IDictionaryTypeConverter(bool omitNullValues = false) {
this.omitNullValues = omitNullValues;
}

public bool Accepts(Type type) {
return typeof(IDictionary).IsAssignableFrom(type);
}

public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) {
var deserializedObject = rootDeserializer(typeof(IDictionary<string, object>)) as IDictionary;
return deserializedObject;
}

public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) {
var hObj = (IDictionary)value;
emitter.Emit(new MappingStart());
foreach (DictionaryEntry entry in hObj) {
if(entry.Value == null) {
if (this.omitNullValues == true) {
continue;
}
serializer(entry.Key, entry.Key.GetType());
emitter.Emit(new Scalar(AnchorName.Empty, "tag:yaml.org,2002:null", "", ScalarStyle.Plain, true, false));
continue;
}
serializer(entry.Key, entry.Key.GetType());
if (entry.Value is PSObject nestedObj) {
serializer(nestedObj.BaseObject, nestedObj.BaseObject.GetType());
} else {
serializer(entry.Value, entry.Value.GetType());
}
}
emitter.Emit(new MappingEnd());
}
}

public class PSObjectTypeConverter : IYamlTypeConverter {

private bool omitNullValues;
Expand All @@ -70,7 +110,6 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria

public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) {
var psObj = (PSObject)value;

emitter.Emit(new MappingStart());
foreach (var prop in psObj.Properties) {
if (prop.Value == null) {
Expand All @@ -81,7 +120,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());
Expand Down Expand Up @@ -151,6 +200,7 @@ public static SerializerBuilder BuildSerializer(
builder = builder
.WithEventEmitter(next => new StringQuotingEmitter(next))
.WithTypeConverter(new BigIntegerTypeConverter())
.WithTypeConverter(new IDictionaryTypeConverter(omitNullValues))
.WithTypeConverter(new PSObjectTypeConverter(omitNullValues));
if (omitNullValues == true) {
builder = builder
Expand Down