From d862d4075f3de2f2b89d8e62eafb2d36efe66afe Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 2 Oct 2018 10:54:33 +0500 Subject: [PATCH] Add EscapeHandling parameter in ConvertTo-Json cmdlet (#7775) Add EscapeHandling parameter in ConvertTo-Json cmdlet to allow escape the HTML (<, >, &, ', ") and control characters (e.g. newline). --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 24 +++++++++++++++---- .../ConvertTo-Json.Tests.ps1 | 14 +++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) 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 1adb1d0d47..7848a61b22 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 @@ namespace Microsoft.PowerShell.Commands [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 @@ namespace Microsoft.PowerShell.Commands { 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 5bc819be1c..198cccf596 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 + } }