Ignore Newtonsoft.Json metadata properties in ConvertFrom-Json (#7308)

Some values are not converted from json to PSCustomObject, because Newtonsoft.Json reads them and thinks they are metadata properties only useful for deserialization. This change makes Newtonsoft.Json "ignore" those properties so they are converted correctly from json to PSCustomObject.
This commit is contained in:
Adam Gauthier
2018-07-25 08:56:49 +05:00
committed by Ilya
parent f5a7d79175
commit e34554c3ca
2 changed files with 27 additions and 2 deletions
@@ -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;
@@ -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 <AsHashtable>' -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
}
}
}