Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
MessagePack implementation for C and C++ / msgpack.org[C/C++]

Merge pull request #222 from jszakmeister/fix-byteswap-freebsd-and-op…

…enbsd

Don't include byteswap.h and FreeBSD and OpenBSD.
latest commit f6d0cd9a4b
Takatoshi Kondo redboltz authored
Failed to load latest commit information.
ci Merge branch 'poc/0.6' of https://github.com/redboltz/msgpack-c into …
erb Fixed #204, #202.
example Merge pull request #212 from redboltz/add_zone_reuse_example
include Don't include byteswap.h and FreeBSD and OpenBSD.
src Add range-based for loop support.
test Replaced auto& with auto const& for msgpack::object.
.gitignore Add a list of contributors in README.md and remove AUTHORS file
.travis.yml Added a 32bit environment tests for travis-ci.
CHANGELOG.md misc top level directory cleanup
CMakeLists.txt Fix cmake for subproject usage
COPYING reorganized tree
CROSSLANG.md misc top level directory cleanup
Doxyfile reorganized tree
LICENSE reorganized tree
Makefile.am Supported make install.
QUICKSTART-C.md Imported quickstarts from wiki
QUICKSTART-CPP.md Fix typo in QUICKSTART-CPP
README.md Add a list of contributors in README.md and remove AUTHORS file
bootstrap Added cmake files.
cases.json reorganized tree
cases_gen.rb reorganized tree
configure.in Support OS X's sed in configure.in
crosslang.cc Fixed linking order.
crosslang.rb reorganized tree
msgpack.pc.in reorganized tree
msgpack_vc.postbuild.bat Add fbuffer.h and fbuffer.hpp to CMakeLists.txt and msgpack_vc.postbu…
msgpack_vc8.sln reorganized tree
msgpack_vc8.vcproj Moved bool and inline definition from msgpack.h to sysdep.h.
preprocess Separated adaptor functions' definitions and declarations.

README.md

msgpack for C/C++

Version 0.5.9 Build Status

It's like JSON but small and fast.

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 while typical short strings require only one extra byte in addition to the strings themselves.

Example

In C:

#include <msgpack.h>
#include <stdio.h>

int main(void)
{
    /* msgpack::sbuffer is a simple buffer implementation. */
    msgpack_sbuffer sbuf;
    msgpack_sbuffer_init(&sbuf);

    /* serialize values into the buffer using msgpack_sbuffer_write callback function. */
    msgpack_packer pk;
    msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

    msgpack_pack_array(&pk, 3);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_true(&pk);
    msgpack_pack_str(&pk, 7);
    msgpack_pack_str_body(&pk, "example", 7);

    /* deserialize the buffer into msgpack_object instance. */
    /* deserialized object is valid during the msgpack_zone instance alive. */
    msgpack_zone mempool;
    msgpack_zone_init(&mempool, 2048);

    msgpack_object deserialized;
    msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

    /* print the deserialized object. */
    msgpack_object_print(stdout, deserialized);
    puts("");

    msgpack_zone_destroy(&mempool);
    msgpack_sbuffer_destroy(&sbuf);

    return 0;
}

See QUICKSTART-C.md for more details.

In C++:

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

int main(void)
{
    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::unpacked result;

    msgpack::unpack(result, str.data(), str.size());

    // deserialized object is valid during the msgpack::unpacked instance alive.
    msgpack::object deserialized = result.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);

    return 0;
}

See QUICKSTART-CPP.md for more details.

Usage

C++ Header Only Library

When you use msgpack on C++03 and C++11, you can just add msgpack-c/include to your include path:

g++ -I msgpack-c/include your_source_file.cpp

If you want to use C version of msgpack, you need to build it. You can also install the C and C++ versions of msgpack.

Building and Installing

Install from git repository

Using autotools

You will need:

  • gcc >= 4.1.0 or clang >= 3.3.0
  • autoconf >= 2.60
  • automake >= 1.10
  • libtool >= 2.2.4

The build steps below are for C and C++03. If compiling for C++11, add -std=c++11 to the environmental variable CXXFLAGS with export CXXFLAGS="$CXXFLAGS -std=c++11" prior to following the directions below.

$ git clone https://github.com/msgpack/msgpack-c
$ cd msgpack-c
$ ./bootstrap
$ ./configure
$ make

You can install the resulting library like this:

$ sudo make install
Using cmake
Using the Terminal (CLI)

You will need:

  • gcc >= 4.1.0
  • cmake >= 2.8.0

C and C++03:

$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ cmake .
$ make
$ sudo make install

If you want to setup C++11 version of msgpack instead, execute the following commands:

$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ cmake -DMSGPACK_CXX11=ON .
$ sudo make install
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/

  1. Launch cmake GUI client.

  2. Set 'Where is the source code:' text box and 'Where to build the binaries:' text box.

  3. Click 'Configure' button.

  4. Choose your Visual Studio version.

  5. Click 'Generate' button.

  6. Open the created msgpack.sln on Visual Studio.

  7. Build all.

Documentation

You can get addtional information 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 Apache License Version 2.0. See the LICENSE file for details.

Something went wrong with that request. Please try again.