Reputation: 451
I have an application which communicates over TCP/IP method, and needs to serialize some data over to the device.
In the application, I would like to serialize a mixed type object array, which includes string, double array, and some integer..etc. For example, I would like to serialize Data where:
Data = [size, mainmsg]; where size is an int16, and mainmsg is a string.
I looked over the following reference http://wiki.msgpack.org/display/MSGPACK/QuickStart+for+C+Sharp
It seems to me that the BoxingPacker will throw an exception while unpacked if I have string in my object array.
I would like to ask, if I have a mixed type object array, [5,"D1"], what would the best way to serialize using msgpack(in c#) ? (This package is designed, can't change over other serialization methods)
Right now, I uses BoxingPacker to pack my integer, and use ObjectPacker to pack my string, for example:
size = 1;
msg = "D1"
BoxingPacker intpacker = new BoxingPacker();
packedsize = intpacker.Pack(size);
ObjectPacker packer = new ObjectPacker();
packedmsg = packer.Pack<String>(msg);
Then I combined the binary data (packedsize + packedmsg) together using Buffer.BlockCopy.
I am looking for if there's an easy way to do this ? Maybe I am missing something, but I could not find anything documentation except the link I've pasted above. Any guidance is appreciated.
Upvotes: 0
Views: 2247
Reputation: 405
You just need to serialize an array of objects
size = 1;
msg = "D1"
object[] objs = new object[] { size, msg };
ObjectPacker packer = new ObjectPacker();
packedmsg = packer.Pack<object[]>(objs);
Upvotes: 1