dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.45k stars 10.03k forks source link

ASP.NET 6 JSON special characters serialization issue #42833

Open vsfeedback opened 2 years ago

vsfeedback commented 2 years ago

This issue has been moved from a ticket on Developer Community.


Hi, I'm working on Blazor Server project and I wish to make special characters like Chinese not encoded, but when I config global setting it doesn't work, I have to apply setting for single JsonSerializer.Serialize method, wondering did I do something wrong? My dotnet version is 6.0.301 and I'm using VS 2022 17.2.1. Program.cs

using System.Text.Encodings.Web;
using System.Text.Unicode;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Encoder = JavaScriptEncoder
        .Create(UnicodeRanges.BasicLatin, UnicodeRanges.CjkUnifiedIdeographs);
});
builder.Services.AddServerSideBlazor();
...

Index.razor


@jsonString

@code {
    public string jsonString { get; set; }
    protected override async Task OnInitializedAsync()
    {
        jsonString = JsonSerializer.Serialize(new
            {
                Name = "你的名字"
            });
    }
}

image.png


Original Comments

Feedback Bot on 7/4/2022, 10:35 AM:

(private comment, text removed)


Original Solutions

(no solutions)

ghost commented 2 years ago

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost commented 2 years ago

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

captainsafia commented 1 year ago

When invoked in this fashion, the serialization options will not be pulled from DI. You'll probably want to inject the options into your Razor component and then pass them into the JsonSerializer.Serialize call.

ghost commented 1 year ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.