diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs
index 3556c1ac39c..6433e3dc614 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs
@@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
-using System.Reflection;
namespace Microsoft.PowerShell.Commands
{
@@ -43,6 +42,13 @@ public class ConvertFromJsonCommand : Cmdlet
[ValidateRange(ValidateRangeKind.Positive)]
public int Depth { get; set; } = 1024;
+ ///
+ /// Gets or sets the switch to prevent ConvertFrom-Json from unravelling collections during deserialization, instead passing them as a single
+ /// object through the pipeline.
+ ///
+ [Parameter]
+ public SwitchParameter NoEnumerate { get; set; }
+
#endregion parameters
#region overrides
@@ -114,7 +120,7 @@ private bool ConvertFromJsonHelper(string input)
ThrowTerminatingError(error);
}
- WriteObject(result);
+ WriteObject(result, !NoEnumerate.IsPresent);
return (result != null);
}
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1
index f71ac4dcb94..d400dbc6b9f 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1
@@ -37,6 +37,10 @@ Describe 'ConvertFrom-Json Unit Tests' -tags "CI" {
@{ AsHashtable = $true }
@{ AsHashtable = $false }
)
+ $testCasesWithAndWithoutNoEnumerateSwitch = @(
+ @{ NoEnumerate = $true }
+ @{ NoEnumerate = $false }
+ )
}
It 'Can convert a single-line object with AsHashtable switch set to ' -TestCases $testCasesWithAndWithoutAsHashtableSwitch {
@@ -107,6 +111,27 @@ Describe 'ConvertFrom-Json Unit Tests' -tags "CI" {
Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
}
+ It 'Can correctly round trip arrays with NoEnumerate switch set to ' -TestCases $testCasesWithAndWithoutNoEnumerateSwitch {
+ Param($NoEnumerate)
+ '[ 1, 2 ]' | ConvertFrom-Json -NoEnumerate:$NoEnumerate | ConvertTo-Json -Compress | Should -Be '[1,2]'
+ }
+
+ It 'Unravels array elements when NoEnumerate switch is not set' {
+ ('[ 1, 2 ]' | ConvertFrom-Json | Measure-Object).Count | Should -Be 2
+ }
+
+ It 'Sends a Json array as a single element when NoEnumerate switch is set' {
+ ('[ 1, 2 ]' | ConvertFrom-Json -NoEnumerate | Measure-Object).Count | Should -Be 1
+ }
+
+ It 'Cannot round trip single element arrays without NoEnumerate switch' {
+ '[ 1 ]' | ConvertFrom-Json | ConvertTo-Json | Should -Be 1
+ }
+
+ It 'Can round trip single element arrays with NoEnumerate switch' {
+ '[ 1 ]' | ConvertFrom-Json -NoEnumerate | ConvertTo-Json -Compress | Should -Be '[1]'
+ }
+
It 'Can convert null' {
'null' | ConvertFrom-Json | Should -Be $null
$out = '[1, null, 2]' | ConvertFrom-Json