Skip to content

Commit ec0dba4

Browse files
authored
Remove pre-parse for array-based JSON (PowerShell#15684)
1 parent 3d4e294 commit ec0dba4

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,6 @@ public static object ConvertFromJson(string input, bool returnHashtable, int? ma
160160
error = null;
161161
try
162162
{
163-
// JsonConvert.DeserializeObject does not throw an exception when an invalid Json array is passed.
164-
// This issue is being tracked by https://github.com/JamesNK/Newtonsoft.Json/issues/1930.
165-
// To work around this, we need to identify when input is a Json array, and then try to parse it via JArray.Parse().
166-
167-
// If input starts with '[' (ignoring white spaces).
168-
if (Regex.Match(input, @"^\s*\[").Success)
169-
{
170-
// JArray.Parse() will throw a JsonException if the array is invalid.
171-
// This will be caught by the catch block below, and then throw an
172-
// ArgumentException - this is done to have same behavior as the JavaScriptSerializer.
173-
JArray.Parse(input);
174-
175-
// Please note that if the Json array is valid, we don't do anything,
176-
// we just continue the deserialization.
177-
}
178-
179163
var obj = JsonConvert.DeserializeObject(
180164
input,
181165
new JsonSerializerSettings

test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ Describe 'ConvertFrom-Json Unit Tests' -tags "CI" {
5858
$json | Should -BeOfType Hashtable
5959
}
6060
}
61-
61+
62+
It 'Throws an ArgumentException with an incomplete array with AsHashtable switch set to <AsHashtable>' -TestCase $testCasesWithAndWithoutAsHashtableSwitch {
63+
Param($AsHashtable)
64+
{ ConvertFrom-Json '["1",' -AsHashtable:$AsHashtable } |
65+
Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
66+
{ ConvertFrom-Json '[' -AsHashtable:$AsHashtable } |
67+
Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
68+
}
69+
6270
It 'Can convert multi-line object with AsHashtable switch set to <AsHashtable>' -TestCases $testCasesWithAndWithoutAsHashtableSwitch {
6371
Param($AsHashtable)
6472
$json = @('{"a" :', '"x"}') | ConvertFrom-Json -AsHashtable:$AsHashtable

0 commit comments

Comments
 (0)