Implemented: Code Generation for Serialization #325
Replies: 1 comment
-
Code Generation for serialization overview - https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ Developer needs to define PersonContext in order to serialize Person [JsonSerializable(typeof(Person))]
internal partial class PersonContext : JsonSerializerContext
{
} Code generation creates various methods and properties, of which one is Person person = new() { FirstName = "Jane", LastName = "Doe" };
byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(person, PersonContext.Default.Person);
person = JsonSerializer.Deserialize(utf8Json, PersonContext.Default.Person): ProposalDeveloper has to create serializer context for each class they want to serialize eg PersonContext for Person class. Source generation will expose the JsonTypeInfo instance via the Default.Person property. JsonTypeInfo is registered with the DI container eg: public class SystemTextJsonGeneratedSerializer<T> : ISerializer<T>
{
public SystemTextJsonGeneratedSerializer(ISerializer reflectionBasedSerializer, JsonTypeInfo<T>? typeInfo){....}
} The typeInfo parameter would be marked as optional and if it's not available the implementation would fall back to the reflection based json serializer (a reflection based implementation would be provided via the ISerializer implementation). |
Beta Was this translation helpful? Give feedback.
-
Uno.Extensions.Serialization includes an implementation of json serialization that uses System.Text.Json which is reflection based.
Need a mechanism for using support for code generation to improve efficiency of json serialization
Beta Was this translation helpful? Give feedback.
All reactions