Austriarwindegger/msgpack23
A modern, header-only C++ library for MessagePack serialization and deserialization. msgpack.org[c++23]
Featured achievements
The project's latest awards
#2 Repository of the month
#2 Repository of the month
#6 Repository of the month
msgpack23
A modern, header-only C++ library for MessagePack serialization and deserialization.
Overview
msgpack23 is a lightweight library that provides a straightforward approach to serializing and deserializing C++ data structures into the MessagePack format. It is written in modern C++ (targeting C++20 and beyond) and leverages templates and type traits to provide a flexible, zero-dependency solution for packing and unpacking various data types.
Key Features
- Header-only: Simply include the header and start using it—no additional build steps or dependencies.
- Modern C++: Uses C++ features like concepts to handle containers, maps, enums, time points, and user-defined types.
- Extensible: Allows you to define custom types by implementing
packandunpackmember functions, automatically integrating them into the serialization pipeline. - Collection and Map Support: Automatically detects and serializes STL containers (e.g.,
std::vector,std::map) without extra work. - Time Point Support: Native support for serializing
std::chrono::time_pointobjects. - Variety of Primitive Types: Integers (signed/unsigned), booleans, floating-point,
std::string, byte arrays, andnullptrare all supported out-of-the-box. - Endian-Aware: Properly handles endianness using
std::endianandstd::byteswapto ensure portability.
Getting Started
-
Clone the Repository
git clone https://github.com/rwindegger/msgpack23.git -
Include the Header
Since this is a header-only library, just include the main header in your project:#include "msgpack23.h" -
Pack and Unpack
#include <iostream> #include <map> #include "msgpack23.hpp" int main() { // Create a map of some data std::map<std::string, int> original {{"apple", 1}, {"banana", 2}}; // 1) Pack into a vector of std::byte std::vector<std::byte> packedData{}; auto const inserter = std::back_insert_iterator(packedData); msgpack23::Packer packer{inserter}; packer(original); // 2) Unpack back into a map std::map<std::string, int> unpacked; msgpack23::Unpacker unpacker(packedData); unpacker(unpacked); // Verify the result for (auto const& [key, value] : unpacked) { std::cout << key << ": " << value << "\n"; } return 0; }
Custom Types
To serialize your own types, define a pack and unpack function. The pack should accept a T & and the unpack should accept a T &.
struct MyData {
int64_t my_integer;
std::string my_string;
template<typename T>
void pack(T &packer) const {
packer(my_integer, my_string);
}
template<typename T>
void unpack(T &unpacker) {
unpacker(my_integer, my_string);
}
};
Now you can use MyData with msgpack23 just like any built-in type:
MyData const my_data {42, "Hello" };
std::vector<std::byte> data{};
auto const inserter = std::back_insert_iterator(data);
msgpack23::pack(my_data);
auto obj = msgpack23::unpack<MyData>(data);
Why msgpack23?
- Simplicity: A single header with clearly structured pack/unpack logic.
- Performance: Minimal overhead by using direct memory operations and compile-time type deductions.
- Flexibility: From primitive types and STL containers to custom structures, everything can be serialized with minimal boilerplate.
Contributing
Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.
- Fork it!
- Create your feature branch:
git checkout -b feature/my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/my-new-feature - Submit a pull request
License
This project is licensed under the MIT License.
Happy packing (and unpacking)! If you have any questions or feedback, please open an issue or start a discussion.
Similar Projects
Discover other similar projects and AI models.
Extremely Fast MessagePack Serializer for C#(.NET, .NET Core, Unity, Xamarin). / msgpack.org[C#]
A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
MPack - A C encoder/decoder for the MessagePack serialization format / msgpack.org[C]
Convert between CBOR, JSON, MessagePack, TOML, and YAML 1.1 & 1.2
A pure PHP implementation of the MessagePack serialization format / msgpack.org[PHP]
A minimal yet complete MessagePack implementation for JavaScript. msgpack.org[JavaScript]
Julia MessagePack implementation with type-driven, overloadable packing/unpacking functionality // msgpack.org[Julia]