Open Alois-xx opened 4 years ago
For completeness/comparison, added S.T.Json (for the particular case of writing Base64 encoded strings):
sw = Stopwatch.StartNew();
for (int i = 0; i < N; i++)
{
byte[] bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(wrapper);
}
sw.Stop();
GC.Collect();
Console.WriteLine($"Did serialize with S.T.Json in {sw.Elapsed.TotalSeconds:F3}s");
dotnet run -c Release
Did serialize with Json.NET in 1.907s
Did serialize with UTF8Json in 2.846s
Did serialize with S.T.Json in 0.248s
This did come somewhat unexpected:
Code to Test:
The main issue is that Json.NET does not try to escape characters in its JsonWriter. But Utf8Json will call in its Json Writer Utf8Json.JsonWriter.WriteString(string value) for an already Base64 encoded string. Json.NET on the other hand does no checking and directly writes the contents to the output. Is this a known issue and would it easy to solve?