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

Pass non mapping to base serializer #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
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.
4 changes: 4 additions & 0 deletions src/PowerShellYamlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down