Use Messagepack to compress transmitted data

Source: Internet
Author: User
Tags unpack

Messagepack provides a number of ways to implement the language.

Take the C # language as an example: (Code from Messagepack's website)

Simple Packer/unpacker Boxingpacker Example
usingMsgpack;namespaceTest { Public classTestApp { Public Static voidMain () {Boxingpacker packer=NewBoxingpacker (); Object[] obj =New Object[] {        New Object[] {1,2,3,4},        Newkeyvaluepair<Object,Object>[] {          Newkeyvaluepair<Object,Object> (3,true),          Newkeyvaluepair<Object,Object> (5,false)        },        New Object[] {3.14f, Math.PI}}; byte[] binary =Packer.      Pack (obj); Objectdeserialized =Packer.    Unpack (binary); }  }}


Object Packer/unpacker

Compiledpacker can serialize public class only.

Compiledpacker
usingMsgpack;namespaceTest { Public classTestApp { Public Static voidMain () {DataClass data=NewDataClass {Intfield =1, Stringfield ="Hello World", Arrayfield=New int[]{1,2,3}, Private ="Private Field"}; Compiledpacker Packer=NewCompiledpacker (false);//Serialize public field only (fastest method)      byte[] binary = packer. Pack<dataclass>(data); DataClass data2= Packer. Unpack<dataclass>(binary); Packer=NewCompiledpacker (true);//Serialize all fieldsbinary = 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 classDataClass { Public intIntfield;  Public stringStringfield;  Public int[] Arrayfield; stringPrivatefield;  Public stringPrivate {Get{returnPrivatefield;} Set{Privatefield =value;} }        Public Override stringToString () {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 Fielddata2: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
usingMsgpack;namespaceTest { Public classTestApp {Static voidMain () {DataClass data=NewDataClass {Intfield =1, Stringfield ="Hello World", Arrayfield=New int[]{1,2,3}, Private ="Private Field"}; Objectpacker Packer=NewObjectpacker (); 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 classDataClass { Public intIntfield;  Public stringStringfield;  Public int[] Arrayfield; stringPrivatefield;  Public stringPrivate {Get{returnPrivatefield;} Set{Privatefield =value;} }        Public Override stringToString () {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 = 3itemsPrivate  = Private Field data2:IntField = 1StrField = Hello WorldAryField = 3itemsPrivate  = Private Field
Msgpackreader/writer

Represents a reader/writer that provides fast, non-cached, forward-only access to Messagepack stream.

Msgpackreader/msgpackwriter Example
usingMsgpack;namespaceTest { Public classTestApp {Static voidMain () {Stream strm=NewMemoryStream (); Msgpackwriter writer=NewMsgpackwriter (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=NewMsgpackreader (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:

Use Messagepack to compress transmitted data

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.