I can deserialize when passing a stream, but if I pass a ReadOnlyMemory instance instead of a stream, I get a ProtoException "Invalid wire type (VarInt)" Why?
Protobuf-net version 3.1.26
repro:
using (var stream = new MemoryStream())
{
var test = 1;
Serializer.Serialize(stream, test);
// save off bytes for later comparison
var bytes = stream.ToArray();
// this succeeds
stream.Seek(0, SeekOrigin.Begin);
var item = Serializer.Deserialize<int>(stream);
// this succeeds
stream.Seek(0, SeekOrigin.Begin);
var item2 = Serializer.Deserialize(typeof(int), stream);
stream.Seek(0, SeekOrigin.Begin);
var rom = new ReadOnlySpan<byte>(stream.ToArray());
// sanity check: newBytes should be equal to bytes, and it is.
var newBytes = rom.ToArray();
// this fails with ProtoException "Invalid wire-type (Varint)..."
var item3 = Serializer.Deserialize(rom, typeof(int));
}
Can you try Serializer.Deserialize<int>(rom)
? I wonder if your call is doing Deserialize<Type>
via the second parameter being interpreted as an existing object.
If you need a non-generic API:
ProtoBuf.Serializer.NonGeneric.Deserialize(type, rom);