2013-12-17 08:53:15 +10:30
# Msgpack for C/C++
2013-12-22 21:04:53 -08:00
2013-12-17 08:53:15 +10:30
It's like JSON but small and fast.
2013-12-22 21:04:53 -08:00
2013-12-17 08:53:15 +10:30
## Overview
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
## License
Msgpack is Copyright (C) 2008-2010 FURUHASHI Sadayuki and licensed under the Apache License, Version 2.0 (the "License"). For details see the `COPYING` file in this directory.
## Contributing
The source for msgpack-c is held at [msgpack-c ](https://github.com/msgpack/msgpack-c ) github.com site.
To report an issue, use the [msgpack-c issue tracker ](https://github.com/msgpack/msgpack-c/issues ) at github.com.
## Using Msgpack
### Building and Installing
#### Install from git repository
2014-06-07 00:13:23 +09:00
##### Using autotools
2013-12-17 08:53:15 +10:30
You will need gcc (4.1.0 or higher), autotools.
```
$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ ./bootstrap
$ ./configure
$ make
$ sudo make install
```
2014-06-07 00:13:23 +09:00
##### Using cmake
You will need gcc (4.1.0 or higher), cmake.
```
$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ cmake .
$ make
```
2013-12-17 08:53:15 +10:30
#### Install from package
##### UNIX-like platform with ./configure
2013-12-22 21:04:53 -08:00
On typical UNIX-like platforms, download source package from [Releases ](https://github.com/msgpack/msgpack-c/releases ) and run `./configure && make && make install` . Example:
2013-12-17 08:53:15 +10:30
```
2013-12-22 21:04:53 -08:00
$ wget https://github.com/msgpack/msgpack-c/releases/download/cpp-0.5.8/msgpack-0.5.8.tar.gz
$ tar zxvf msgpack-0.5.8.tar.gz
$ cd msgpack-0.5.8
2013-12-17 08:53:15 +10:30
$ ./configure
$ make
$ sudo make install
```
##### FreeBSD with Ports Collection
2013-12-22 21:04:53 -08:00
On FreeBSD, you can use Ports Collection. Install [net/msgpack ](http://www.freebsd.org/cgi/cvsweb.cgi/ports/devel/msgpack/ ) package.
2013-12-17 08:53:15 +10:30
##### Gentoo Linux with Portage
2013-12-22 21:04:53 -08:00
On Gentoo Linux, you can use emerge. Install [dev-libs/msgpack ](http://gentoo-portage.com/dev-libs/msgpack ) package.
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
##### Mac OS X with MacPorts
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
On Mac OS X, you can install MessagePack for C using MacPorts.
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
```
$ sudo port install msgpack
```
You might need to run `sudo port selfupdate` before installing to update the package repository.
You can also install via Homebrew.
```
$ sudo brew install msgpack
```
##### Windows
2014-06-07 00:13:23 +09:00
Clone msgpack-c git repository.
2013-12-17 08:53:15 +10:30
```
2014-06-07 00:13:23 +09:00
$ git clone https://github.com/msgpack/msgpack-c.git
2013-12-17 08:53:15 +10:30
```
2013-01-03 19:13:28 -08:00
2014-06-07 00:13:23 +09:00
or using GUI git client.
e.g.) tortoise git https://code.google.com/p/tortoisegit/
Launch cmake GUI client. http://www.cmake.org/cmake/resources/software.html
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.
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
### Linking with an Application
2013-01-03 19:13:28 -08:00
2013-12-17 08:53:15 +10:30
Include `msgpack.hpp` (or `msgpack.h` for C) in your application and link with libmsgpack. Here is a typical gcc link command:
2014-06-01 19:03:51 +00:00
g++ myapp.cpp -lmsgpack -o myapp
2013-12-17 08:53:15 +10:30
### Code Example
2013-08-17 03:11:15 +02:00
```CPP
#include <msgpack.hpp>
#include <vector>
2014-04-24 22:39:01 +00:00
#include <string>
#include <iostream>
2013-12-17 08:53:15 +10:30
2014-04-24 22:39:01 +00:00
int main() {
2013-08-17 03:11:15 +02:00
// This is target object.
std::vector< std::string > target;
target.push_back("Hello,");
target.push_back("World!");
2013-12-17 08:53:15 +10:30
2013-08-17 03:11:15 +02:00
// Serialize it.
msgpack::sbuffer sbuf; // simple buffer
msgpack::pack(& sbuf, target);
2013-12-17 08:53:15 +10:30
2013-08-17 03:11:15 +02:00
// Deserialize the serialized data.
msgpack::unpacked msg; // includes memory pool and deserialized object
msgpack::unpack(& msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
2013-12-17 08:53:15 +10:30
2013-08-17 03:11:15 +02:00
// Print the deserialized object to stdout.
std::cout < < obj < < std::endl ; / / [ " Hello , " " World ! " ]
2013-12-17 08:53:15 +10:30
2013-08-17 03:11:15 +02:00
// Convert the deserialized object to staticaly typed object.
std::vector< std::string > result;
obj.convert(&result);
2013-12-17 08:53:15 +10:30
2013-08-17 03:11:15 +02:00
// If the type is mismatched, it throws msgpack::type_error.
obj.as< int > (); // type is mismatched, msgpack::type_error is thrown
}
```
2013-12-17 08:53:15 +10:30
### Quickstart Guides
2013-08-17 03:11:15 +02:00
2013-12-17 08:53:15 +10:30
For more detailed examples see [QuickStart for C ](QUICKSTART-C.md ) and [QuickStart for C++ ](QUICKSTART-CPP.md ).