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