Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Boost Up Your Serialization/Deserialization Similar to JSON

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
23 Dec 2012CPOL3 min read 29.9K   7   2
This tip explains how to increase performance in serialization by using MessagePack

Introduction

Recently, I have been involved in a project which requires better ser./des. capabilities in consideration of message size packing. In general, we need to transfer data over wire (basically it's HTTP) in representation of bulk portion of messages, which is not so big in its size.

JSON is a fantastic format, anywhere people in your organization want to reach for XML. So as a result, the first thought that comes to my mind was usage of Json.NET (Newtonsoft.net), but after some investigation, I have discovered that from size packing consideration JSON.NET is not so compact.

MessagePack Comes to the Rescue

MessagePack is an interoperable binary encoding format. You can reduce the size of stream dramatically when you have a data of which contains many binary (for example, numeric) data.

MessagePack is faster to serialize binary data such as thumbnail images. MessagePack is better to reduce overheads to exchange small objects between servers. JSON is better to use with browsers.

If you ever wished to use JSON for convenience (storing an image with metadata) but could not for technical reasons (encoding, size, speed...), MessagePack is a perfect replacement.

MessagePack is effectively JSON, but with efficient binary encoding. Like JSON, there is no type checking or schemas, which depending on your application can be either a pro or a con. But, if you are already streaming JSON via an API or using it for storage, then MessagePack can be a drop-in replacement.

So, let's summarize and provide some pros:

  • JSON Compatible: Anything that works with JSON will work with MessagePack.
  • More space efficient: MessagePack uses an extremely efficient binary serialization format, for things like numbers and binary data MessagePack can be hugely more efficient. For use in persisting datastructures in something like Redis, where you want to be careful with memory usage, this is quite useful.
  • Faster: MessagePack is usually much faster to encode / decode than JSON.
  • Supports Your Language: Ruby, Python, Perl, Javascript, PHP, Java, C++, C#,Go, Erlang, Haskell, OCaml, Scala, Clojure, and more all support MessagePack.
  • RPC: There's a separate MessagePack RPC project that maintains a high performance MessagePack based RPC server and client available in most of the languages above.

Using the Code

NOTE: To use the code below, just add a reference to MsgPack.dll.

So let's pretend that we have some class called Foo, in such case for serialization and deserialization code will be next:

C#
using(var stream =new MemoryStream()){ 
var serializer = MessagePackSerializer.Create<Foo>();
serializer.Pack(foo, stream); 
stream.Position = 0; 
var value = serializer.Unpack(stream); 
}   

Serialization Rules in WCF

For transferring data over the wire and successfully deserializing it on a client side, use the next approach (relevant for WCF, and another type of RPC).

C#
[DataContract] 
public class Person
{
[MessagePackMember] 
public int ID{get;set;}  
[MessagePackMember] 
public string Name{get;set;} 
[NonSerialized] 
public int Age {get;set;} 
}   

Performance

According to the vendors information, from performance consideration MessagePack is 4 times faster than protobuff. To be honest, I do not believe in it. But what I know exactly is that it's much faster, performs significantly better with the fields in class, - over 10% faster in ser/des in comparison with protobuf.

Points of Interest

In future, I am planning to write some more tips about MessagePack and especially usage of MessagePack in conjunction with ASP.NET Web API.

Original link http://msgpack.org/.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Delphi LLC
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
sam.hill24-Dec-12 10:55
sam.hill24-Dec-12 10:55 
GeneralRe: My vote of 5 Pin
Oleksandr Kulchytskyi24-Dec-12 11:15
professionalOleksandr Kulchytskyi24-Dec-12 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.