Skip to content

Instantly share code, notes, and snippets.

@OXPHOS
Created May 15, 2016 04:43
Show Gist options
  • Save OXPHOS/3650d75f57c106d0d4d60ba2841a6a51 to your computer and use it in GitHub Desktop.
Save OXPHOS/3650d75f57c106d0d4d60ba2841a6a51 to your computer and use it in GitHub Desktop.
Serialization
#include <cereal/types/unordered_map.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/json.hpp>
#include <fstream>
struct MyRecord
{
uint8_t x = 10, y = 100;
float z = 3.2;
template <class Archive>
void serialize( Archive & ar )
{
ar( x, y, z );
}
};
struct SomeData
{
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
SomeData()
{
id = 5;
MyRecord record;
data = std::make_shared<std::unordered_map<uint32_t, MyRecord>>();
data->emplace(id, record);
}
template <class Archive>
void save( Archive & ar ) const
{
ar( data );
}
template <class Archive>
void load( Archive & ar )
{
static int32_t idGen = 0;
id = idGen++;
ar( data );
}
};
int main()
{
std::ofstream os("out.cereal", std::ios::binary);
cereal::JSONOutputArchive archive( os );
SomeData myData;
//MyRecord myData;
archive( myData );
return 0;
}
{
"value0": {
"value0": {
"ptr_wrapper": {
"id": 2147483649,
"data": [
{
"key": 5,
"value": {
"value0": 10,
"value1": 100,
"value2": 3.2000000476837158
}
}
]
}
}
}
}
@karlnapf
Copy link

The main thing here is that we actually do this with a Shogun class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment