当前位置: 首页 > news >正文

C++ Cereal序列化库的使用

C++ Cereal 库使用指南

Cereal 是一个轻量级的 C++ 序列化库,用于将对象序列化为二进制、XML 或 JSON 格式,以及从这些格式反序列化。它支持标准库类型和用户自定义类型的序列化,且无需修改原有类定义。

基本用法

1. 安装与包含

#include <cereal/archives/binary.hpp>
#include <cereal/types/vector.hpp>  // 包含对vector的支持
#include <cereal/types/string.hpp> // 包含对string的支持

2. 基本序列化/反序列化

#include <fstream>
#include <iostream>
#include <vector>

struct MyData {
    int x;
    double y;
    std::string z;
    std::vector<int> nums;

    // 序列化函数模板
    template<class Archive>
    void serialize(Archive & archive) {
        archive(x, y, z, nums); // 序列化所有成员
    }
};

int main() {
    MyData data1{42, 3.14, "hello", {1, 2, 3}};
    
    // 序列化到文件
    {
        std::ofstream os("data.bin", std::ios::binary);
        cereal::BinaryOutputArchive archive(os);
        archive(data1); // 写入数据
    }

    // 从文件反序列化
    MyData data2;
    {
        std::ifstream is("data.bin", std::ios::binary);
        cereal::BinaryInputArchive archive(is);
        archive(data2); // 读取数据
    }

    std::cout << data2.x << ", " << data2.y << ", " << data2.z << std::endl;
    return 0;
}

高级特性

1. 分离加载和保存

struct MyClass {
    int a;
    std::string b;

    template<class Archive>
    void save(Archive & archive) const {
        archive(a, b);
    }

    template<class Archive>
    void load(Archive & archive) {
        archive(a, b);
    }
};

2. 版本控制

struct VersionedData {
    int x;
    std::string y;

    template<class Archive>
    void serialize(Archive & ar, const std::uint32_t version) {
        ar(x, y);
        if(version >= 2) {
            // 版本2新增的字段
        }
    }
};

// 注册版本
CEREAL_CLASS_VERSION(VersionedData, 2)

3. 指针和智能指针

struct Node {
    int value;
    std::shared_ptr<Node> next;

    template<class Archive>
    void serialize(Archive & ar) {
        ar(value, next);
    }
};

4. 继承

struct Base {
    int x;
    template<class Archive>
    void serialize(Archive & ar) {
        ar(x);
    }
};

struct Derived : Base {
    double y;
    template<class Archive>
    void serialize(Archive & ar) {
        ar(cereal::base_class<Base>(this), y);
    }
};

不同格式支持

1. 二进制格式

#include <cereal/archives/binary.hpp>

// 输出
std::ofstream os("data.bin", std::ios::binary);
cereal::BinaryOutputArchive archive(os);
archive(data);

// 输入
std::ifstream is("data.bin", std::ios::binary);
cereal::BinaryInputArchive archive(is);
archive(data);

2. JSON格式

#include <cereal/archives/json.hpp>

// 输出
std::ofstream os("data.json");
cereal::JSONOutputArchive archive(os);
archive(cereal::make_nvp("MyData", data)); // 使用命名

// 输入
std::ifstream is("data.json");
cereal::JSONInputArchive archive(is);
archive(data);

3. XML格式

#include <cereal/archives/xml.hpp>

// 输出
std::ofstream os("data.xml");
cereal::XMLOutputArchive archive(os);
archive(cereal::make_nvp("MyData", data));

// 输入
std::ifstream is("data.xml");
cereal::XMLInputArchive archive(is);
archive(data);

实际应用示例

1. 序列化自定义类

class Person {
public:
    Person() = default;
    Person(std::string n, int a) : name(n), age(a) {}
    
private:
    std::string name;
    int age;
    std::vector<std::string> friends;
    
    friend class cereal::access;
    
    template<class Archive>
    void serialize(Archive & ar) {
        ar(name, age, friends);
    }
};

2. 序列化STL容器

std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}};
std::vector<double> myVec = {1.1, 2.2, 3.3};

{
    std::ofstream os("container.bin", std::ios::binary);
    cereal::BinaryOutputArchive archive(os);
    archive(myMap, myVec);
}

3. 多态类型序列化

struct Base {
    virtual void foo() = 0;
    virtual ~Base() = default;
    
    template<class Archive>
    void serialize(Archive & ar) {}
};

struct Derived : Base {
    int x;
    void foo() override {}
    
    template<class Archive>
    void serialize(Archive & ar) {
        ar(cereal::base_class<Base>(this), x);
    }
};

// 注册多态类型
CEREAL_REGISTER_TYPE(Derived)

注意事项

  1. 默认构造函数:反序列化的类必须有可访问的默认构造函数

  2. 内存管理:序列化原始指针时需要特别小心,推荐使用智能指针

  3. 版本兼容性:修改类结构后,旧版本数据可能无法正确反序列化

  4. 性能:二进制格式通常比文本格式更高效

  5. 可移植性:二进制数据在不同平台间可能存在兼容性问题

Cereal 是一个强大而灵活的序列化库,通过简单的接口实现了复杂对象的序列化功能,非常适合需要持久化或网络传输C++对象的场景。

深入学习链接(参考🔗)C++序列化Cereal库的使用-CSDN博客

相关文章:

  • C++Cherno 学习笔记day19 [76]-[80] std::optional、variant、any
  • java.lang.OutOfMemoryError: GC overhead limit exceeded如何解决
  • 《Python星球日记》第23天:Pandas基础
  • AGI|AutoGen入门食用手册,搭建你的智能体流水线
  • Position属性实现元素精准定位
  • source insight编码对齐与中文乱码问题以及CRT中文乱码处理
  • 基于支持向量回归(SVR)的空气质量预测
  • 电商中的购物车(redis的hash类型操作)
  • NutriJarvis:AI慧眼识餐,精准营养触手可及!—— 基于深度学习的菜品识别与营养计算系统
  • halcon模板匹配(一)create_shape_model_xld
  • Python全栈开发项目实战——日历事件管理系统
  • QuarkPi-CA2 RK3588S卡片电脑:6.0Tops NPU+8K视频编解码+接口丰富,高性能嵌入式开发!
  • 2020年INS SCI1区TOP:平衡复合运动优化算法BCMO,深度解析+性能实测
  • Unity VideoPlayer 播放无声音
  • 【leetcode hot 100 300】最长递增子序列
  • NoV病毒抗原抗体,助力疫苗研究与诊断试剂开发!
  • 大型语言模型智能应用Coze、Dify、FastGPT、MaxKB 对比,选择合适自己的LLM工具
  • 某局jsvmp算法分析(dunshan.js/lzkqow23819/lzkqow39189)
  • BERT - 段嵌入(Segment Embedding)
  • Composer安装Laravel步骤
  • 中国免费素材网站/搜索引擎有哪些网站
  • 杭州市建设网官网/优化关键词排名哪家好
  • 快速网站备案多少钱/域名备案查询系统
  • nba网站开发毕业论文/网络推广计划书范文
  • 用macbook做网站开发吗/seo教程自学网
  • 山东爱易网站建设工作室/软文代写价格