From 6c8a6ae4ba7c460048bb18b814abf118f40bb060 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Tue, 20 Mar 2018 09:29:22 -0700 Subject: [PATCH 1/4] Add -AsArray parameter to ConvertoTo-Json command to always pack the output string in array symbols. --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 11 ++++++++++- .../ConvertTo-Json.Tests.ps1 | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index d6431fd3ff6..eb97803b24a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -64,6 +64,15 @@ public class ConvertToJsonCommand : PSCmdlet [Parameter()] public SwitchParameter EnumsAsStrings { get; set; } + /// + /// gets or sets the AsArray property. + /// If the AsArray property is set to be true, the result Json string will + /// be returned with surrounding '[', ']' chars. Otherwise, + /// the array symbols will occur only if there is more than one input object. + /// + [Parameter] + public SwitchParameter AsArray { get; set; } + #endregion parameters #region overrides @@ -104,7 +113,7 @@ protected override void EndProcessing() { if (_inputObjects.Count > 0) { - object objectToProcess = (_inputObjects.Count > 1) ? (_inputObjects.ToArray() as object) : (_inputObjects[0]); + object objectToProcess = (_inputObjects.Count > 1 || AsArray) ? (_inputObjects.ToArray() as object) : (_inputObjects[0]); // Pre-process the object so that it serializes the same, except that properties whose // values cannot be evaluated are treated as having the value null. object preprocessedObject = null; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 8e9336a51f6..bb439d1adaa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -35,4 +35,17 @@ Describe 'ConvertTo-Json' -tags "CI" { $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" $ps.Dispose() } + + It "The result string is packed in an array symbols when AsArray parameter is used." { + $output = ConvertTo-Json -InputObject 1 -AsArray + $output | Should -BeLike "``[*1*]" + + $output = ConvertTo-Json -InputObject 1,2 -AsArray + $output | Should -BeLike "``[*1*2*]" + } + + It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." { + $output = ConvertTo-Json -InputObject 1 + $output | Should -Be '1' + } } From d527cba687c228d23c1560f3f85ae7d568f302d0 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Tue, 20 Mar 2018 15:16:41 -0700 Subject: [PATCH 2/4] Tests correction for -AsArray parameter in ConvertTo-Json command. --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index bb439d1adaa..d3c023ec12c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -37,15 +37,15 @@ Describe 'ConvertTo-Json' -tags "CI" { } It "The result string is packed in an array symbols when AsArray parameter is used." { - $output = ConvertTo-Json -InputObject 1 -AsArray + $output = 1 | ConvertTo-Json -AsArray $output | Should -BeLike "``[*1*]" - $output = ConvertTo-Json -InputObject 1,2 -AsArray + $output = 1,2 | ConvertTo-Json -AsArray $output | Should -BeLike "``[*1*2*]" } It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." { - $output = ConvertTo-Json -InputObject 1 + $output = 1 | ConvertTo-Json $output | Should -Be '1' } } From e8e7bda8f454760ea980c63a86c51f0e276ac88c Mon Sep 17 00:00:00 2001 From: kalgiz Date: Wed, 21 Mar 2018 12:58:01 -0700 Subject: [PATCH 3/4] ConvertToJson command code cleanup --- .../commands/utility/WebCmdlet/ConvertToJsonCommand.cs | 4 ++-- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index eb97803b24a..7eff25f2e7b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -61,11 +61,11 @@ public class ConvertToJsonCommand : PSCmdlet /// be converted to their string equivalent. Otherwise, enum values /// will be converted to their numeric equivalent. /// - [Parameter()] + [Parameter] public SwitchParameter EnumsAsStrings { get; set; } /// - /// gets or sets the AsArray property. + /// Gets or sets the AsArray property. /// If the AsArray property is set to be true, the result Json string will /// be returned with surrounding '[', ']' chars. Otherwise, /// the array symbols will occur only if there is more than one input object. diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index d3c023ec12c..4fb734556f7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -46,6 +46,6 @@ Describe 'ConvertTo-Json' -tags "CI" { It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." { $output = 1 | ConvertTo-Json - $output | Should -Be '1' + $output | Should -BeExactly '1' } } From 9044d32d2d2e37406809d3d0ba2bae0c3a789ae7 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Thu, 22 Mar 2018 09:49:20 -0700 Subject: [PATCH 4/4] Comment correction. --- .../commands/utility/WebCmdlet/ConvertToJsonCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 7eff25f2e7b..b1a3cbac05d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -66,7 +66,7 @@ public class ConvertToJsonCommand : PSCmdlet /// /// Gets or sets the AsArray property. - /// If the AsArray property is set to be true, the result Json string will + /// If the AsArray property is set to be true, the result JSON string will /// be returned with surrounding '[', ']' chars. Otherwise, /// the array symbols will occur only if there is more than one input object. ///