You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a bug only impacting serialization. Minimal reproduction:
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;namespaceRepro{publicstaticpartialclassProgram{publicstaticvoidMain(){MyClassvalue=JsonSerializer.Deserialize(@"{""MyProperty"":5}",MyJsonContext.Default.MyClass)!;Console.WriteLine(value.MyProperty);// Correctly deserializes as 4Console.WriteLine(JsonSerializer.Serialize(value,MyJsonContext.Default.MyClass));// incorrectly serializes as {"MyProperty":4}}[JsonSerializable(typeof(MyClass))]internalpartialclassMyJsonContext:JsonSerializerContext{}internalclassMyClass{[JsonConverter(typeof(MyIntegerConverter))]publicintMyProperty{get;set;}}publicclassMyIntegerConverter:JsonConverter<int>{publicoverrideintRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions)=>reader.GetInt32()-1;publicoverridevoidWrite(Utf8JsonWriterwriter,intvalue,JsonSerializerOptionsoptions)=>writer.WriteNumberValue(value+1);}}}
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.
We shouldn't need to change defaults. Here's my understanding of how things should work.
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.
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.
Reacted by Steve Harter
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
eiriktsarpalis commentedon Aug 26, 2021
This is a bug only impacting serialization. Minimal reproduction:
We have test coverage for deserialization but we seem to be missing equivalent testing for serialization.
eiriktsarpalis commentedon Aug 26, 2021
Also, note that the issue can be worked around by attaching the JsonConverter attribute to the class definition rather than the property.
Jericho commentedon Aug 26, 2021
Please note that the workaround you suggested does not work for the scenario I presented. I cannot change the
boolclass definition.eiriktsarpalis commentedon Aug 27, 2021
You can work around the issue by making the following change to your serialization context:
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.
ericstj commentedon Aug 27, 2021
We shouldn't need to change defaults. Here's my understanding of how things should work.