1
0
mirror of synced 2025-02-09 07:09:42 +01:00
jujube/test/cereal_unordered_map.cpp
2020-02-19 00:51:26 +01:00

60 lines
1.2 KiB
C++

#include <fstream>
#include <string>
#include <unordered_map>
#include <cereal/archives/json.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/unordered_map.hpp>
enum class Keys {
blue,
green,
yellow,
orange,
red,
};
enum Values {
YES,
NO,
MAYBE
};
struct MyThingWithAnUnorderedMap {
std::unordered_map<Keys, Values> m_map;
};
struct Indirect {
MyThingWithAnUnorderedMap m;
template<class Archive>
void serialize(Archive & archive) {
archive(
cereal::make_nvp("map", m.m_map)
);
}
};
int main(int argc, char const *argv[]) {
Indirect ref;
ref.m.m_map = {{Keys::blue, YES}, {Keys::green, MAYBE}};
{
std::ofstream file;
file.open("cereal_test_unordered_map.json", std::ofstream::trunc | std::ofstream::out);
cereal::JSONOutputArchive archive{file};
archive(ref);
}
Indirect test;
{
std::ifstream file;
file.open("cereal_test_unordered_map.json", std::ifstream::in);
cereal::JSONInputArchive archive{file};
archive(test);
}
if (ref.m.m_map != test.m.m_map) {
return -1;
}
return 0;
}