From fcf4953c3b410133520b9688422ddef93168f634 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 27 Feb 2024 22:52:58 +0000 Subject: [PATCH] ConvertTo-Json: Serialize `BigInteger` as a number (#21000) --- .../commands/utility/WebCmdlet/JsonObject.cs | 4 +++- .../ConvertTo-Json.Tests.ps1 | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs index 644ed5e0f7..8e0e7e7776 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs @@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Management.Automation; using System.Management.Automation.Language; +using System.Numerics; using System.Reflection; using System.Text.RegularExpressions; using System.Threading; @@ -563,7 +564,8 @@ namespace Microsoft.PowerShell.Commands || obj is Uri || obj is double || obj is float - || obj is decimal) + || obj is decimal + || obj is BigInteger) { rv = obj; } 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 c549975b32..1f2abe05c6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -146,4 +146,14 @@ Describe 'ConvertTo-Json' -tags "CI" { $t = Add-Member -InputObject $text -MemberType NoteProperty -Name text -Value $text -PassThru $t | ConvertTo-Json -Compress | Should -BeExactly "`"$text`"" } + + It 'Should serialize BigInteger values' { + $obj = [Ordered]@{ + Positive = 18446744073709551615n + Negative = -18446744073709551615n + } + + $actual = ConvertTo-Json -Compress -InputObject $obj + $actual | Should -Be '{"Positive":18446744073709551615,"Negative":-18446744073709551615}' + } }