Skip to content
MessagePack implementation for C and C++ / msgpack.org[C/C++]
Branch: master
Clone or download
Latest commit b759f5b Feb 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ci Updated travis-ci environment. Aug 12, 2018
erb/v1
example Fixed #752. Dec 19, 2018
external/boost
fuzz add a test file for regression tests, cmakelist.txt to get it build c… May 19, 2018
include Fixed #763. Feb 12, 2019
src Add support for Windows Drivers Jan 17, 2019
test
.gitignore
.gitmodules
.travis.yml Updated travis-ci environment. Aug 12, 2018
CHANGELOG.md
CMakeLists.txt Merge pull request #721 from dennisklein/master Aug 16, 2018
COPYING Version 1.3.0. Nov 21, 2015
Doxyfile
Files.cmake Added wstring adaptor. Sep 12, 2018
LICENSE_1_0.txt
NOTICE
QUICKSTART-C.md Don't sudo with brew in QUICKSTART-C.md Oct 1, 2017
QUICKSTART-CPP.md
README.md Updated the version to 3.1.1. Aug 27, 2018
appveyor.yml Merge pull request #727 from redboltz/update_311 Sep 9, 2018
make_file_list.sh Fix typo that caused the C++ headers not to be included in cmake file… Oct 30, 2016
makedist.sh
msgpack-config.cmake.in Add cmake package config support Apr 4, 2017
msgpack.pc.in Fixed #395 Jan 13, 2016
msgpack_vc8.sln Add msvc project back Jun 7, 2016
msgpack_vc8.vcproj
preprocess Re-organized tree to prepare for version 2.0.0. Jan 31, 2016
update_version.sh Added version updating script. Aug 3, 2017

README.md

msgpack for C/C++

Version 3.1.1 Build Status Build status

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

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::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

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 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 or C++17 version of msgpack instead, execute the following commands:

$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ 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.

When you use the C part of msgpack-c, you need to build and link the library. By default, both static/shared libraries are built. If you want to build only static library, set BUILD_SHARED_LIBS=OFF to cmake. If you want to build only shared library, set `BUILD_SHARED_L

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 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.

You can’t perform that action at this time.