码迷,mamicode.com
首页 > 其他好文 > 详细

使用MessagePack来压缩传输数据

时间:2016-07-20 09:05:55      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

MessagePack提供了很多中语言的实现方式。

以C#语言为例:(代码来自MessagePack的网站)

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:
 

 

使用MessagePack来压缩传输数据

标签:

原文地址:http://www.cnblogs.com/androidshouce/p/5686961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!