Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Management.Automation.Language;
using System.Numerics;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;

using Newtonsoft.Json;
Expand Down Expand Up @@ -289,11 +288,11 @@ private static PSObject PopulateFromJDictionary(JObject entries, DuplicateMember
return null;
}

// Array
switch (entry.Value)
{
case JArray list:
{
// Array
var listResult = PopulateFromJArray(list, out error);
if (error != null)
{
Expand Down Expand Up @@ -333,45 +332,44 @@ private static ICollection<object> PopulateFromJArray(JArray list, out ErrorReco
{
error = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daxian-dbw Just curious, why move error initialization up here? Imo initializing it at the beginning can potentially hide issues where you forget to set error before return, while if it was only assigned directly before the return, compiler would throw an error about a potentially uninitialized variable.

var result = new object[list.Count];
var i = 0;

for (var index = 0; index < list.Count; index++)
foreach (var element in list)
{
var element = list[index];
switch (element)
{
case JArray subList:
// Array
result[i++] = PopulateFromJArray(subList, out error);
if (error != null)
{
// Array
var listResult = PopulateFromJArray(subList, out error);
if (error != null)
{
return null;
}

result[index] = listResult;
break;
return null;
}

break;

case JObject dic:
// Dictionary
result[i++] = PopulateFromJDictionary(dic, new DuplicateMemberHashSet(dic.Count), out error);
if (error != null)
{
// Dictionary
var dicResult = PopulateFromJDictionary(dic, new DuplicateMemberHashSet(dic.Count), out error);
if (error != null)
{
return null;
}

result[index] = dicResult;
break;
return null;
}

break;

case JValue value:
if (value.Type != JTokenType.Comment)
{
result[index] = value.Value;
break;
result[i++] = value.Value;
}

break;
}
}

return result;
// In the common case of not having any comments, return the original array, otherwise create a sliced copy.
return i == list.Count ? result : result[..i];
}

// This function is a clone of PopulateFromDictionary using JObject as an input.
Expand Down Expand Up @@ -436,46 +434,44 @@ private static ICollection<object> PopulateHashTableFromJArray(JArray list, out
{
error = null;
var result = new object[list.Count];
var i = 0;

for (var index = 0; index < list.Count; index++)
foreach (var element in list)
{
var element = list[index];

switch (element)
{
case JArray array:
case JArray subList:
// Array
result[i++] = PopulateHashTableFromJArray(subList, out error);
if (error != null)
{
// Array
var listResult = PopulateHashTableFromJArray(array, out error);
if (error != null)
{
return null;
}

result[index] = listResult;
break;
return null;
}

break;

case JObject dic:
// Dictionary
result[i++] = PopulateHashTableFromJDictionary(dic, out error);
if (error != null)
{
// Dictionary
var dicResult = PopulateHashTableFromJDictionary(dic, out error);
if (error != null)
{
return null;
}

result[index] = dicResult;
break;
return null;
}

break;

case JValue value:
if (value.Type != JTokenType.Comment)
{
result[index] = value.Value;
break;
result[i++] = value.Value;
}

break;
}
}

return result;
// In the common case of not having any comments, return the original array, otherwise create a sliced copy.
return i == list.Count ? result : result[..i];
}

#endregion ConvertFromJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatejKafka Please remove. We never use references to GitHub in tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think there is no harm to have this reference in test :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful if it were a tracking issue for a disabled test. In this form, it's just garbage.

'[
// 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" {
Expand Down
Loading