标签:
MessagePack提供了很多中语言的实现方式。
以C#语言为例:(代码来自MessagePack的网站)
using MsgPack; namespace Test { public class TestApp { public static void Main () { BoxingPacker packer = new BoxingPacker (); object[] obj = new object[] { new object[] {1, 2, 3, 4}, new KeyValuePair<object, object>[] { new KeyValuePair<object, object> (3, true), new KeyValuePair<object, object> (5, false) }, new object[] {3.14f, Math.PI} }; byte[] binary = packer.Pack (obj); object deserialized = packer.Unpack (binary); } } }
CompiledPacker can serialize public class only.
using MsgPack; namespace Test { public class TestApp { public static void Main () { DataClass data = new DataClass {IntField = 1, StringField = "Hello World", ArrayField = new int[]{1, 2, 3}, Private = "Private Field"}; CompiledPacker packer = new CompiledPacker (false); // serialize public field only (fastest method) byte[] binary = packer.Pack<DataClass> (data); DataClass data2 = packer.Unpack<DataClass> (binary); packer = new CompiledPacker (true); // serialize all fields binary = packer.Pack<DataClass> (data); DataClass data3 = packer.Unpack<DataClass> (binary); Console.WriteLine ("data :"); Console.WriteLine (data); Console.WriteLine (); Console.WriteLine ("data2:"); Console.WriteLine (data2); Console.WriteLine (); Console.WriteLine ("data3:"); Console.WriteLine (data3); Console.WriteLine (); } public class DataClass { public int IntField; public string StringField; public int[] ArrayField; string PrivateField; public string Private { get { return PrivateField; } set { PrivateField = value;} } public override string ToString () { return string.Format ("IntField = {0}\r\nStrField = {1}\r\nAryField = {2} items\r\nPrivate = {3}", IntField, StringField, ArrayField == null ? 0 : ArrayField.Length, PrivateField); } } } }
data : IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field data2: IntField = 1 StrField = Hello World AryField = 3 items Private = data3: IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field |
ObjectPacker can serialize private class/field.
|
|
using MsgPack; namespace Test { public class TestApp { static void Main () { DataClass data = new DataClass {IntField = 1, StringField = "Hello World", ArrayField = new int[]{1, 2, 3}, Private = "Private Field"}; ObjectPacker packer = new ObjectPacker (); byte[] binary = packer.Pack (data); DataClass data2 = packer.Unpack<DataClass> (binary); Console.WriteLine ("data :"); Console.WriteLine (data); Console.WriteLine (); Console.WriteLine ("data2:"); Console.WriteLine (data2); } public class DataClass { public int IntField; public string StringField; public int[] ArrayField; string PrivateField; public string Private { get { return PrivateField; } set { PrivateField = value;} } public override string ToString () { return string.Format ("IntField = {0}\r\nStrField = {1}\r\nAryField = {2} items\r\nPrivate = {3}", IntField, StringField, ArrayField == null ? 0 : ArrayField.Length, PrivateField); } } } }
data : IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field data2: IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field |
Represents a reader/writer that provides fast, non-cached, forward-only access to MessagePack stream.
|
|
using MsgPack; namespace Test { public class TestApp { static void Main () { Stream strm = new MemoryStream (); MsgPackWriter writer = new MsgPackWriter (strm); writer.Write (1); writer.WriteNil (); writer.Write (true); writer.WriteArrayHeader (3); writer.Write (1); writer.Write (2); writer.Write (3); strm.Seek (0, SeekOrigin.Begin); MsgPackReader reader = new MsgPackReader (strm); while (reader.Read ()) { Console.Write ("{0}: ", reader.Type); if (reader.IsSigned ()) Console.WriteLine (reader.ValueSigned); else if (reader.Type == TypePrefixes.Nil) Console.WriteLine ("(nil)"); else if (reader.IsBoolean ()) Console.WriteLine (reader.ValueBoolean); else if (reader.IsArray ()) Console.WriteLine ("len = {0}", reader.Length); } } } }
PositiveFixNum: 1 Nil: (nil) True: True FixArray: len = 3 PositiveFixNum: 1 PositiveFixNum: 2 PositiveFixNum: 3 |
标签:
原文地址:http://www.cnblogs.com/androidshouce/p/5686961.html