Skip to content

Commit

Permalink
Cleaned up Deseralize logic of SystemTextJsonSourceGen
Browse files Browse the repository at this point in the history
Added attribute usage.
  • Loading branch information
AloisKraus committed Nov 19, 2022
1 parent 2ccc34a commit 50a65b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
4 changes: 3 additions & 1 deletion SerializerTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum SerializerTypes
/// <summary>
/// Describe serializer roughly which data format it writes and which data format it writes
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
class SerializerTypeAttribute : Attribute
{
public SerializerTypes SerializerTypeDescription
Expand All @@ -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
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
class IgnoreSerializeTimeAttribute : Attribute
{
public IgnoreSerializeTimeAttribute(string reason)
public IgnoreSerializeTimeAttribute(string _)
{

}
Expand Down
23 changes: 4 additions & 19 deletions Serializers/SystemTextJsonSourceGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,27 @@ 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<byte> owner = MemoryPool<byte>.Shared.Rent((int)stream.Length);
using IMemoryOwner<char> destOwner = MemoryPool<char>.Shared.Rent((int)stream.Length);

Span<byte> buffer = owner.Memory.Span[..(int)stream.Length];
stream.Read(buffer);

int chars = Encoding.UTF8.GetChars(buffer, destOwner.Memory.Span);
ReadOnlySpan<char> 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))
{
return (T)JsonSerializer.Deserialize(stream, MyJsonContext.Default.LargeBookShelf.Type, MyJsonContext.Default);
}

throw new NotSupportedException($"No source generator for type {typeof(T).Name} declared.");
*/

}
}
}
Expand Down

0 comments on commit 50a65b2

Please sign in to comment.