[英]How to serialize an mixed type object array using MessagePack in C#
I have an application which communicates over TCP/IP method, and needs to serialize some data over to the device. 我有一个通过TCP / IP方法通信的应用程序,需要将一些数据序列化到设备上。
In the application, I would like to serialize a mixed type object array, which includes string, double array, and some integer..etc. 在应用程序中,我想序列化混合类型的对象数组,其中包括字符串,双精度数组和一些整数..etc。 For example, I would like to serialize Data where:
例如,我想在以下位置序列化数据:
Data = [size, mainmsg]; 数据= [大小,mainmsg]; where size is an int16, and mainmsg is a string.
其中size是一个int16,而mainmsg是一个字符串。
I looked over the following reference http://wiki.msgpack.org/display/MSGPACK/QuickStart+for+C+Sharp 我查看了以下参考资料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. 在我看来,如果我的对象数组中有字符串,则BoxingPacker在解压时将引发异常。
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#) ? 我想问一下,如果我有一个混合类型的对象数组[5,“ D1”],使用msgpack(在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: 现在,我使用BoxingPacker打包我的整数,并使用ObjectPacker打包我的字符串,例如:
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. 然后,我使用Buffer.BlockCopy将二进制数据(packedsize + packedmsg)组合在一起。
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.
任何指导表示赞赏。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.