Use MessagePack to compress and transmit data.
MessagePack provides implementation methods in many languages.
Take the C # language as an example: (the code is from the MessagePack website)
Simple Packer/Unpacker
BoxingPacker Example
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); } }}
Object Packer/Unpacker
CompiledPacker can serialize public class only.
CompiledPacker
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); } } }}
Result
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 (Reflection-based Packer, Slowly)
ObjectPacker can serialize private class/field.
ObjectPacker Example
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); } } }}
Result
data : IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field data2: IntField = 1 StrField = Hello World AryField = 3 items Private = Private Field |
MsgPackReader/Writer
Represents a reader/writer that provides fast, non-cached, forward-only access to MessagePack stream.
MsgPackReader/MsgPackWriter Example
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); } } }}
Result
PositiveFixNum: 1 Nil: (nil) True: True FixArray: len = 3 PositiveFixNum: 1 PositiveFixNum: 2 PositiveFixNum: 3 |
Labels: