dotnet core System.Text.Json unescape unicode string
You need to set the JsonSerializer options not to encode those strings.
JsonSerializerOptions jso = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
Then you pass this options when you call your Serialize
method.
var s = JsonSerializer.Serialize(a, jso);
Full code:
JsonSerializerOptions jso = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
var a = new A { Name = "你好" };
var s = JsonSerializer.Serialize(a, jso);
Console.WriteLine(s);
Result:
If you need to print the result in the console, you may need to install additional language. Please refer here.
Issues with System.Text.Json serializing Unicode characters (like emojis)
This is by-design. Our goal is to ship secure defaults, which is why we escape anything that we don't know for a fact is safe. For practical reasons, we can't detect all safe characters because that would mean us shipping large tables and perform potentially non-trivial lookups.
If you really insist, you can extend the JavaScriptEncoder
class and choose the encoded characters yourself. I would advise against this because if you're not careful people can sneak in payloads that might change the semantics of the JSON.
System.Text.json JsonSerializer unicode issue on read from json file
This seems like a problem that occurs out of this context. Maybe you are transfering this string between components that doesn't have the same default encoding or is reading a file that has a different encoding than the default encoding of your CLR.
In a comment on your question, you say "I print the data to console". Can you print the raw string (before the desserialization, this messageTemplateJsonData object)? Is he character showing correctly?
Or you could create a string internally with this character (not read externally, but make a literal in your code) and see if the error occurs.
I tried to reproduce this problem on a Csharp notebook (in vscode), and got a correct result, but this is .Net 6 so it could be a version problem.
c# Save korean string to file, not utf form string
What you probably want here is - right at the start:
opt.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
(obviously don't change this constantly - set it once only; if a different usage needs different options: use a different options object)
This leaves the Name
unescaped, rather than escaping it in the JSON. From there, you can then encode the string
any way you like.
You should also note that the name UnsafeRelaxedJsonEscaping
suggests that there may be scenarios in which this is undesirable, so: it would be worth trying to read the documentation on UnsafeRelaxedJsonEscaping
to understand when and why this is.
dotnet core System.Text.Json unescape unicode string
You need to set the JsonSerializer options not to encode those strings.
JsonSerializerOptions jso = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
Then you pass this options when you call your Serialize
method.
var s = JsonSerializer.Serialize(a, jso);
Full code:
JsonSerializerOptions jso = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
var a = new A { Name = "你好" };
var s = JsonSerializer.Serialize(a, jso);
Console.WriteLine(s);
Result:
If you need to print the result in the console, you may need to install additional language. Please refer here.
Related Topics
Foreach VS Somelist.Foreach(){}
Dictionary Returning a Default Value If the Key Does Not Exist
How to Detect the Language of a String
Add Items to Columns in a Wpf Listview
How to Get an Oauth 2.0 Authentication Token in C#
Wpf: Binding a Contextmenu to an Mvvm Command
Extracting Mantissa and Exponent from Double in C#
Replace Multiple String Elements in C#
C#:What If a Static Method Is Called from Multiple Threads
Entity Framework Very Slow to Load for First Time After Every Compilation
How to Get Command Line Arguments of Other Processes from .Net/C#
Nullable Type Is Not a Nullable Type
Error - Unable to Access the Iis Metabase