Serialization of Objects in C++: Overview of Tools and Libraries
Serialization is the process of converting complex data structures (objects, arrays, graphs) into a linear format (byte stream, JSON string, XML) that can be saved to a file, transmitted over a network, or written to a database. The reverse process — deserialization — restores the original state of the object. C++ has no built-in support for reflection (as in Java or Python), so for serialization you either have to write code manually or use ready-made libraries. In this article, we will cover the most popular and effective tools for serialization in C++.
1. Boost.Serialization
The oldest and most powerful library. It is part of Boost and requires separate installation. Supports binary, textual (XML), and its own portable archive formats.
- Advantages: deep support for STL, smart pointers, polymorphism, cyclic references; versioning; cross-platform compatibility.
- Disadvantages: verbose syntax, slow compilation, large executable size.
#include <boost/archive/text_oarchive.hpp>#include <boost/archive/text_iarchive.hpp>#include <boost/serialization/vector.hpp>#include <fstream>#include <vector>
struct Person { std::string name; int age; std::vector<std::string> tags;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & age; ar & tags; }};
int main() { Person p{"Alice", 30, {"developer", "cpp"}}; std::ofstream ofs("person.txt"); boost::archive::text_oarchive oa(ofs); oa << p;
// Deserialization Person restored; std::ifstream ifs("person.txt"); boost::archive::text_iarchive ia(ifs); ia >> restored; return 0;}2. Cereal
A modern, lightweight, header-only library (no build required). Works with C++11 and above. Supports JSON, XML, and binary formats. Very popular in the game industry and embedded development.
- Advantages: minimal overhead, intuitive API, support for smart pointers, polymorphism, STL containers.
- Disadvantages: fewer customization options compared to Boost.
#include <cereal/archives/json.hpp>#include <cereal/types/vector.hpp>#include <cereal/types/string.hpp>#include <sstream>
struct Person { std::string name; int age;
template<class Archive> void serialize(Archive & ar) { ar(CEREAL_NVP(name), CEREAL_NVP(age)); }};
int main() { Person p{"Bob", 25}; std::ostringstream oss; { cereal::JSONOutputArchive archive(oss); archive(CEREAL_NVP(p)); } std::cout << oss.str() << std::endl;
// Deserialization Person restored; std::istringstream iss(oss.str()); { cereal::JSONInputArchive archive(iss); archive(restored); } return 0;}3. nlohmann/json (JSON for Modern C++)
The most popular library for working with JSON in C++. Although not originally designed for object serialization, it can be easily adapted using custom converters (adapter pattern).
- Advantages: simple and elegant syntax, excellent documentation, header-only, integration with any types.
- Disadvantages: works only with JSON, does not support binary formats, requires manual description for complex object graphs.
#include <nlohmann/json.hpp>#include <fstream>
using json = nlohmann::json;
struct Point { double x, y; Also in library
Internationalization (i18n) in JavaScript (Node.js): i18next Library
Node.js Monitoring: Tools and Libraries for Performance Tracking
Doctrine ORM for PHP: Complete Guide with Examples
Generating PDF Reports in Swift for iOS and macOS with PDFKit and Core Graphics