msgpack / msgpack-cli

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]
http://msgpack.org
Apache License 2.0
835 stars 175 forks source link

Re-serialize a MessagePackObject? #234

Closed endel closed 7 years ago

endel commented 7 years ago

Hi there,

Given that I have a MessagePackObject instance, I'd like to have a byte[] of its representation.

I basically want to re-serialize the de-serialized object back to byte[].

My failed attempt:

MessagePackObject myObj = ....; // unpacked object
var serializer = MessagePackSerializer.Get<byte[]>();
byte[] packedAgain = serializer.PackSingleObject( myObj );

Error message:

ArgumentException: 'MsgPack.MessagePackObject' is not compatible for 'System.Byte[]'.
Parameter name: objectTree
MsgPack.Serialization.MessagePackSerializer`1[System.Byte[]].ThrowArgumentException (System.String message, System.String parameterName)
MsgPack.Serialization.MessagePackSerializer`1[System.Byte[]].InternalPackSingleObject (System.Object objectTree)
MsgPack.Serialization.MessagePackSerializer.PackSingleObject (System.Object objectTree)

This is very simple to achieve in JavaScript. (msgpack.encode( msgpack.decode( packedMessage ) ))

Thanks in advance.

yfakariya commented 7 years ago

Use MessagePackSerializer.Get<MessagePackObject>() instead.

MessagePackSerializer.PackSingleObject(object) is just a non generic wrapper for PackSingleObject(T).

endel commented 7 years ago

Thank you so much for your swift reply @yfakariya, as always! 🎉