Don't serialize to json ETS properties for DateTime and string types (#15665)
This commit is contained in:
parent
6d09c6e40a
commit
adc236c598
|
@ -678,6 +678,12 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// <param name="context">The context for the operation.</param>
|
/// <param name="context">The context for the operation.</param>
|
||||||
private static void AppendPsProperties(PSObject psObj, IDictionary receiver, int depth, bool isCustomObject, in ConvertToJsonContext context)
|
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..
|
// serialize only Extended and Adapted properties..
|
||||||
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
|
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
|
||||||
new PSMemberInfoIntegratingCollection<PSPropertyInfo>(psObj,
|
new PSMemberInfoIntegratingCollection<PSPropertyInfo>(psObj,
|
||||||
|
|
|
@ -131,4 +131,19 @@ Describe 'ConvertTo-Json' -tags "CI" {
|
||||||
$p2.psobject.Properties.Remove('nullstr')
|
$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`""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue