Don't serialize to json ETS properties for DateTime and string types (#15665)

This commit is contained in:
Steve Lee 2021-07-12 10:56:33 -07:00 committed by GitHub
parent 6d09c6e40a
commit adc236c598
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View file

@ -678,6 +678,12 @@ namespace Microsoft.PowerShell.Commands
/// <param name="context">The context for the operation.</param>
private static void AppendPsProperties(PSObject psObj, IDictionary receiver, int depth, bool isCustomObject, in ConvertToJsonContext context)
{
// if the psObj is a DateTime or String type, we don't serialize any extended or adapted properties
if (psObj.BaseObject is string || psObj.BaseObject is DateTime)
{
return;
}
// serialize only Extended and Adapted properties..
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
new PSMemberInfoIntegratingCollection<PSPropertyInfo>(psObj,

View file

@ -131,4 +131,19 @@ Describe 'ConvertTo-Json' -tags "CI" {
$p2.psobject.Properties.Remove('nullstr')
}
}
It 'Should not serialize ETS properties added to DateTime' {
$date = "2021-06-24T15:54:06.796999-07:00"
$d = [DateTime]::Parse($date)
# need to use wildcard here due to some systems may be configured with different culture setting showing time in different format
$d | ConvertTo-Json -Compress | Should -BeLike '"2021-06-24T*'
$d | ConvertTo-Json | ConvertFrom-Json | Should -Be $d
}
It 'Should not serialize ETS properties added to String' {
$text = "Hello there"
$t = Add-Member -InputObject $text -MemberType NoteProperty -Name text -Value $text -PassThru
$t | ConvertTo-Json -Compress | Should -BeExactly "`"$text`""
}
}