From 50a65b202ed89a03d9485581d732c1542ecf043c Mon Sep 17 00:00:00 2001 From: aloiskraus Date: Sat, 19 Nov 2022 21:15:22 +0100 Subject: [PATCH] Cleaned up Deseralize logic of SystemTextJsonSourceGen Added attribute usage. --- SerializerTypeAttribute.cs | 4 +++- Serializers/SystemTextJsonSourceGen.cs | 23 ++++------------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/SerializerTypeAttribute.cs b/SerializerTypeAttribute.cs index bdabb2e..2ab991c 100644 --- a/SerializerTypeAttribute.cs +++ b/SerializerTypeAttribute.cs @@ -27,6 +27,7 @@ enum SerializerTypes /// /// Describe serializer roughly which data format it writes and which data format it writes /// + [AttributeUsage(AttributeTargets.Class)] class SerializerTypeAttribute : Attribute { public SerializerTypes SerializerTypeDescription @@ -50,9 +51,10 @@ public SerializerTypeAttribute(string projectHomeUrl, SerializerTypes serializer /// When added to a serializer the serialize time is set to 0 because it is e.g. a serializer which only /// can deserialize and the preparation is done in its serialize method /// + [AttributeUsage(AttributeTargets.Class)] class IgnoreSerializeTimeAttribute : Attribute { - public IgnoreSerializeTimeAttribute(string reason) + public IgnoreSerializeTimeAttribute(string _) { } diff --git a/Serializers/SystemTextJsonSourceGen.cs b/Serializers/SystemTextJsonSourceGen.cs index 18333a5..9d8c1f0 100644 --- a/Serializers/SystemTextJsonSourceGen.cs +++ b/Serializers/SystemTextJsonSourceGen.cs @@ -53,34 +53,19 @@ protected override void Serialize(T obj, Stream stream) protected override T Deserialize(Stream stream) { // Currently we need to use the MetaData approach for deserialization. - // Precompiled Deserialization seems not to be supporte yet? + // Precompiled Deserialization seems not to be supported yet? - using IMemoryOwner owner = MemoryPool.Shared.Rent((int)stream.Length); - using IMemoryOwner destOwner = MemoryPool.Shared.Rent((int)stream.Length); - - Span buffer = owner.Memory.Span[..(int)stream.Length]; - stream.Read(buffer); - - int chars = Encoding.UTF8.GetChars(buffer, destOwner.Memory.Span); - ReadOnlySpan dest = destOwner.Memory.Span[..chars]; - - // This overload is used in unit tests of .NET runtime for source generators - // https://github.com/dotnet/runtime/blob/c96e47047af2816cac1f2e240068f71628ef105d/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs - return (T)(object)JsonSerializer.Deserialize(dest, MyJsonContext.Default.BookShelf); - - - /* if (typeof(T) == typeof(BookShelf)) { return (T) (object) JsonSerializer.Deserialize(stream, MyJsonContext.Default.BookShelf); } else if (typeof(T) == typeof(BookShelf1)) { - return (T)JsonSerializer.Deserialize(stream, MyJsonContext.Default.BookShelf1.Type, MyJsonContext.Default); + return (T) JsonSerializer.Deserialize(stream, MyJsonContext.Default.BookShelf1.Type, MyJsonContext.Default); } else if (typeof(T) == typeof(BookShelf2)) { - return (T)JsonSerializer.Deserialize(stream, MyJsonContext.Default.BookShelf2.Type, MyJsonContext.Default); + return (T) JsonSerializer.Deserialize(stream, MyJsonContext.Default.BookShelf2.Type, MyJsonContext.Default); } else if (typeof(T) == typeof(LargeBookShelf)) { @@ -88,7 +73,7 @@ protected override T Deserialize(Stream stream) } throw new NotSupportedException($"No source generator for type {typeof(T).Name} declared."); - */ + } } }