Msgpack introduction of Object serialization Class library

Source: Internet
Author: User
Tags file copy object serialization serialization unpack

Messagepack (hereinafter referred to as Msgpack) a binary efficient object serialization class library that can be used for cross-language communication. It can exchange fabric objects in many languages, like JSON, but it is faster and lighter than JSON. Supports Python, Ruby, Java, C + +, and many other languages. It's 4 times times faster than Google Protocol buffers.

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

The above is excerpted from Oschina introduction.

Msgpack Official homepage: http://msgpack.org/

GitHub Home: Https://github.com/msgpack/msgpack

Because I only use C + + version, so only download the CPP section, please download on demand. Source Installation Msgpack

Open Terminal Download MSGPAC 4 CPP Latest Version 0.5.7

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

Extract

Tar zxvf msgpack-0.5.7.tar.gz

Go to the Unpacked folder for installation

CD msgpack-0.5.7
./configure
make
sudo make install

Of course, you can also use Git and svn to directly crawl the source code to compile, but you need to install version control tools. automatic installation of Msgpack

Apt-get Install Libmsgpack-dev

(The header file is copied to the/usr/local/include/library file copy to the/usr/local/lib/during installation)

installed, we use it directly to see the effect.

It can be used directly containing MSGPACK.HPP. Simple using:

#include <msgpack.hpp>
#include <vector>
#include <string>
#include <iostream>

int main ()
{
	std::vector<std::string> _vecstring;
	_vecstring.push_back ("Hello");
	_vecstring.push_back ("World");

	Pack
	Msgpack::sbuffer _sbuffer;
	Msgpack::p ack (_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<std::string> _vecrstring;
	Obj.convert (&_vecrstring);

	Print for
	(size_t i = 0; i < _vecrstring.size (); ++i)
	{
		std::cout << _vecrstring[i] << std :: Endl;
	}

    return 0;
}

Results:

挜 helloorld
["Hello", "World"]
Hello
World
using stream:

#include <msgpack.hpp>
#include <vector>
#include <string>
#include <iostream>

int main ()
{
	//Msgpack stream

	//Use Msgpack::p the Acker to pack multiple objects.
	Msgpack::sbuffer Buffer_;
	Msgpack::p Acker 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;
}

Serializes multiple objects using the Sbuffer stream. How to serialize a custom data structure

Msgpack supports serialization/deserialization of custom data structures by simply using Msgpack_define macros.

# #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 ()

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.