Writing JSON has became trivial in C# and there's no class I like better for that purpose than Utf8JsonWriter
. Just look at a simple example:
Codevar jsonUtf8 = new Utf8JsonWriter(Console.OpenStandardOutput(),
new JsonWriterOptions() { Indented = true });
jsonUtf8.WriteStartObject();
jsonUtf8.WriteString("Test", "2+2");
jsonUtf8.WriteEndObject();
jsonUtf8.Flush();
This simple code will produce perfectly valid JSON:
JSON{
"Test": "2\u002B2"
}
While valid, you'll notice this is slightly different than any other programming language would do. A single plus character became escape sequence \u002B
.
In their eternal wisdom, .NET architects decided that, by default, JSON should be over-escaped and they "explained" their reasoning in the ticket. Essentially they did it out of abundance of caution to avoid any issues if someone puts JSON where it might not be expected.
Mind you, in 99% of cases JSON is used in HTTP body and thus doesn't need this but I guess one odd case justifies this non-standard but valid output in their minds. And no, other JSON encoders don't behave this way either. Only .NET as far as I can tell.
Fortunately, some time later, they also implemented what I (alongside probably 90% of developers) consider the proper JSON encoder which escapes just mandatory characters and leaves the rest of text alone. It just requires a small extra parameter.
Codevar jsonUtf8 = new Utf8JsonWriter(Console.OpenStandardOutput(),
new JsonWriterOptions() { Indented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping});
jsonUtf8.WriteStartObject();
jsonUtf8.WriteString("Test", "2+2");
jsonUtf8.WriteEndObject();
jsonUtf8.Flush();
Using UnsafeRelaxedJsonEscaping
is not unsafe despite it's name; darn it, it's not even relaxed as compared to the specification. It's just a properly implemented JSON encoder without any extra nonsense thrown in.