Use MessagePack to compress and transmit data.

Source: Internet
Author: User

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 = 1StrField = Hello WorldAryField = 3 itemsPrivate  = Private Field data2:IntField = 1StrField = Hello WorldAryField = 3 itemsPrivate  = data3:IntField = 1StrField = Hello WorldAryField = 3 itemsPrivate  = 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 = 1StrField = Hello WorldAryField = 3 itemsPrivate  = Private Field data2:IntField = 1StrField = Hello WorldAryField = 3 itemsPrivate  = 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: 1Nil: (nil)True: TrueFixArray: len = 3PositiveFixNum: 1PositiveFixNum: 2PositiveFixNum: 3
Labels: 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.