msgpack-cli icon indicating copy to clipboard operation
msgpack-cli copied to clipboard

context.GetSerializer with type in parameter not work well

Open tchekjunior opened this issue 7 years ago • 3 comments

hi, somes problem when you use serializer from context.GetSerializer

public class SimpleItem
{
    public int Id;
    public SimpleItem()
    {
    }
}
var item = new SimpleItem();
var stream = new MemoryStream();
var context = new SerializationContext();

classique use, is work well

var serializer = context.GetSerializer<SimpleItem>();
serializer .Pack(stream, items);
stream.Position = 0;
var cloneItems = serializer .Unpack(stream);

but when you try this

var serializer = context.GetSerializer(item.GetType()  );
var packer = MsgPack.Packer.Create(stream, varcontext.CompatibilityOptions.PackerCompatibilityOptions);
serializer .PackTo(packer, item);
stream.Position = 0;
var cloneItems = serializer .UnpackFrom(MsgPack.Unpacker.Create(stream));

cloneItems is null. Does not seem normall!!

tchekjunior avatar Feb 02 '17 15:02 tchekjunior

Thank you for reporting. I will investigate it too.

yfakariya avatar Feb 02 '17 15:02 yfakariya

You must call Unpacker.Read() before you pass it to UnpackFrom. This is by design for performance, but I found that there is no document of it.

yfakariya avatar Feb 02 '17 22:02 yfakariya

I will consider convenient API for this in the future.

yfakariya avatar Feb 03 '17 15:02 yfakariya