Object Serialization in C++: Libraries, Methods, and Code 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

Object Serialization in C++: A Complete Guide



Serialization is the process of converting complex data structures (class objects, containers) into a sequential format (byte stream, JSON string, XML) suitable for saving to a file or transmitting over a network. The reverse process — deserialization — restores the object from this format. In C++, which lacks built-in reflection, serialization requires manual description or the use of specialized libraries. In this article, we will cover the main approaches and best tools for serialization in C++.



Why is Serialization Needed?

  • State saving — saving game progress, application settings.
  • Network communication — transferring objects between client and server.
  • Caching — saving computation results to a file.
  • Data exchange — integration with other languages (JSON, Protobuf).


Popular Libraries for Serialization in C++



1. Boost.Serialization

A classic library from the Boost collection. Supports binary, text, and XML serialization. Requires explicit addition of serialize or save/load methods.



#include <boost/archive/text_oarchive.hpp>#include <boost/archive/text_iarchive.hpp>#include <fstream>

class Person {public: std::string name; int age; template<class Archive> void serialize(Archive& ar, const unsigned int version) { ar & name; ar & age; }};

// Savingvoid save(const Person& p) { std::ofstream ofs("person.txt"); boost::archive::text_oarchive oa(ofs); oa << p;}

// LoadingPerson load() { Person p; std::ifstream ifs("person.txt"); boost::archive::text_iarchive ia(ifs); ia >> p; return p;}


2. Cereal

A modern, lightweight library (header-only). Supports JSON, XML, and binary formats. Does not require inheritance from base classes. Great for projects where the entire Boost library is not needed.



#include <cereal/archives/json.hpp>#include <cereal/types/string.hpp>#include <cereal/types/vector.hpp>#include <fstream>

struct Student { std::string name; std::vector<int> grades; template<class Archive> void serialize(Archive& ar) { ar(CEREAL_NVP(name), CEREAL_NVP(grades)); }};

// Saving to JSONvoid saveStudent(const Student& s) { std::ofstream ofs("student.json"); cereal::JSONOutputArchive archive(ofs); archive(CEREAL_NVP(s));}

// Loading from JSONStudent loadStudent() { Student s; std::ifstream ifs("student.json"); cereal::JSONInputArchive archive(ifs); archive(s); return s;}


3. nlohmann/json (JSON for Modern C++)

Not a serialization library in the classical sense, but it allows easy conversion of objects to and from JSON. Requires manual writing of conversion functions.



#include <nlohmann/json.hpp>using json = nlohmann::json;

struct Point { int x, y;};

void to_json(json& j, const Point& p) { j = json{{"x", p.x}, {"y", p.y}};}

void from_json(const json& j, Point& p) { j.at("x").get_to(p.x); j.at("y").get_to(p.y);}

// UsagePoint p1{10, 20};json j = p1; // serializationPoint p2 = j.get<Point>(); // deserialization


4. Protocol Buffers (protobuf)

Developed by Google. Requires describing structures in .proto files, after which C++ code is generated. Promises a compact binary format and high speed.



// person.proto filesyntax = "proto3";message P                    

Recommendations