From 70ab772da77d8cc1cbec2e3aa29bd328ff1de589 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 1 Nov 2019 11:54:31 -0700 Subject: [PATCH] Support $null for convertto-json (#10947) ConvertTo-Json doesn't allow $null to be passed in even though it can be valid resulting json. Fix is to remove the check that the input is not null. NewtonSoft.Json handles it just fine. --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 5 +---- .../ConvertFrom-Json.Tests.ps1 | 11 +++++++++++ .../ConvertTo-Json.Tests.ps1 | 7 +++++++ 3 files changed, 19 insertions(+), 4 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 2de53f3bee..5d1cf23580 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -98,10 +98,7 @@ namespace Microsoft.PowerShell.Commands /// protected override void ProcessRecord() { - if (InputObject != null) - { - _inputObjects.Add(InputObject); - } + _inputObjects.Add(InputObject); } /// 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 8160864dd3..f71ac4dcb9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 @@ -106,6 +106,17 @@ Describe 'ConvertFrom-Json Unit Tests' -tags "CI" { { $nestedJson | ConvertFrom-Json -AsHashtable:$AsHashtable } | Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand" } + + It 'Can convert null' { + 'null' | ConvertFrom-Json | Should -Be $null + $out = '[1, null, 2]' | ConvertFrom-Json + $out.Length | Should -Be 3 + + # can't compare directly to array as Pester doesn't handle the $null + $out[0] | Should -Be 1 + $out[1] | Should -Be $null + $out[2] | Should -Be 2 + } } Describe 'ConvertFrom-Json -Depth Tests' -tags "Feature" { 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 54c92de858..371aff5b9c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -62,4 +62,11 @@ Describe 'ConvertTo-Json' -tags "CI" { @{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected } + + It "Should handle null" { + [pscustomobject] @{ prop=$null } | ConvertTo-Json -Compress | Should -BeExactly '{"prop":null}' + $null | ConvertTo-Json -Compress | Should -Be 'null' + ConvertTo-Json -Compress $null | Should -Be 'null' + 1, $null, 2 | ConvertTo-Json -Compress | Should -Be '[1,null,2]' + } }