mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Remove workaround for fixed invalid json array deserializing bug (#8346)
This commit is contained in:
@@ -59,22 +59,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
error = null;
|
||||
try
|
||||
{
|
||||
// JsonConvert.DeserializeObject does not throw an exception when an invalid Json array is passed.
|
||||
// This issue is being tracked by https://github.com/JamesNK/Newtonsoft.Json/issues/1321.
|
||||
// To work around this, we need to identify when input is a Json array, and then try to parse it via JArray.Parse().
|
||||
|
||||
// If input starts with '[' (ignoring white spaces).
|
||||
if (Regex.Match(input, @"^\s*\[").Success)
|
||||
{
|
||||
// JArray.Parse() will throw a JsonException if the array is invalid.
|
||||
// This will be caught by the catch block below, and then throw an
|
||||
// ArgumentException - this is done to have same behavior as the JavaScriptSerializer.
|
||||
JArray.Parse(input);
|
||||
|
||||
// Please note that if the Json array is valid, we don't do anything,
|
||||
// we just continue the deserialization.
|
||||
}
|
||||
|
||||
var obj = JsonConvert.DeserializeObject(
|
||||
input,
|
||||
new JsonSerializerSettings
|
||||
|
||||
@@ -52,4 +52,10 @@ Describe 'ConvertFrom-Json' -tags "CI" {
|
||||
$json | Should -BeOfType Hashtable
|
||||
}
|
||||
}
|
||||
|
||||
It 'Throws an ArgumentException with an incomplete array with AsHashtable switch set to <AsHashtable>' -TestCase $testCasesWithAndWithoutAsHashtableSwitch {
|
||||
Param($AsHashtable)
|
||||
{ ConvertFrom-Json '["1",' -AsHashtable:$AsHashtable } |
|
||||
Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user