cereal for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is cereal and why is it needed?

\n\n

cereal is a lightweight, header-only library for data serialization in C++. It allows you to convert complex data structures (classes, structs, STL containers) into formats such as JSON, XML, and binary format, and then restore them back. This is critically important for saving program state, transmitting data over a network, logging, or exchanging information between different programming languages.

\n\n

Unlike heavier solutions (e.g., Boost.Serialization), cereal does not require pre-compilation or complex setup. It consists entirely of header files, simplifying integration into any project. Key advantages:

\n
    \n
  • Header-only: no need to build the library separately.
  • \n
  • Support for C++11/14/17: uses modern language features.
  • \n
  • Flexibility: supports custom types, smart pointers, polymorphism.
  • \n
  • Performance: the binary format is very fast and compact.
  • \n
\n\n

Installation

\n\n

Since cereal is a header-only library, installation boils down to copying the header files into your project. No pip install or npm install is required for C++. Here are two main methods:

\n\n

Method 1: Manual Installation (recommended for learning)

\n
    \n
  1. Download the latest version from GitHub (green "Code" button → "Download ZIP").
  2. \n
  3. Unzip the archive.
  4. \n
  5. Copy the include/cereal folder into your project folder.
  6. \n
  7. In code, include: #include "cereal/cereal.hpp".
  8. \n
\n\n

Method 2: Via the vcpkg package manager

\n
# Install vcpkg if not already installed\ngit clone https://github.com/Microsoft/vcpkg.git\ncd vcpkg\n.\\bootstrap-vcpkg.bat  # Windows\n./bootstrap-vcpkg.sh   # Linux/macOS\n\n# Install cereal\n.\\vcpkg install cereal  # Windows\n./vcpkg install cereal  # Linux/macOS\n\n# Include in CMakeLists.txt:\n# find_package(cereal CONFIG REQUIRED)\n# target_link_libraries(your_project PRIVATE cereal::cereal)\n
\n\n

Quick Start with Code Example

\n\n

Let's create a simple Person struct and serialize it to JSON. Create a file main.cpp:

\n\n
#include <iostream>\n#include <fstream>\n#include <string>\n#include <cereal/archives/json.hpp>\n#include <cereal/types/string.hpp>\n\nstruct Person {\n    std::string name;\n    int age;\n\n    // Required method for cereal\n    template<class Archive>\n    void serialize(Archive& archive) {\n        archive(name, age);\n    }\n};\n\nint main() {\n    Person p1{"Alice", 30};\n\n    // Write to file\n    {\n        std::ofstream file("person.json");\n        cereal::JSONOutputArchive archive(file);\n        archive(CEREAL_NVP(p1));  // CEREAL_NVP — for named fields\n    }\n\n    // Read from file\n    Person p2;\n    {\n        std::ifstream file("person.json");\n        cereal::JSONInputArchive archive(file);\n        archive(p2);\n    }\n\n    std::cout << "Name: " << p2.name << ", Age: " << p2.age << std::endl;\n    return 0;\n}\n
\n\n

Compile: g++ -std=c++11 main.cpp -o main. After running, a file person.json will appear with the content {"p1": {"name": "Alice", "age": 30}}.

\n\n

Main Methods and Functions

\n\n

Below are the key elements of the library. All examples assume #include <cereal/cereal.hpp> and the corresponding archives.

\n\n

1. cereal::JSONOutputArchive

\n

What it does: Creates an archive for writing data to

Recommendations