Runtime: JsonSerializer outputs Unicode codes instead of characters

Created on 15 Jun 2019  ยท  5Comments  ยท  Source: dotnet/runtime

I have the following code:
c# public class Point { public string Value { get; set; } } var json = JsonSerializer.ToString(new Point { Value = "This is \"first\" point." });
Now json contains the following:

{"Value":"This is \u0022first\u0022 point."}

area-System.Text.Json

Most helpful comment

I never want to use a serializer which escape non-ASCII characters. It's too ugly and slow.

using System;
using System.Text.Json.Serialization;

class X { public string Value { get; set; } }

class Program
{
    static void Main()
    {
        var s = JsonSerializer.ToString(new X { Value = "ใ‚ใ„ใ†ใˆใŠ" });
        Console.WriteLine(s); // {"Value":"\u3042\u3044\u3046\u3048\u304a"}
    }
}

๐Ÿ‘ to a custom encoder/options.

All 5 comments

What is the output you expected, \"? The quotes need to be escaped in some fashion.

Also this appears to be related to or duplicate of dotnet/runtime#29879 and dotnet/runtime#1564.

This is a duplicate of existing issues:
https://github.com/dotnet/corefx/issues/35385
https://github.com/dotnet/corefx/issues/34958

We plan to add the ability for the caller to modify the default behavior by passing in a custom JavaScriptEncoder to JsonWriterOptions, JsonSerializerOptions, and JsonEncodedText.
https://github.com/dotnet/corefx/issues/37192

I never want to use a serializer which escape non-ASCII characters. It's too ugly and slow.

using System;
using System.Text.Json.Serialization;

class X { public string Value { get; set; } }

class Program
{
    static void Main()
    {
        var s = JsonSerializer.ToString(new X { Value = "ใ‚ใ„ใ†ใˆใŠ" });
        Console.WriteLine(s); // {"Value":"\u3042\u3044\u3046\u3048\u304a"}
    }
}

๐Ÿ‘ to a custom encoder/options.

json is utf-8, this seems like a bug if it's escaping all non-ascii codepoints.

Closing as duplicate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yahorsi picture yahorsi  ยท  3Comments

nalywa picture nalywa  ยท  3Comments

Timovzl picture Timovzl  ยท  3Comments

bencz picture bencz  ยท  3Comments

chunseoklee picture chunseoklee  ยท  3Comments