diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs index 06c050be70..37ec46f92f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs @@ -73,7 +73,14 @@ namespace Microsoft.PowerShell.Commands // we just continue the deserialization. } - obj = JsonConvert.DeserializeObject(input, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }); + obj = JsonConvert.DeserializeObject( + input, + new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.None, + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, + MaxDepth = 1024 + }); // JObject is a IDictionary var dictionary = obj as JObject; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 index 97ae068d6b..e4a32a5ee8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. Describe 'ConvertFrom-Json' -tags "CI" { - + BeforeAll { $testCasesWithAndWithoutAsHashtableSwitch = @( @{ AsHashtable = $true } @@ -34,4 +34,22 @@ Describe 'ConvertFrom-Json' -tags "CI" { $json | Should -BeOfType Hashtable } } + + It 'Can convert an object with Newtonsoft.Json metadata properties with AsHashtable switch set to ' -TestCase $testCasesWithAndWithoutAsHashtableSwitch { + Param($AsHashtable) + $id = 13 + $type = 'Calendar.Months.December' + $ref = 1989 + + $json = '{"$id":' + $id + ', "$type":"' + $type + '", "$ref":' + $ref + '}' | ConvertFrom-Json -AsHashtable:$AsHashtable + + $json.'$id' | Should -Be $id + $json.'$type' | Should -Be $type + $json.'$ref' | Should -Be $ref + + if ($AsHashtable) + { + $json | Should -BeOfType Hashtable + } + } }