C#的二进制序列化组件MessagePack介绍

                                        <div id="js_content">
                

                

                
                
                <p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy82T3hxU3FXQnFzTHhPQmFQbGZXNlVhR1RPY1VDeUUxYm1aR3R6QXhLYzJnM3A3b2w4eDlQWUppY2VMbUtDWDlHTDNKUkxrUk1sdHhBWHFQSms1WHZpYkVRLzY0MD93eF9mbXQ9anBlZw?x-oss-process=image/format,png" width="800"></p><p>C#的序列化有多种,我一般喜欢用第三方组件,一个公共组件要拿出来用,而且支持很多语言,甚至以此谋生,肯定有其优势。</p><p>有或者说存在必然有其合理性,经过几年开发,我更加喜欢第三方的东西,类似序列化的东西。</p><p>&nbsp;开篇总是牢骚,大家要习惯哈</p><p>最近在写一个小组件,组合一个框架,先分别介绍里面用到的东西,恰好一个个说说。</p><p>本篇就叫序列化篇吧,没有什么多说的,直接代码上了。</p><p>已经测试了,没有什么多说的。</p><p>里面有个init方法,因为我的类是静态的,所以每个都调用一次初始化。</p><p>&nbsp;/// 序列化二进制<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="obj"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static byte[] Serializer&lt;T&gt;(T obj)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &nbsp;MessagePackSerializer.Serialize&lt;T&gt;(obj);<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// 反序列化二进制<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="bytes"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static T Deserialize&lt;T&gt;(byte[] bytes)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return MessagePackSerializer.Deserialize&lt;T&gt;(bytes);<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// byte[]转json字符串<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="json"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static string JSONBytesToString(byte[]json)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &nbsp; MessagePackSerializer.ToJson(json);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// json字符串转byte[]<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="json"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static byte[] &nbsp; JSONStringToBytes(string json)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return MessagePackSerializer.FromJson(json);<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// 对象转json字符串<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="obj"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static string &nbsp;JSONObjectToString&lt;T&gt;(T obj)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return MessagePackSerializer.ToJson&lt;T&gt;(obj);<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// 对象直接转json的byte[]<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="obj"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static byte[] &nbsp;JSONObjectToBytes&lt;T&gt;(T obj )<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JSONStringToBytes(JSONObjectToString&lt;T&gt;(obj));<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// json字符串转对象,序列化<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="json"&gt;&lt;/param&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; public static T JSONStringToObject&lt;T&gt;(string json)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Init();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Deserialize&lt;T&gt;(JSONStringToBytes(json));<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp;</p><p>后说:</p><p>MessagePack中也有JSON,所以一并用了,谁叫fastjson只有java版本呢。</p><p>MessagePack有2种使用方式,一直是特性,需要在序列化的类上添加MessagePackObject,这叫做限制类扩展使用</p><p>例如:</p><p>[MessagePackObject]</p><p>public &nbsp;Class Person</p><p>{</p><p>}</p><p>另外一种就是非限制的,就是我使用的方式,封装一个方法,不使用特性,但是这种就需要初始化了,采用默认的实例。</p><p>也就有了我的初始化方法Init.就像下面这样。</p><p>&nbsp; &nbsp;private static volatile bool isInit=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; private static void Init()<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isInit)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessagePackSerializer.SetDefaultResolver(MessagePack.Resolvers.ContractlessStandardResolver.Instance);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isInit = false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>MessagePack支持.Net 各类型平台,.Net Core,.Net FramWork,.Net Standard.</p><p>最最后说一下,mongdb有个东西叫BSON,这也是一种格式,也可以作为通信格式序列化。</p><p>mongdb的BSON格式也支持.Net各类型平台了。</p><p>总体来说,c#的序列化有XML,二进制,JSON,BSON</p>
            </div>