mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user