My implementation returns a string where the json propertyname strings are escaped. This doesn't match the json format and brings up some problems. Is there a way to use the JsonSerializer, or do I have to build the output property for property from hand like suggested in the Microsoft documentation.
So my serialization seems to work, but putting it in a proper json format seems to fail.
public override void Write(Utf8JsonWriter writer, IMyInterface value, JsonSerializerOptions options)
{
switch (value)
{
case Implementation1 i1:
writer.WriteStringValue(JsonSerializer.Serialize(i1, options));
return;
case Implementation2 i2:
writer.WriteStringValue(JsonSerializer.Serialize(i2, options));
return;
case Implementation3 i3:
writer.WriteStringValue(JsonSerializer.Serialize(i3, options));
break;
}
}
This is part of the result e.g. : ["{\"propertyName\":null,...
Update:
I updated like suggested the JsonSerializer options so that it doesn't encode like this in my Startup.cs
in the ConfigureServices
, which seems to not fix the problem.
services.AddMvc().AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
});