Skip to content

ConvertTo-Json string escaped handling differs between PS and PSCore #7693

Closed

Description

Steps to reproduce

On PowerShell Core 6.0.4

ConvertTo-Json -InputObject @{ 'abc' = "'def'" }

Expected behavior

Windows PowerShell 5.x returns:

{
    "abc":  "\u0027def\u0027"
}

Actual behavior

PowerShell Core 6.0.4 returns:

{
  "abc": "'def'"
}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.4
PSEdition                      Core
GitCommitId                    v6.0.4
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

This would appear to be because the StringEscapeHandling setting is set to Default in

JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 };

If the StringEscapeHandling setting is set to [Newtonsoft.Json.StringEscapeHandling]::EscapeHtml then the JSON string that is generated matches what is returned by Windows PowerShell.

image

Activity

  1. iSazonov commented on Sep 3, 2018

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) Please look the Issue.

    Is this a regression and should be fixed before 6.1.0 GA?

    /cc Steve Lee (@SteveL-MSFT)

  2. mkht commented on Sep 5, 2018

    @mkht
    Contributor

    Is not it a destructive change to change this behavior?

    How about add parameter to change behavior like below ?

    ConvertTo-Json -InputObject @{ 'abc' = "'def'" } -EscapeHandling Default
    ConvertTo-Json -InputObject @{ 'abc' = "'def'" } -EscapeHandling EscapeNonAscii
    ConvertTo-Json -InputObject @{ 'abc' = "'def'" } -EscapeHandling EscapeHtml
  3. SteveL-MSFT commented on Sep 5, 2018

    @SteveL-MSFT
    Member

    This has been the behavior since we changed to using NewtonSoft.JSON from the .NET Framework JSON serializer and would be a breaking change to change it back. mkht's suggestion to add a parameter would work and give more flexibility to the user. However, are there other settings that should be exposed via the cmdlet?

  4. added
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Sep 6, 2018
  5. mkht commented on Sep 6, 2018

    @mkht
    Contributor

    Confirmed NewtonSoft's reference.
    I think that there is no other setting that needs to be exposed.
    Other handling settings such as DateFormatHandling can be replaced by formatting the properties of the object to be passed in advance.

    If you are adding parameters for escape handling, it will be necessary to discuss how to do the default value.
    I think it should be [Newtonsoft.Json.StringEscapeHandling]::EscapeHtml. That's because JSON standard requires that quotation marks must be escaped. However, to avoid destructive changes, it should be [Newtonsoft.Json.StringEscapeHandling]::Default.

  6. mkht commented on Sep 6, 2018

    @mkht
    Contributor

    Sorry, I had a misunderstanding.

    Latest JSON standard (RFC 8259) requires that the QUOTATION MARK (U+0022) must be escaped.
    But, other quotation marks such as RIGHT SINGLE QUOTATION MARK (U+2019) or APOSTROPHE (U+0027) is not required to be.
    Now, I think that the default setting should be [Newtonsoft.Json.StringEscapeHandling]::Default.

    https://tools.ietf.org/rfc/rfc8259.txt

    All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

  7. SteveL-MSFT commented on Sep 12, 2018

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this and agrees that Default should stay as default and we should expose -EscapeHandling as an optional parameter.

  8. added
    Committee-ReviewedPS-Committee has reviewed this and made a decision
    and removed
    Breaking-Changebreaking change that may affect users
    Review - CommitteeThe PR/Issue needs a review from the PowerShell Committee
    on Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions