6 #ifndef UTILS_SERIALIZER_H 7 #define UTILS_SERIALIZER_H 10 #include <cereal/archives/json.hpp> 11 #include <cereal/types/vector.hpp> 13 #include <Utils/ExtSTD.h> 31 template <
typename Type,
typename SerializableType>
37 explicit Serializer(
const std::string &root_name)
38 : root_name_(root_name)
48 return FromString<cereal::JSONInputArchive>(json_string);
62 return ToString<cereal::JSONOutputArchive>(objects);
69 return FromFile<cereal::JSONInputArchive>(file_path);
81 bool ToJsonFile(
const std::vector<Type> &objects,
const std::string &file_path)
const 83 return ToFile<cereal::JSONOutputArchive>(objects, file_path);
90 template <
typename ArchiveType>
92 std::vector<Type> FromString(
const std::string &json_string)
const 98 std::vector<SerializableType> serializables;
99 ArchiveType archive(ss);
100 archive >> serializables;
103 catch (
const std::exception &e)
105 std::cout << e.what();
106 return std::vector<Type>();
111 template <
typename ArchiveType>
113 std::string ToString(
const std::vector<Type> &objects)
const 115 std::stringstream ss;
117 ArchiveType archive(ss);
118 const std::vector<SerializableType> serializables =
std_ext::Transformed(objects, [](
const auto & s) {
return SerializableType(s); });
119 archive << ::cereal::make_nvp(root_name_, serializables);
126 template <
typename ArchiveType>
128 std::vector<Type> FromFile(
const std::string &file_path)
const 132 std::ifstream is(file_path.c_str());
136 ArchiveType archive(is);
137 std::vector<SerializableType> serializables;
138 archive >> serializables;
143 std::cout <<
"Could not open file: " << file_path <<
'\n';
144 return std::vector<Type>();
147 catch (
const std::exception &e)
149 std::cout << e.what();
150 return std::vector<Type>();
155 template <
typename ArchiveType>
157 bool ToFile(
const std::vector<Type> &objects,
const std::string &file_path)
const 161 std::ofstream os(file_path);
164 ArchiveType archive(os);
165 const std::vector<SerializableType> serializables =
std_ext::Transformed(objects, [](
const auto & s) {
return SerializableType(s); });
166 archive << ::cereal::make_nvp(root_name_, serializables);
171 std::cout <<
"Could not open file: " << file_path <<
'\n';
175 catch (
const std::exception &e)
177 std::cout << e.what();
186 const std::string root_name_;
bool ToJsonFile(const std::vector< Type > &objects, const std::string &file_path) const
Serialize to JSON string and save to file.
Definition: Serializer.h:81
std::vector< Type > FromJsonString(const std::string &json_string) const
Read JSON string.
Definition: Serializer.h:46
std::vector< std::shared_ptr< Type > > FromJsonFileAsPtr(const std::string &file_path) const
Read from JSON file by path, return as shared pointers.
Definition: Serializer.h:74
std::vector< Type > FromJsonFile(const std::string &file_path) const
Read from JSON file by path.
Definition: Serializer.h:67
auto Transformed(const C &container, F Function)
Simplified wrapper for std::transform.
Definition: ExtSTD.h:831
Serialization utility class. Can perform automatic read and write.
Definition: Serializer.h:32
General namespace for utilities.
Definition: BulletConversions.h:9
std::string ToJsonString(const std::vector< Type > &objects) const
Serialize to JSON string.
Definition: Serializer.h:60
std::vector< std::shared_ptr< Type > > FromJsonStringAsPtr(const std::string &json_string) const
Read JSON string, return as shared pointers.
Definition: Serializer.h:53