0

I'm using the following code to attempt to serialize/deserialize an object as binary data:

MyDTO dto1;    
std::ostringstream os(std::stringstream::binary);
{
    cereal::BinaryOutputArchive oarchive(os); // Create an output archive
    oarchive(dto1);
}

MyDTO dto2;

std::istringstream is(os.str(), std::stringstream::binary);
{
    cereal::BinaryInputArchive iarchive(is); // Create an input archive
    try {
        iarchive(dto2);
    }
    catch (std::runtime_error e) {
        e.what();
    }
}

When the code runs, an exception is caught with the message:

"Failed to read 8 bytes from input stream! Read 0"

Can anyone help me understand what's going wrong?

Brian Templeton
  • 106
  • 2
  • 10

1 Answers1

1

Your input archive iarchive has no data to read from since is is empty. You should first write to the stringstream using output archive and use the same stringstream for iarchive to read from (I guess that is what you want to do)

You should try something like below (I did not test it):

MyDTO dto1;    
std::stringstream os(std::stringstream::binary);
{
    cereal::BinaryOutputArchive oarchive(os); // Create an output archive
    oarchive(dto1);
}

MyDTO dto2;

{
    cereal::BinaryInputArchive iarchive(os); // Create an output archive
    try {
        iarchive(dto2);
    }
    catch (std::runtime_error e) {
        e.what();
    }
}
Arunmu
  • 6,837
  • 1
  • 24
  • 46