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 @@ -2,15 +2,16 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Management.Automation;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Globalization;
using Dbg = System.Management.Automation;
using System.Management.Automation;
using System.Management.Automation.Internal;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Expand Down Expand Up @@ -73,6 +74,14 @@ public class ConvertToJsonCommand : PSCmdlet
[Parameter]
public SwitchParameter AsArray { get; set; }

/// <summary>
/// Specifies how strings are escaped when writing JSON text.
/// If the EscapeHandling property is set to EscapeHtml, the result JSON string will
/// be returned with HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
/// </summary>
[Parameter]
public StringEscapeHandling EscapeHandling { get; set; } = StringEscapeHandling.Default;

#endregion parameters

#region overrides
Expand Down Expand Up @@ -125,7 +134,12 @@ protected override void EndProcessing()
{
return;
}
JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 };
JsonSerializerSettings jsonSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None,
MaxDepth = 1024,
StringEscapeHandling = EscapeHandling
};
if (EnumsAsStrings)
{
jsonSettings.Converters.Add(new StringEnumConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe 'ConvertTo-Json' -tags "CI" {
BeforeAll {
$newline = [System.Environment]::NewLine
}

It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' {
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
$EgJObject.Add("TestValue1", "123456")
Expand Down Expand Up @@ -48,4 +52,14 @@ Describe 'ConvertTo-Json' -tags "CI" {
$output = 1 | ConvertTo-Json
$output | Should -BeExactly '1'
}

It "The result string should <Name>." -TestCases @(
@{name = "be not escaped by default."; params = @{}; expected = "{$newline ""abc"": ""'def'""$newline}" }
@{name = "be not escaped with '-EscapeHandling Default'."; params = @{EscapeHandling = 'Default'}; expected = "{$newline ""abc"": ""'def'""$newline}" }
@{name = "be escaped with '-EscapeHandling EscapeHtml'."; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$newline ""abc"": ""\u0027def\u0027""$newline}" }
) {
param ($name, $params ,$expected)

@{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected
}
}