Skip to content

Commit 518a479

Browse files
daxian-dbwAndrew
authored andcommitted
Make ConvertTo-Json treat [AutomationNull]::Value and [NullString]::Value as $null (PowerShell#10957)
1 parent f1b96eb commit 518a479

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics.CodeAnalysis;
88
using System.Globalization;
99
using System.Management.Automation;
10+
using System.Management.Automation.Language;
1011
using System.Reflection;
1112
using System.Text.RegularExpressions;
1213
using System.Threading;
@@ -496,6 +497,11 @@ private static object ProcessValue(object obj, int currentDepth, in ConvertToJso
496497
{
497498
context.CancellationToken.ThrowIfCancellationRequested();
498499

500+
if (LanguagePrimitives.IsNull(obj))
501+
{
502+
return null;
503+
}
504+
499505
PSObject pso = obj as PSObject;
500506

501507
if (pso != null)
@@ -507,18 +513,21 @@ private static object ProcessValue(object obj, int currentDepth, in ConvertToJso
507513
bool isPurePSObj = false;
508514
bool isCustomObj = false;
509515

510-
if (obj == null
511-
|| DBNull.Value.Equals(obj)
512-
|| obj is string
513-
|| obj is char
514-
|| obj is bool
515-
|| obj is DateTime
516-
|| obj is DateTimeOffset
517-
|| obj is Guid
518-
|| obj is Uri
519-
|| obj is double
520-
|| obj is float
521-
|| obj is decimal)
516+
if (obj == NullString.Value
517+
|| obj == DBNull.Value)
518+
{
519+
rv = null;
520+
}
521+
else if (obj is string
522+
|| obj is char
523+
|| obj is bool
524+
|| obj is DateTime
525+
|| obj is DateTimeOffset
526+
|| obj is Guid
527+
|| obj is Uri
528+
|| obj is double
529+
|| obj is float
530+
|| obj is decimal)
522531
{
523532
rv = obj;
524533
}

test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,38 @@ Describe 'ConvertTo-Json' -tags "CI" {
6969
ConvertTo-Json -Compress $null | Should -Be 'null'
7070
1, $null, 2 | ConvertTo-Json -Compress | Should -Be '[1,null,2]'
7171
}
72+
73+
It "Should handle 'AutomationNull.Value' and 'NullString.Value' correctly" {
74+
[ordered]@{
75+
a = $null;
76+
b = [System.Management.Automation.Internal.AutomationNull]::Value;
77+
c = [System.DBNull]::Value;
78+
d = [NullString]::Value
79+
} | ConvertTo-Json -Compress | Should -BeExactly '{"a":null,"b":null,"c":null,"d":null}'
80+
81+
ConvertTo-Json ([System.Management.Automation.Internal.AutomationNull]::Value) | Should -BeExactly 'null'
82+
ConvertTo-Json ([NullString]::Value) | Should -BeExactly 'null'
83+
84+
ConvertTo-Json -Compress @(
85+
$null,
86+
[System.Management.Automation.Internal.AutomationNull]::Value,
87+
[System.DBNull]::Value,
88+
[NullString]::Value
89+
) | Should -BeExactly '[null,null,null,null]'
90+
}
91+
92+
It "Should handle the ETS properties added to 'DBNull.Value' and 'NullString.Value'" {
93+
try
94+
{
95+
$p1 = Add-Member -InputObject ([System.DBNull]::Value) -MemberType NoteProperty -Name dbnull -Value 'dbnull' -PassThru
96+
$p2 = Add-Member -InputObject ([NullString]::Value) -MemberType NoteProperty -Name nullstr -Value 'nullstr' -PassThru
97+
98+
$p1, $p2 | ConvertTo-Json -Compress | Should -BeExactly '[{"value":null,"dbnull":"dbnull"},{"value":null,"nullstr":"nullstr"}]'
99+
}
100+
finally
101+
{
102+
$p1.psobject.Properties.Remove('dbnull')
103+
$p2.psobject.Properties.Remove('nullstr')
104+
}
105+
}
72106
}

0 commit comments

Comments
 (0)