Skip to content

Commit

Permalink
fix nongenerics bug
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Nov 15, 2022
1 parent f925dd0 commit c2f36c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/MemoryPack.Core/IMemoryPackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ public abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter
[Preserve]
void IMemoryPackFormatter.Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref object? value)
{
var v = (T?)value;
var v = (value == null)
? default(T?)
: (T?)value;
Serialize(ref writer, ref v);
}

[Preserve]
void IMemoryPackFormatter.Deserialize(ref MemoryPackReader reader, scoped ref object? value)
{
var v = (T?)value;
var v = (value == null)
? default(T?)
: (T?)value;
Deserialize(ref reader, ref v);
value = v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ public abstract void Serialize(ref MemoryPackWriter writer, ref T? value)
[Preserve]
void IMemoryPackFormatter.Serialize(ref MemoryPackWriter writer, ref object? value)
{
var v = (T?)value;
var v = (value == null)
? default(T?)
: (T?)value;
Serialize(ref writer, ref v);
}

[Preserve]
void IMemoryPackFormatter.Deserialize(ref MemoryPackReader reader, ref object? value)
{
var v = (T?)value;
var v = (value == null)
? default(T?)
: (T?)value;
Deserialize(ref reader, ref v);
value = v;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/MemoryPack.Tests/PrimitiveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public void ArrayWriterInt()
var i = MemoryPackSerializer.Deserialize<int>(buffer.WrittenSpan);
i.Should().Be(123);
}

[Fact]
public void NonGenericInt()
{
var bin = MemoryPackSerializer.Serialize(123);
var i = MemoryPackSerializer.Deserialize<int>(bin);
i.Should().Be(123);

var j = (int)MemoryPackSerializer.Deserialize(typeof(int), bin)!;
j.Should().Be(123);
}
}

0 comments on commit c2f36c3

Please sign in to comment.