Msgpack23 —— 一个现代的、仅包含头文件的 C++ MessagePack (反)序列化库
Msgpack23 – A modern, header-only C++ library for MessagePack (de)serialization

原始链接: https://github.com/rwindegger/msgpack23

`msgpack23` 是一个现代的、仅包含头文件的 C++20 库,它提供了一种简单直接的方法来使用 MessagePack 格式序列化和反序列化数据。它轻量级,不需要外部依赖,并利用 C++ 的特性,例如概念和模板,以提高灵活性。 该库开箱即用地支持各种数据类型,包括基本类型(整数、浮点数、布尔值、字符串)、STL 容器(向量、映射)和 `std::chrono::time_point` 对象。它也能正确处理字节序。 对于自定义数据结构,您可以定义 `pack` 和 `unpack` 成员函数,将其自动集成到序列化过程中。该库通过单个头文件、最小的性能开销以及能够以最少的样板代码序列化各种数据类型来提高简洁性。欢迎贡献、错误报告和功能请求。采用 MIT 许可证。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Msgpack23 – 一个现代的、仅包含头文件的 C++ MessagePack (反)序列化库 (github.com/rwindegger) 3 分,来自 gjvc,14 分钟前 | 隐藏 | 过去 | 收藏 | 讨论 加入我们,参加 6 月 16-17 日在旧金山举办的 AI 初创企业学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:

原文

A modern, header-only C++ library for MessagePack serialization and deserialization.

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.

  • 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 pack and unpack member 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_point objects.
  • Variety of Primitive Types: Integers (signed/unsigned), booleans, floating-point, std::string, byte arrays, and nullptr are all supported out-of-the-box.
  • Endian-Aware: Properly handles endianness using std::endian and std::byteswap to ensure portability.
  1. Clone the Repository

    git clone https://github.com/rwindegger/msgpack23.git
  2. Include the Header
    Since this is a header-only library, just include the main header in your project:

  3. 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
        msgpack23::Packer packer;
        auto packedData = 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;
    }

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>
   std::vector<std::byte> pack(T &packer) const {
      return 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" };
auto const data = msgpack23::pack(my_data);
auto obj = msgpack23::unpack<MyData>(data);
  • 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.

Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.

  1. Fork it!
  2. Create your feature branch: git checkout -b feature/my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature/my-new-feature
  5. Submit a pull request

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.

联系我们 contact @ memedata.com