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 eef7da897e4..7feb75ce842 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 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 @@ public class ConvertToJsonCommand : PSCmdlet [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 @@ protected override void EndProcessing() 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 666f5463d1f..b0279686ebd 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" { @@ -1458,9 +1475,9 @@ Describe "Json Bug fixes" -Tags "Feature" { It "ConvertFrom-Json deserializes an array of PSObjects (in multiple lines) as a single string." { # Create an array of PSCustomObjects, and serialize it - $array = [pscustomobject]@{ objectName = "object1Name"; objectValue = "object1Value" }, - [pscustomobject]@{ objectName = "object2Name"; objectValue = "object2Value" } - + $array = [pscustomobject]@{ objectName = "object1Name"; objectValue = "object1Value" }, + [pscustomobject]@{ objectName = "object2Name"; objectValue = "object2Value" } + # Serialize the array to a text file $filePath = Join-Path $TESTDRIVE test.json $array | ConvertTo-Json | Out-File $filePath -Encoding utf8 @@ -1472,7 +1489,7 @@ Describe "Json Bug fixes" -Tags "Feature" { It "ConvertFrom-Json deserializes an array of strings (in multiple lines) as a single string." { - $result = "[1,","2,","3]" | ConvertFrom-Json + $result = "[1,","2,","3]" | ConvertFrom-Json $result.Count | Should be 3 } }