mirror of
https://github.com/PowerShell/PowerShell.git
synced 2024-11-23 01:34:19 +08:00
Treat large Enum values as numbers in ConvertTo-Json
(#20999)
This commit is contained in:
parent
805e621517
commit
3e3d83cfa4
@ -2,6 +2,7 @@
|
||||
"PSFeedbackProvider",
|
||||
"PSLoadAssemblyFromNativeCode",
|
||||
"PSNativeWindowsTildeExpansion",
|
||||
"PSSerializeJSONLongEnumAsNumber",
|
||||
"PSRedirectToVariable",
|
||||
"PSSubsystemPluginModel"
|
||||
]
|
||||
|
@ -2,6 +2,7 @@
|
||||
"PSFeedbackProvider",
|
||||
"PSLoadAssemblyFromNativeCode",
|
||||
"PSNativeWindowsTildeExpansion",
|
||||
"PSSerializeJSONLongEnumAsNumber",
|
||||
"PSRedirectToVariable",
|
||||
"PSSubsystemPluginModel"
|
||||
]
|
||||
|
@ -577,7 +577,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
Type t = obj.GetType();
|
||||
|
||||
if (t.IsPrimitive)
|
||||
if (t.IsPrimitive || (t.IsEnum && ExperimentalFeature.IsEnabled(ExperimentalFeature.PSSerializeJSONLongEnumAsNumber)))
|
||||
{
|
||||
rv = obj;
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ namespace System.Management.Automation
|
||||
internal const string PSFeedbackProvider = "PSFeedbackProvider";
|
||||
internal const string PSNativeWindowsTildeExpansion = nameof(PSNativeWindowsTildeExpansion);
|
||||
internal const string PSRedirectToVariable = "PSRedirectToVariable";
|
||||
internal const string PSSerializeJSONLongEnumAsNumber = nameof(PSSerializeJSONLongEnumAsNumber);
|
||||
|
||||
#endregion
|
||||
|
||||
@ -121,6 +122,10 @@ namespace System.Management.Automation
|
||||
new ExperimentalFeature(
|
||||
name: PSRedirectToVariable,
|
||||
description: "Add support for redirecting to the variable drive"),
|
||||
new ExperimentalFeature(
|
||||
name: PSSerializeJSONLongEnumAsNumber,
|
||||
description: "Serialize enums based on long or ulong as an numeric value rather than the string representation when using ConvertTo-Json."
|
||||
)
|
||||
};
|
||||
|
||||
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);
|
||||
|
@ -0,0 +1,34 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
Describe 'ConvertTo-Json with PSSerializeJSONLongEnumAsNumber' -tags "CI" {
|
||||
|
||||
BeforeAll {
|
||||
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
|
||||
$PSDefaultParameterValues['It:Skip'] = -not [ExperimentalFeature]::IsEnabled('PSSerializeJSONLongEnumAsNumber')
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
$global:PSDefaultParameterValues = $originalDefaultParameterValues
|
||||
}
|
||||
|
||||
It 'Should treat enums as integers' {
|
||||
enum LongEnum : long {
|
||||
LongValue = -1
|
||||
}
|
||||
|
||||
enum ULongEnum : ulong {
|
||||
ULongValue = 18446744073709551615
|
||||
}
|
||||
|
||||
$obj = [Ordered]@{
|
||||
Long = [LongEnum]::LongValue
|
||||
ULong = [ULongEnum]::ULongValue
|
||||
}
|
||||
|
||||
$actual = ConvertTo-Json -InputObject $obj -Compress
|
||||
$actual | Should -Be '{"Long":-1,"ULong":18446744073709551615}'
|
||||
|
||||
$actual = ConvertTo-Json -InputObject $obj -EnumsAsStrings -Compress
|
||||
$actual | Should -Be '{"Long":"LongValue","ULong":"ULongValue"}'
|
||||
}
|
||||
}
|
@ -58,7 +58,9 @@ Describe "Json Tests" -Tags "Feature" {
|
||||
$valueFromNotCompressedResult.FirstName | Should -Match $valueFromCompressedResult.FirstName
|
||||
}
|
||||
|
||||
It "Convertto-Json should handle Enum based on Int64" {
|
||||
It "Convertto-Json should handle Enum based on Int64" -Skip:(
|
||||
[ExperimentalFeature]::IsEnabled("PSSerializeJSONLongEnumAsNumber")
|
||||
) {
|
||||
|
||||
# Test follow-up for bug Win8: 378368 Convertto-Json problems with Enum based on Int64.
|
||||
if ( $null -eq ("JsonEnumTest" -as "Type")) {
|
||||
|
@ -3,6 +3,7 @@
|
||||
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ],
|
||||
"PSCultureInvariantReplaceOperator": [ "test/powershell/Language/Operators/ReplaceOperator.Tests.ps1" ],
|
||||
"Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace": [ "test/powershell/Modules/Microsoft.PowerShell.Utility/RunspaceBreakpointManagement.Tests.ps1" ],
|
||||
"PSNativeWindowsTildeExpansion": [ "test/powershell/Language/Scripting/NativeExecution/NativeWindowsTildeExpansion.Tests.ps1" ]
|
||||
"PSNativeWindowsTildeExpansion": [ "test/powershell/Language/Scripting/NativeExecution/NativeWindowsTildeExpansion.Tests.ps1" ],
|
||||
"PSSerializeJSONLongEnumAsNumber": [ "test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.PSSerializeJSONLongEnumAsNumber.Tests.ps1" ]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user