Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,15 @@ public class ConvertToJsonCommand : PSCmdlet
[Parameter]
public SwitchParameter Compress { get; set; }

/// <summary>
/// gets or sets the EnumsAsStrings property.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct typo gets -> Gets.

/// 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.
/// </summary>
[Parameter()]
public SwitchParameter EnumsAsStrings { get; set; }

#endregion parameters

#region overrides
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove extra line.

}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove extra line.

}

Context "JsonObject Tests" {
Expand Down Expand Up @@ -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" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintended whitespace change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But I believe this is because the source newlines were inconsistent. I didn't apply any changes here nor in the next one. IMHO better for newlines to be consistent than to have to manually reset them when tooling does the right thing.

[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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintended whitespace change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reply as above. I did not make these changes, and believe them to be due to inconsistent newlines. My recommendation is to accept the change to make newlines consistent so that future changes in this file don't run into the same issue.

$result.Count | Should be 3
}
}