
msgpack
for C++
It's like JSON but smaller and faster.
Overview
MessagePack is an efficient binary serialization format, which lets you exchange data among multiple languages like JSON, except that it's faster and smaller. Small integers are encoded into a single byte and short strings require only one extra byte in addition to the strings themselves.
Example
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
int main()
{
msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
// serialize the object into the buffer.
// any classes that implements write(const char*,size_t) can be a buffer.
std::stringstream buffer;
msgpack::pack(buffer, src);
// send the buffer ...
buffer.seekg(0);
// deserialize the buffer into msgpack::object instance.
std::string str(buffer.str());
msgpack::object_handle oh =
msgpack::unpack(str.data(), str.size());
// deserialized object is valid during the msgpack::object_handle instance is alive.
msgpack::object deserialized = oh.get();
// msgpack::object supports ostream.
std::cout << deserialized << std::endl;
// convert msgpack::object instance into the original type.
// if the type is mismatched, it throws msgpack::type_error exception.
msgpack::type::tuple<int, bool, std::string> dst;
deserialized.convert(dst);
// or create the new instance
msgpack::type::tuple<int, bool, std::string> dst2 =
deserialized.as<msgpack::type::tuple<int, bool, std::string> >();
return 0;
}
See QUICKSTART-CPP.md
for more details.
Usage
C++ Header Only Library
When you use msgpack on C++, you can just add msgpack-c/include to your include path:
g++ -I msgpack-c/include your_source_file.cpp
Building and Installing
Install from git repository
Using the Terminal (CLI)
You will need:
gcc >= 4.1.0
cmake >= 3.0.0
C++03:
$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ git checkout cpp_master
$ cmake .
$ make
$ sudo make install
If you want to setup C++11 or C++17 version of msgpack instead, execute the following commands:
$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ git checkout cpp_master
$ cmake -DMSGPACK_CXX[11|17]=ON .
$ sudo make install
MSGPACK_CXX[11|17]
flags are not affected to installing files. Just switching test cases. All files are installed in every settings.
GUI on Windows
Clone msgpack-c git repository.
$ git clone https://github.com/msgpack/msgpack-c.git
or using GUI git client.
e.g.) tortoise git https://code.google.com/p/tortoisegit/
-
Checkout to cpp_master branch
-
Launch cmake GUI client.
-
Set 'Where is the source code:' text box and 'Where to build the binaries:' text box.
-
Click 'Configure' button.
-
Choose your Visual Studio version.
-
Click 'Generate' button.
-
Open the created msgpack.sln on Visual Studio.
-
Build all.
Documentation
You can get additional information including the tutorial on the wiki.
Contributing
msgpack-c
is developed on GitHub at msgpack/msgpack-c.
To report an issue or send a pull request, use the
issue tracker.
Here's the list of great contributors.
License
msgpack-c
is licensed under the Boost Software License, Version 1.0. See
the LICENSE_1_0.txt
file for details.