From 02c4d34380a780773fef458c742976a4f8a096ff Mon Sep 17 00:00:00 2001 From: Kirk Munro Date: Wed, 16 Aug 2017 13:57:30 -0300 Subject: [PATCH] Add support for converting enums as strings in JSON (#4318) * converting enums as strings support * removed !CORECLR code per @SteveL-MSFT's request --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 14 ++++++++++++++ .../Pester.Commands.Cmdlets.Json.Tests.ps1 | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) 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 eef7da897..7feb75ce8 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -14,6 +14,7 @@ using Dbg = System.Management.Automation; using System.Management.Automation.Internal; #if CORECLR using Newtonsoft.Json; +using Newtonsoft.Json.Converters; #else using System.Collections.Specialized; using System.Web.Script.Serialization; @@ -60,6 +61,15 @@ namespace Microsoft.PowerShell.Commands [Parameter] public SwitchParameter Compress { get; set; } + /// + /// gets or sets the EnumsAsStrings property. + /// If the EnumsAsStrings property is set to true, enum values will + /// be converted to their string equivalent. Otherwise, enum values + /// will be converted to their numeric equivalent. + /// + [Parameter()] + public SwitchParameter EnumsAsStrings { get; set; } + #endregion parameters #region overrides @@ -122,6 +132,10 @@ namespace Microsoft.PowerShell.Commands object preprocessedObject = ProcessValue(objectToProcess, 0); #if CORECLR JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }; + if (EnumsAsStrings) + { + jsonSettings.Converters.Add(new StringEnumConverter()); + } if (!Compress) { jsonSettings.Formatting = Formatting.Indented; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Pester.Commands.Cmdlets.Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Pester.Commands.Cmdlets.Json.Tests.ps1 index 2e96004cf..fe1cc85a8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Pester.Commands.Cmdlets.Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Pester.Commands.Cmdlets.Json.Tests.ps1 @@ -211,6 +211,23 @@ Describe "Json Tests" -Tags "Feature" { $emptyStringResult = ConvertFrom-Json "" $emptyStringResult | Should Be $null } + + It "Convert enumerated values to Json" { + + $sampleObject = [pscustomobject]@{ + PSTypeName = 'Test.EnumSample' + SampleSimpleEnum = [System.Management.Automation.ActionPreference]::Ignore + SampleBitwiseEnum = [System.Management.Automation.CommandTypes]'Alias,Function,Cmdlet' + } + + $response4 = ConvertTo-Json -InputObject $sampleObject -Compress + $response4 | Should Be '{"SampleSimpleEnum":4,"SampleBitwiseEnum":11}' + + $response4 = ConvertTo-Json -InputObject $sampleObject -Compress -EnumsAsStrings + $response4 | Should Be '{"SampleSimpleEnum":"Ignore","SampleBitwiseEnum":"Alias, Function, Cmdlet"}' + + } + } Context "JsonObject Tests" {