2

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;
            });
TobiasW
  • 861
  • 3
  • 13
  • 36
  • Why are you building escaped JSON? – mason Dec 10 '20 at 14:41
  • Does this answer your question? [dotnet core System.Text.Json unescape unicode string](https://stackoverflow.com/questions/58003293/dotnet-core-system-text-json-unescape-unicode-string) – Roar S. Dec 10 '20 at 14:45
  • @mason Thats the question, I don't want to build it escaped, I want it in the propert json format. – TobiasW Dec 10 '20 at 14:51
  • @RoarS.It seems to match, but I doesn't change the output. I will update how I integrated it in my code. – TobiasW Dec 10 '20 at 14:51

3 Answers3

2

As @GeorgeSquair already explained, JsonSerializer.Serialize() converts my object into a json string and passing the value into writer.WriteStringValue() will repeat this, resulting in escaping the property names. I checked the parameters of the JsonSerializer.Serialize() and found out, that I can pass in the writer as parameter which solves this issue properly.

public override void Write(Utf8JsonWriter writer, IMyInterface value, JsonSerializerOptions options)
        {
            switch (value)
            {
                case Implementation1 i1:
                    JsonSerializer.Serialize(writer, i1, options);
                    return;
                case Implementation2 i2:
                    JsonSerializer.Serialize(writer, i2, options);
                    return;
                case Implementation3 i3:
                    JsonSerializer.Serialize(writer, i3, options);
                    break;
            }
        }
TobiasW
  • 861
  • 3
  • 13
  • 36
0

When you call JsonSerializer.Serialize() your object is converted into a string, representing json.

Next passing the result of this call into writer.WriteStringValue() will write a string value as json, which in turn will be escaping the characters in the string which you've already serialized into json.

Try using the JsonSerializerOptions instead.

JsonSerializer.Serialize(i1, new JsonSerializerOptions() { WriteIndented = true });

  • 1
    Makes sense! Your solution brings this result : `["{\r\n \"propertyName\": null...` which is a change but unfortunately isn't correct either. – TobiasW Dec 11 '20 at 05:23
-1

Perhaps you should have read the documentation.

WriteStringValue(String): Writes a string text value (as a JSON string) as an element of a JSON array.

You probably want WriteString(String, String): Writes a property name specified as a string and a string text value (as a JSON string) as part of a name/value pair of a JSON object.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • Well this would lead me to manually iterating my whole object and write the json property for property like I already mentioned in my question... I posted a convenient solution which works. – TobiasW Dec 11 '20 at 05:33