Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

450
Views
System.Text.Json.Utf8JsonWriter - how to prevent breaking Unicode characters into escape sequences?

I have a JsonNode to write to a file. The JSON contains a string with a special character in it: "๐Ÿ•".

It's written as "\uD83D\uDC15" and it's not exactly what I want. JSON files support UTF-8, and "๐Ÿ•" is a valid UTF-8 code point consisting of 4 bytes: 0xF0 0x9F 0x90 0xB6.

Instead I get it translated to 12 bytes, just in case I would edit it on old terminal from the 80s. I'm not interested. I actually use Visual Studio Code for editing the file.

How to force writing without such unwanted translations?

BTW, the file is deserialized correctly, the deserialized string contains valid Unicode codepoint. So - basically the application works, however I'm super curious how to change the serialization behavior.

In case someone's too curious about the code, here it is:

public virtual void Save(JsonNode node, Stream stream) {
    if (node is null) return;
    using var writer = new Utf8JsonWriter(stream, WriterOptions);
    node.WriteTo(writer, SerializerOptions);
}

...where WriterOptions:

public JsonWriterOptions WriterOptions { get; } = new() { Indented = true, SkipValidation = true };

...and SerializerOptions:

public JsonSerializerOptions SerializerOptions { get; } = new() { WriteIndented = true };

Here's an example project showing the issue: https://github.com/HTD/JsonNodeUtfIssue/blob/master/JsonNodeUtfIssue/Program.cs

https://dotnetfiddle.net/73RxAd

over 4 years ago ยท Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!