Binary Serialization using Cereal

Serializing Private Class Members with Cereal

I want to serialize private member variables of my Player class using cereal. How can I accomplish this?

Abstract art representing computer programming

To serialize private member variables of your Player class using cereal, you can follow these steps:

Step 1: Include the necessary cereal headers:

#include <cereal/access.hpp>
#include <cereal/types/string.hpp>

Step 2: In your Player class, make the serialization functions private and befriend cereal::access:

class Player {
private:
  std::string name;
  int level;
  int health;

  friend class cereal::access;

  template<class Archive>
  void serialize(Archive& ar) {
    ar(name, level, health);
  }
};

By making the serialize function private and friending cereal::access, you grant cereal access to your class's private members for serialization purposes.

Step 3: Serialize and deserialize the Player object as usual:

#include <cereal/archives/binary.hpp>
#include <fstream>

int main() {
  // Serialization
  Player player("Alice", 10, 100);
  std::ofstream outputFile("player.dat");
  cereal::BinaryOutputArchive outputArchive(
    outputFile);
  outputArchive(player);

  // Deserialization
  Player deserializedPlayer;
  std::ifstream inputFile("player.dat");
  cereal::BinaryInputArchive inputArchive(
    inputFile);
  inputArchive(deserializedPlayer);
}

By following this approach, cereal will be able to access and serialize the private member variables of your Player class without the need to make them public or use getter/setter functions.

Note: Be cautious when serializing private members, as it can potentially break encapsulation. Only serialize data that is necessary for saving and loading the object's state.

A complete example is below:

#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
#include <fstream>
#include <iostream>

class Player {
 public:
  Player() = default;
  Player(const std::string& name, int level,
    int health) : name(name), level(level),
    health(health) {}

  std::string GetName() { return name; }
  int GetLevel() { return level; }
  int GetHealth() { return health; }

 private:
  std::string name;
  int level;
  int health;

  friend class cereal::access;

  template <class Archive>
  void serialize(Archive& ar) {
    ar(name, level, health);
  }
};

int main() {
  // Serialization
  Player player("Alice", 10, 100);
  {
    std::ofstream outputFile("player.dat");
    cereal::BinaryOutputArchive outputArchive(
      outputFile);
    outputArchive(player);
  }

  // Deserialization
  Player deserializedPlayer;
  {
    std::ifstream inputFile("player.dat");
    cereal::BinaryInputArchive inputArchive(
      inputFile);
    inputArchive(deserializedPlayer);
  }

  std::cout << "Deserialized Player:\n";
  std::cout << "Name: "
    << deserializedPlayer.GetName() << "\n";
  std::cout << "Level: "
    << deserializedPlayer.GetLevel() << "\n";
  std::cout << "Health: "
    << deserializedPlayer.GetHealth() << "\n";
}
Deserialized Player:
Name: Alice
Level: 10
Health: 100

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved