Introduction to object serialization class library MsgPack

Source: Internet
Author: User
Tags object serialization unpack

MessagePack (MsgPack) is a binary-efficient object serialization class library that can be used for cross-language communication. It can exchange structure objects in many languages like JSON, but it is faster and lighter than JSON. Supports Python, Ruby, Java, C/C ++, and many other languages. Compared with Google
Protocol Buffers is four times faster.

Code:
> Require 'msgpack'
> Msg = [1, 2, 3]. to_msgpack # => "\ x93 \ x01 \ x02 \ x03 ″
> MessagePack. unpack (msg) # => [1, 2, 3]

The above is an introduction from oschina.

Msgpack official home: http://msgpack.org/

Github home: https://github.com/msgpack/msgpack

Because we only use the C ++ version, we only download the CPP part. Please download it as needed.

Install msgpack with source code

Open the terminal and download the latest version of msgpac 4 cpp 0.5.7.

1 Wget http://msgpack.org/releases/cpp/msgpack-0.5.7.tar.gz

Extract

1 Tar zxvf msgpack-0.5.7.tar.gz

Enter the decompressed folder for Installation

1234 Cd msgpack-0.5.7./configuremakesudo make install

Of course, you can also use git and svn to directly capture the source code for compilation, but you need to install a version control tool.

Automatic Installation of msgpack

1 Apt-get install libmsgpack-dev

(During installation, the header file is copied to the/usr/local/include/library file to/usr/local/lib /)

After the installation is complete, we can use it to see the effect.

You can directly include msgpack. hpp.

Simple using

# Include <msgpack. hpp>
# Include <vector>
# Include <string>
# Include <iostream>
 
Int main ()
{
Std: vector _ vecString;
_ VecString. push_back ("Hello ");
_ VecString. push_back ("world ");
 
// Pack
Msgpack: sbuffer _ sbuffer;
Msgpack: pack (_ sbuffer, _ vecString );
Std: cout <_ sbuffer. data () <std: endl;
 
// Unpack
Msgpack: unpacked msg;
Msgpack: unpack (& msg, _ sbuffer. data (), _ sbuffer. size ());
Msgpack: object obj = msg. get ();
Std: cout <obj <std: endl;
 
// Convert
Std: vector _ vecRString;
Obj. convert (& _ vecRString );
 
// Print
For (size_t I = 0; I <_ vecRString. size (); ++ I)
{
Std: cout <_ vecRString [I] <std: endl;
}
 
Return 0;
}
The results will not be posted, and you will know it by yourself.

Using stream

# Include <msgpack. hpp>
# Include <vector>
# Include <string>
# Include <iostream>
 
Int main ()
{
// Msgpack stream
 
// Use msgpack: packer to pack multiple objects.
Msgpack: sbuffer buffer _;
Msgpack: packer pack _ (& buffer _);
Pack _. pack (std: string ("this is 1st string "));
Pack _. pack (std: string ("this is 2nd string "));
Pack _. pack (std: string ("this is 3th string "));
 
// Use msgpack: unpacker to unpack multiple objects.
Msgpack: unpacker unpack _;
Unpack _. reserve_buffer (buffer _. size ());
Memcpy (unpack _. buffer (), buffer _. data (), buffer _. size ());
Unpack _. buffer_consumed (buffer _. size ());
 
Msgpack: unpacked result _;
While (unpack _. next (& result _))
{
Std: cout <result _. get () <std: endl;
}
 
Return 0;
}
Use sbuffer stream to serialize multiple objects.

How to serialize Custom Data Structures
Msgpack supports serialization/deserialization of custom data structures. You only need to simply use the MSGPACK_DEFINE macro.

# Include <msgpack. hpp>
# Include <vector>
# Include <string>
 
Class my_class
{
Private:
Std: string my_string;
Std: vector vec_int;
Std: vector vec_string;
Public:
MSGPACK_DEFINE (my_string, vec_int, vec_string );
};
 
Int main ()
{
Std: vector my_class_vec;
 
// Add some data
 
Msgpack: sbuffer buffer;
Msgpack: pack (buffer, my_class_vec );
 
Msgpack: unpacked msg;
Msgpack: unpack (& msg, buffer. data (), buffer. size ());
 
Msgpack: object obj = msg. get ();
Std: vector my_class_vec_r;
Obj. convert (& my_class_vec_r );
 
Return 0;
}

In this way, msgpack can be used in network communication and other places to serialize our data structure, which is completely secure and efficient, and the receiver can use other languages to process the structure for logic. Multiple languages are supported. The Supported languages are as follows:
Ruby Perl Python C/C ++ Java php js oc c # Lua Scala D Haskell Erlang Ocaml Smallalk GO LabVIEW
It is enough for us to use. Of course, if you don't have the language you want, we recommend that you copy the source code.
For the performance test results, see msgpack and test in linux.

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.