locked
Problem of JsonSerializer.Serialize with different language character RRS feed

  • Question

  • User1052024640 posted

    Here is my code:

    public class TestModel{
    public string AAA{get;set;}
    public string BBB{get;set;}
    }
    [Route("Test")]
    public async Task<string> Test()
    { 
    TestModel _TestModel=new TestModel(){AAA="123",BBB="привет123"};
    string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel)); 
    return JSON;
    }



    When I ran the program, the JSON convert successfully.

    However, the character which is not English or number can not convert correctly but only displays some strange string such as "\u65B0".

    I think maybe it is the problem of encoding. However, it seems I can't set the encoding in the JsonSerializer.Serialize.

    How can I solve this problem? Thank you.

    -------------------------------------

    PS: I don't want to use any other JsonSerializer(such as Newtonsoft) at all for the project has used the JsonSerializer of Microsoft already. I don't want to use two different JsonSerializer or modify all the old one to the new one.

    Saturday, November 2, 2019 1:17 PM

Answers

  • User1052024640 posted

    I solved the problem by modify the `JsonSerializer.Serialize` like this:

    string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel), new JsonSerializerOptions() { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) });



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 3, 2019 11:51 AM

All replies

  • User475983607 posted

    Your are not following standards patterns and practices.  The problem is due to serializing the response twice.  Simply, return the type and let the framework serialize the response.

    [Route("Test")]
    public TestModel Test()
    {
        return new TestModel() { AAA = "123", BBB = "привет123" };
    }

    Saturday, November 2, 2019 4:56 PM
  • User-474980206 posted

    Json by definition is UTF-8. This requires Unicode characters to be encoded in UTF-8. You created a class with Unicode strings. When serialized to JSON they are encoded. If the JSON strings are deserialized to Unicode strings, they will be correct.

    Saturday, November 2, 2019 5:34 PM
  • User1052024640 posted

    But how can I create a class with UTF-8? Just by the way of @mgebhard showed?

    Sunday, November 3, 2019 1:58 AM
  • User1052024640 posted

    Well, it is so amazing that it doesn't need a JsonSerializer.Serialize in your way and it works!

    It seems the JsonSerializer.Serialize no use at all. But what should the JsonSerializer.Serialize truly use for?

    And also, how to DeSerialize the JSON in your way?

    Thank you.

    Sunday, November 3, 2019 2:06 AM
  • User1052024640 posted

    Now there is one more problem:

    I have to get the string but not return at once. For example, I have to use the IHttpClientFactory to send them to another URL.

    How can I solve it?

    Thank you.

    Sunday, November 3, 2019 11:44 AM
  • User1052024640 posted

    I solved the problem by modify the `JsonSerializer.Serialize` like this:

    string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel), new JsonSerializerOptions() { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) });



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 3, 2019 11:51 AM
  • User475983607 posted

    I have to get the string but not return at once. For example, I have to use the IHttpClientFactory to send them to another URL.

    You misunderstand basic data formatting in HTTP and ASP.NET Core.  The first step is reading the framework documentation.

    https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.0

    I have to get the string but not return at once. For example, I have to use the IHttpClientFactory to send them to another URL.

    Can you share code that reproduces this issue?

    Sunday, November 3, 2019 11:54 AM
  • User1052024640 posted

    Thanks for helping me and finally I found a solution by using the JsonSerializer.Serialize also:

    string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel), new JsonSerializerOptions() { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) });
    

    Sunday, November 3, 2019 12:18 PM