Fix ConvertFrom-Json to ignore comments inside array literals (#14553) (#26050)

Co-authored-by: Dongbo Wang <dongbow@microsoft.com>
This commit is contained in:
Matěj Kafka
2025-09-23 10:29:11 -07:00
committed by GitHub
co-authored by Dongbo Wang
parent 506869c157
commit 551a0fe642
2 changed files with 78 additions and 50 deletions
@@ -350,6 +350,38 @@ c 3
$json | Should -BeOfType ([string])
$json | Should -Be $Value.Substring(1, $Value.Length - 2)
}
It 'Ignores comments in arrays' -TestCase $testCasesWithAndWithoutAsHashtableSwitch {
param($AsHashtable)
# https://github.com/powerShell/powerShell/issues/14553
'[
// comment
100,
/* comment */
200
]' | ConvertFrom-Json -AsHashtable:$AsHashtable | Should -Be @(100, 200)
}
It 'Ignores comments in dictionaries' -TestCase $testCasesWithAndWithoutAsHashtableSwitch {
param($AsHashtable)
$json = '{
// comment
"a": 100,
/* comment */
"b": 200
}' | ConvertFrom-Json -AsHashtable:$AsHashtable
if ($AsHashtable) {
$json.Keys | Should -Be @("a", "b")
} else {
$json.psobject.Properties | Should -HaveCount 2
}
$json.a | Should -Be 100
$json.b | Should -Be 200
}
}
Describe 'ConvertFrom-Json -Depth Tests' -tags "Feature" {