Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,12 @@ private static object AddPsProperties(object psObj, object obj, int depth, bool
/// <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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`""
}
}