Skip to content

System.Text.Json code generator: custom converter ignored? #58119

Closed

Description

@Jericho

I have a class that contains the following property (notice the presence of a custom converter):

[JsonPropertyName("tls")]
[JsonConverter(typeof(IntegerBooleanConverter))]
public bool Tls { get; set; }

The following snippet gets generated:

writer.WriteBoolean(tlsPropName, value.Tls);

Correct me if I'm wrong but this seems to ignore my custom converter. I was expecting something along the lines of:

writer.WritePropertyName(tlsPropName);
global::System.Text.Json.JsonSerializer.Serialize(writer, value.Tls, global::MyLibrary.Utilities.MyJsonSerializerContext.Default.Tls);

Activity

  1. ghost added
    untriaged
    on Aug 25, 2021
  2. eiriktsarpalis commented on Aug 26, 2021

    @eiriktsarpalis
    Member

    This is a bug only impacting serialization. Minimal reproduction:

    using System;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    namespace Repro
    {
        public static partial class Program
        {
            public static void Main()
            {
                MyClass value = JsonSerializer.Deserialize(@"{""MyProperty"":5}", MyJsonContext.Default.MyClass)!;
                Console.WriteLine(value.MyProperty); // Correctly deserializes as 4
                Console.WriteLine(JsonSerializer.Serialize(value, MyJsonContext.Default.MyClass)); // incorrectly serializes as {"MyProperty":4}
            }
    
            [JsonSerializable(typeof(MyClass))]
            internal partial class MyJsonContext : JsonSerializerContext
            {
            }
    
            internal class MyClass
            {
                [JsonConverter(typeof(MyIntegerConverter))]
                public int MyProperty { get; set; }
            }
    
            public class MyIntegerConverter : JsonConverter<int>
            {
                public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetInt32() - 1;
                public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) => writer.WriteNumberValue(value + 1);
            }
        }
    }

    We have test coverage for deserialization but we seem to be missing equivalent testing for serialization.

  3. added this to the 6.0.0 milestone on Aug 26, 2021
  4. added and removed
    untriaged
    on Aug 26, 2021
  5. eiriktsarpalis commented on Aug 26, 2021

    @eiriktsarpalis
    Member

    Also, note that the issue can be worked around by attaching the JsonConverter attribute to the class definition rather than the property.

  6. Jericho commented on Aug 26, 2021

    @Jericho
    Author

    Please note that the workaround you suggested does not work for the scenario I presented. I cannot change the bool class definition.

  7. eiriktsarpalis commented on Aug 27, 2021

    @eiriktsarpalis
    Member

    You can work around the issue by making the following change to your serialization context:

    [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
    [JsonSerializable(typeof(MyClass))]
    internal partial class MyJsonContext : JsonSerializerContext
    {
    }

    Root cause of the issue is that source gen by default uses fast-path serialization, which by design does not implement all features found in metadata-based serialization. The reason this does not impact deserialization is that there's no fast-path deserialization implemented yet. Note that switching to metadata-based serialization will result in slower serialization performance.

    @layomia @ericstj should we consider making metadata-based serialization the default? As seen in this example, the current default configuration can result in non-roundtrippable classes. We should make sure that the default configuration just works even if doesn't result in optimal performance.

  8. ericstj commented on Aug 27, 2021

    @ericstj
    Member

    We shouldn't need to change defaults. Here's my understanding of how things should work.

    1. If a type can't be supported through fast path deserialization based on information available at compile time we should tell the user and they can make the change. We should not generate serialization logic that ignores part of the serialization contract.
    2. If a type can't be supported through fast path deserialization based on information available at runtime (EG: converter added at runtime), we should fallback to using metadata if present.
  9. ghost added
    in-pr
    on Aug 27, 2021
  10. ghost removed
    in-pr
    on Sep 2, 2021
  11. ghost locked as resolved and limited conversation to collaborators on Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions