From 3240a2d3696f8fb5c0e78764a50cecf1e176901d Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Tue, 26 Jul 2016 10:36:53 -0700 Subject: [PATCH] Fix ConvertFrom-Json to handle mutlilines Fix #956 Add Newtonsoft.Json.JsonSerializationException to catch block that determines which mode to use: single-string or multi-string --- .../utility/WebCmdlet/ConvertFromJsonCommand.cs | 6 ++++++ .../ConvertFrom-Json.Tests.ps1 | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs index ef9c26c3f1..5583941ec5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs @@ -93,6 +93,12 @@ namespace Microsoft.PowerShell.Commands // The first input string does not represent a complete Json Syntax. // Hence consider the the entire input as a single Json content. } +#if CORECLR + catch (Newtonsoft.Json.JsonSerializationException) + { + // we use another serializer for CORECLR implementation + } +#endif if (successfullyConverted) { for (int index = 1; index < inputObjectBuffer.Count; index++) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 new file mode 100644 index 0000000000..27b440cc3e --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 @@ -0,0 +1,16 @@ +Describe 'ConvertFrom-Json' { + It 'can convert a single-line object' { + ('{"a" : "1"}' | ConvertFrom-Json).a | Should Be 1 + } + + It 'can convert one string-per-object' { + $json = @('{"a" : "1"}', '{"a" : "x"}') | ConvertFrom-Json + $json.Count | Should Be 2 + $json[1].a | Should Be 'x' + } + + It 'can convert mutli-line object' { + $json = @('{"a" :', '"x"}') | ConvertFrom-Json + $json.a | Should Be 'x' + } +}