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 1adb1d0d473..7848a61b227 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -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; @@ -73,6 +74,14 @@ public class ConvertToJsonCommand : PSCmdlet [Parameter] public SwitchParameter AsArray { get; set; } + /// + /// 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. + /// + [Parameter] + public StringEscapeHandling EscapeHandling { get; set; } = StringEscapeHandling.Default; + #endregion parameters #region overrides @@ -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()); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 5bc819be1c1..198cccf5964 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -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") @@ -48,4 +52,14 @@ Describe 'ConvertTo-Json' -tags "CI" { $output = 1 | ConvertTo-Json $output | Should -BeExactly '1' } + + It "The result string should ." -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 + } }