Msgpack11 - A tiny MessagePack library for C++11 (msgpack.org[C++11])

Overview

Build Status

What is msgpack11 ?

msgpack11 is a tiny MsgPack library for C++11, providing MsgPack parsing and serialization.
This library is inspired by json11.
The API of msgpack11 is designed to be similar with json11.

Installation

  • Using CMake

      git clone [email protected]:ar90n/msgpack11.git
      mkdir build
      cd build
      cmake ../msgpack11
      make && make install
    
  • Using Buck

      git clone [email protected]:ar90n/msgpack11.git
      cd msgpack11
      buck build :msgpack11
    

Example

MsgPack my_msgpack = MsgPack::object {
    { "key1", "value1" },
    { "key2", false },
    { "key3", MsgPack::array { 1, 2, 3 } },
};

//access to elements
std::cout << my_msgpack["key1"].string_value();

//serialize
std::string msgpack_bytes = my_msgpack.dump();

//deserialize
std::string err;
MsgPack des_msgpack = MsgPack::parse(msgpack_bytes, err);

There are more specific examples in example.cpp. Please see it.

Benchmark

Derived from schemaless-benchmarks

Library Binary size time[ms] @ Smallest time[ms] @ Small time[ms] @ Medium time[ms] @ Large time[ms] @ Largest
msgpack-c-pack(v2.1.4) 6649 0.55 2.38 43.22 711.75 8748.20
msgpack-c-unpack(v2.1.4) 21804 1.34 6.00 83.09 714.64 11192.32
msgpack11-pack(v0.0.9) 99844 20.80 130.04 1063.24 10466.65 136640.99
msgpack11-unpack(v0.0.9) 99460 13.31 92.54 786.73 7345.43 99119.56

CPU : 2.6 GHz Intel Core i7
Memory : 16 GB 2133 MHz LPDDR3
Git revision : 6f6b4302b68b3c88312eb24367418b7fce81298c

Feature

  • Support serialization and deserialization.

Acknowledgement

License

This software is released under the MIT License, see LICENSE.txt.

Comments
  • Sending complex objects over the network and deserializing them

    Sending complex objects over the network and deserializing them

    I'm currently using this implementation of MessagePack for serializing/deserializing somewhat complex structures in a project I've been developing.

    I want to be able to send and receive serialized MessagePack Objects that can then be parsed and constructed in order to build the "complex structure" on the receiving end.

    The complex structure in question is a Blockchain. My Blockchain class consists of an array of Blocks (Block class instances) which themselves contain an array of Transactions (Transaction class instances).

    I was able to serialize the Blockchain class by implementing serialize methods in each of the classes: Blockchain::serialize(), Block::serialize(), Transaction::serialize().

    After serializing the Blockchain, I would send the dump() output of that object to the receiving end, in order for it to construct it's own Blockchain from the incoming packet.

    The problem is that, after serializing the Blockchain, I can access it's elements on the Sender Node (the one where the serialized blockchain originated from); however, I cannot to this on the Receiver Node. I get the MessagePack dump string but I am not able to parse the incoming buffer. I've checked the parse error and it's populated with the message "end of buffer".

    I'm not sure if this problem has to do with the way I try to serialize and construct the classes, or if it has to do with how I send the MessagePack dump() output to the receiver Node.

    This issue might not have been very explicit but I am not sure how I can explain it better. I'm just looking for help to figure out what's causing this. I can get into further detail if someone's interested in helping out.

    Also, keep in mind that I am a young dev, and I'm quite new to C++. The code may look like crap, be advised.

    opened by JoaoAJMatos 6
  • Support stream insertion/extraction API

    Support stream insertion/extraction API

    Hello again!

    I've been working to support the C++ streams API on my fork (working branch: https://github.com/alwaysmpe/msgpack11/tree/stream_api) so thought I would open this issue to discuss.

    It's a work in progress at the moment, but stream insertion is (I believe) working correctly and I've integrated some tests into the existing code. I will write separate tests but, as I said, it's a work in progress (and I've not used gtest before so getting started quickly by modifying the existing tests). I probably won't be able to finish the stream extraction stuff until next week.

    opened by alwaysmpe 5
  • Comparing uint64 with int64 causes error when compiling with -Werror=sign-compare

    Comparing uint64 with int64 causes error when compiling with -Werror=sign-compare

    Can be fixed with following patch:

    diff --git a/msgpack11.cpp b/msgpack11.cpp
    index ef87111..6bc6342 100644
    --- a/msgpack11.cpp
    +++ b/msgpack11.cpp
    @@ -409,21 +409,21 @@ protected:
     bool equal_uint64_int64( uint64_t uint64_value, int64_t int64_value )
     {
         bool const is_positive = 0 <= int64_value;
    -    bool const is_leq_int64_max = uint64_value <= std::numeric_limits<int64_t>::max();
    +    bool const is_leq_int64_max = uint64_value <= static_cast<std::uint64_t>(std::numeric_limits<int64_t>::max());
         return is_positive && is_leq_int64_max && ( uint64_value == static_cast<uint64_t>(int64_value));
     }
     
     bool less_uint64_int64( uint64_t uint64_value, int64_t int64_value )
     {
         bool const is_positive = 0 <= int64_value;
    -    bool const is_leq_int64_max = uint64_value <= std::numeric_limits<int64_t>::max();
    +    bool const is_leq_int64_max = uint64_value <= static_cast<std::uint64_t>(std::numeric_limits<int64_t>::max());
         return is_positive && is_leq_int64_max && ( uint64_value < static_cast<uint64_t>(int64_value));
     }
     
     bool less_int64_uint64( int64_t int64_value, uint64_t uint64_value )
     {
         bool const is_negative = int64_value < 0;
    -    bool const is_gt_int64_max = std::numeric_limits<int64_t>::max() < uint64_value;
    +    bool const is_gt_int64_max = static_cast<std::uint64_t>(std::numeric_limits<int64_t>::max()) < uint64_value;
         return is_negative || is_gt_int64_max || ( static_cast<uint64_t>(int64_value) < uint64_value );
     }
    
    opened by aiplemaSICKAG 2
  • Should is_int() return true for all integers? Same for is_number().

    Should is_int() return true for all integers? Same for is_number().

    Looking at the implementation of MsgPack is_int() it only returns true for int_32_t

    To me the logical approach would be to mimic JSON type specification, ie is_int() would return true for all encoded int types (including unsigned) and is_number() should return true for all numbers (floats and ints).

    therefore change is_int() to: is_int(){ return is_int8() || is_int16() || is_int32() || is_int64() || is_uint8 || is_uint16 || is_uint32() || is_uint64();} and is_number() to: is_number(){return is_float32() || is_float64() || is_int();}

    It might be possible to make this cleaner using bitmasks but I've not had coffee yet so can't think.

    But thanks for a nice c++11 friendly implementation!

    opened by alwaysmpe 2
  • nullptr_t comparsion

    nullptr_t comparsion

    When building v0.0.7 for Android with NDK 15.0.4075724 using Clang I got a following error message:

    msgpack11.cpp:360:58: error: invalid operands to binary expression ('nullptr_t' and 'nullptr_t')
        return is_less_type || (is_same_type && (m_value < static_cast<const Value<tag, T> *>(other)->m_value)); 
    
    msgpack11.cpp:623:5: note: in instantiation of member function 'msgpack11::Value<msgpack11::MsgPack::Type::NUL, nullptr_t>::less' requested here
        MsgPackNull() : Value(nullptr) {}  
    
    opened by mikcza 2
  • Implement stream insertion and extraction API

    Implement stream insertion and extraction API

    As discussed in issue #7 I've added support for the C++ streams API. As we're both happy and there has been no noticeable performance impact, and all tests pass I propose merging.

    opened by alwaysmpe 1
  • Correct definition of is_int() and is_number()

    Correct definition of is_int() and is_number()

    All ints should return true for is_int(), regardless of how many bits are used to encode and all numbers should return true for is_number(). Use bit-mask to implement efficiently. (NB: All ints are numbers, but not all numbers are ints).

    This is in response to issue #5

    opened by alwaysmpe 1
  • NumberValue impls, comparing wrong value?

    NumberValue impls, comparing wrong value?

    Hi,

    I was looking over your code and noticed that all the NumberValue implementations are all comparing to float64_value(). Is this what you meant to do? I would assume for UINT32 you would want to call "other->uint32_value()" as an example.

    class MsgPackUint32 final : public NumberValue<MsgPack::UINT32, uint32_t> { bool equals(const MsgPackValue * other) const override { return m_value == other->float64_value(); } bool less(const MsgPackValue * other) const override { return m_value < other->float64_value(); }

    opened by scott4moore 1
  • Is it only for json?

    Is it only for json?

    Hello, is this library suited only for packing jsons? What if I just need to serialize data in such a format:

    struct example {
        string id;
        vector<object> v;
    };
    
    struct object {
        long a;
        double b;
        double c;
        int d;
        bool e;
    }
    

    ? Without any {, ", keys and so on?

    opened by jkryszyn 0
  • Compile using VS?

    Compile using VS?

    Looks like the Visual Studio compiler doesn't like a few things in here if "warning as error" is turned on:

    a) the dump methods all fail with "warning 4309: 'argument: truncation of constant value" b) the int/uint comparitor functions give a "warning 4018: ',=': signed/unsigned mismatch"

    Any thoughts on whether it's safe to ignore those warnings? It seems like it's not...

    opened by jrkst34 0
  • add parsing examples

    add parsing examples

    Would be nice to have some parsing example , I suppose if I have an object containing a string with "route" key I can get the data like this:

    msg["route"].string_value()

    but having a complete example would be great.

    opened by ad34 2
Owner
Masahiro Wada
Masahiro Wada
An implementation of the MessagePack serialization format in C / msgpack.org[C]

CMP CMP is a C implementation of the MessagePack serialization format. It currently implements version 5 of the MessagePack Spec. CMP's goal is to be

Charlie Gunyon 290 Dec 31, 2022
MPack - A C encoder/decoder for the MessagePack serialization format / msgpack.org[C]

Introduction MPack is a C implementation of an encoder and decoder for the MessagePack serialization format. It is: Simple and easy to use Secure agai

Nicholas Fraser 419 Jan 1, 2023
Your binary serialization library

Bitsery Header only C++ binary serialization library. It is designed around the networking requirements for real-time data delivery, especially for ga

Mindaugas Vinkelis 771 Jan 2, 2023
Cap'n Proto serialization/RPC system - core tools and C++ library

Cap'n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except

Cap'n Proto 9.5k Jan 1, 2023
A C++11 library for serialization

cereal - A C++11 library for serialization cereal is a header-only C++11 serialization library. cereal takes arbitrary data types and reversibly turns

iLab @ USC 3.4k Jan 3, 2023
Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockford's base32. MIT licensed with consistent, flexible API.

cppcodec Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockf

Topology 491 Dec 28, 2022
FlatBuffers: Memory Efficient Serialization Library

FlatBuffers FlatBuffers is a cross platform serialization library architected for maximum memory efficiency. It allows you to directly access serializ

Google 19.7k Jan 9, 2023
FlatBuffers Compiler and Library in C for C

OS-X & Ubuntu: Windows: The JSON parser may change the interface for parsing union vectors in a future release which requires code generation to match

null 550 Dec 25, 2022
Simple C++ 20 Serialization Library that works out of the box with aggregate types!

BinaryLove3 Simple C++ 20 Serialization Library that works out of the box with aggregate types! Requirements BinaryLove3 is a c++20 only library.

RedSkittleFox 14 Sep 2, 2022
A C++ library for interacting with JSON.

JsonCpp JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value p

null 6.9k Jan 4, 2023
A header only C++11 library for parsing TOML

tinytoml A header only C++11 library for parsing TOML. This parser is based on TOML v0.4.0. This library is distributed under simplified BSD License.

mayah 157 Dec 22, 2022
cpptoml is a header-only library for parsing TOML

cpptoml A header-only library for parsing TOML configuration files. Targets: TOML v0.5.0 as of August 2018. This includes support for the new DateTime

Chase Geigle 561 Dec 28, 2022
A C++11 or library for parsing and serializing JSON to and from a DOM container in memory.

Branch master develop Azure Docs Drone Matrix Fuzzing --- Appveyor codecov.io Boost.JSON Overview Boost.JSON is a portable C++ library which provides

Boost.org 333 Dec 29, 2022
Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications

Zmeya Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications. Zmeya is not even a serializ

Sergey Makeev 99 Dec 24, 2022
CppSerdes is a serialization/deserialization library designed with embedded systems in mind

A C++ serialization/deserialization library designed with embedded systems in mind

Darren V Levine 79 Nov 5, 2022
Morse code decoding library

ggmorse Morse code decoding library ggmorse2.mp4 ggmorse0.mp4 ggmorse1.mp4 Try it out You can easily test the library using the free GGMorse applicati

Georgi Gerganov 106 Dec 23, 2022
Header-only library for automatic (de)serialization of C++ types to/from JSON.

fuser 1-file header-only library for automatic (de)serialization of C++ types to/from JSON. how it works The library has a predefined set of (de)seria

null 51 Dec 17, 2022
C++17 library for all your binary de-/serialization needs

blobify blobify is a header-only C++17 library to handle binary de-/serialization in your project. Given a user-defined C++ struct, blobify can encode

Tony Wasserka 247 Dec 8, 2022
C++ BSON Library

This is a standalone BSON ("binary JSON") library for C++. (See bsonspec.org for more information on the BSON format.) The library is at this time a b

Dwight Merriman 57 Dec 16, 2022