Utilities
Utilities header only library
Serializer.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_SERIALIZER_H
7 #define UTILS_SERIALIZER_H
8 
9 #include <fstream>
10 #include <cereal/archives/json.hpp>
11 #include <cereal/types/vector.hpp>
12 
13 #include <Utils/ExtSTD.h>
14 
15 
16 
17 namespace utils
18 {
19 
31  template <typename Type, typename SerializableType>
32  class Serializer
33  {
34 
35  public:
36 
37  explicit Serializer(const std::string &root_name)
38  : root_name_(root_name)
39  {
40  }
41 
42 
43 
45  [[nodiscard]]
46  std::vector<Type> FromJsonString(const std::string &json_string) const
47  {
48  return FromString<cereal::JSONInputArchive>(json_string);
49  }
50 
52  [[nodiscard]]
53  std::vector<std::shared_ptr<Type>> FromJsonStringAsPtr(const std::string& json_string) const
54  {
55  return std_ext::Transformed(FromJsonString(json_string), [](const auto& val){ return std::make_shared<Type>(val); });
56  }
57 
59  [[nodiscard]]
60  std::string ToJsonString(const std::vector<Type> &objects) const
61  {
62  return ToString<cereal::JSONOutputArchive>(objects);
63  }
64 
66  [[nodiscard]]
67  std::vector<Type> FromJsonFile(const std::string &file_path) const
68  {
69  return FromFile<cereal::JSONInputArchive>(file_path);
70  }
71 
73  [[nodiscard]]
74  std::vector<std::shared_ptr<Type>> FromJsonFileAsPtr(const std::string& file_path) const
75  {
76  return std_ext::Transformed(FromJsonFile(file_path), [](const auto& val) { return std::make_shared<Type>(val); });
77  }
78 
80  [[nodiscard]]
81  bool ToJsonFile(const std::vector<Type> &objects, const std::string &file_path) const
82  {
83  return ToFile<cereal::JSONOutputArchive>(objects, file_path);
84  }
85 
86 
87 
88  private:
90  template <typename ArchiveType>
91  [[nodiscard]]
92  std::vector<Type> FromString(const std::string &json_string) const
93  {
94  try
95  {
96  std::stringstream ss;
97  ss << json_string;
98  std::vector<SerializableType> serializables;
99  ArchiveType archive(ss);
100  archive >> serializables;
101  return std_ext::Transformed(serializables, [](const auto & s) { return s.To(); });
102  }
103  catch (const std::exception &e)
104  {
105  std::cout << e.what();
106  return std::vector<Type>();
107  }
108  }
109 
111  template <typename ArchiveType>
112  [[nodiscard]]
113  std::string ToString(const std::vector<Type> &objects) const
114  {
115  std::stringstream ss;
116  {
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);
120  }
121 
122  return ss.str();
123  }
124 
126  template <typename ArchiveType>
127  [[nodiscard]]
128  std::vector<Type> FromFile(const std::string &file_path) const
129  {
130  try
131  {
132  std::ifstream is(file_path.c_str());
133 
134  if (is.is_open())
135  {
136  ArchiveType archive(is);
137  std::vector<SerializableType> serializables;
138  archive >> serializables;
139  return std_ext::Transformed(serializables, [](const auto & s) { return s.To(); });
140  }
141  else
142  {
143  std::cout << "Could not open file: " << file_path << '\n';
144  return std::vector<Type>();
145  }
146  }
147  catch (const std::exception &e)
148  {
149  std::cout << e.what();
150  return std::vector<Type>();
151  }
152  }
153 
155  template <typename ArchiveType>
156  [[nodiscard]]
157  bool ToFile(const std::vector<Type> &objects, const std::string &file_path) const
158  {
159  try
160  {
161  std::ofstream os(file_path);
162  if (os.is_open())
163  {
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);
167  return true;
168  }
169  else
170  {
171  std::cout << "Could not open file: " << file_path << '\n';
172  return false;
173  }
174  }
175  catch (const std::exception &e)
176  {
177  std::cout << e.what();
178  return false;
179  }
180 
181  return false;
182  }
183 
184 
186  const std::string root_name_;
187  };
188 
189 }
190 
191 #endif /* !UTILS_SERIALIZER_H */
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