ECSTASY
All in the name
Loading...
Searching...
No Matches
Serializer.hpp
Go to the documentation of this file.
1
11
12#ifndef UTIL_SERIALIZATION_SERIALIZER_HPP_
13#define UTIL_SERIALIZATION_SERIALIZER_HPP_
14
15#include <iostream>
16#include <sstream>
17
18namespace util::serialization
19{
27 class Serializer {
28 public:
42 template <typename Object>
43 static inline std::ostream &serialize(std::ostream &stream, const Object &object)
44 {
45 return stream << object;
46 }
47
60 template <typename Object>
61 static inline std::string serialize(const Object &object)
62 {
64
65 serialize(ss, object);
66 return ss.str();
67 }
68
81 template <typename Object>
82 static inline Object deserialize(std::istream &stream)
83 {
84 Object oblect;
85
86 stream >> oblect;
87 return oblect;
88 }
89
102 template <typename Object>
103 static inline Object deserialize(std::string_view bytes)
104 {
105 std::istringstream ss(std::string(bytes), std::ios_base::in);
106
107 return deserialize<Object>(ss);
108 }
109
110 private:
111 Serializer() = default;
112 };
113} // namespace util::serialization
114
115#endif /* !UTIL_SERIALIZATION_SERIALIZER_HPP_ */
static std::string serialize(const Object &object)
Serialize an object to a std::string.
static std::ostream & serialize(std::ostream &stream, const Object &object)
Serialize an object in a stream.
static Object deserialize(std::istream &stream)
Deserialize an object from an input stream.
static Object deserialize(std::string_view bytes)
Deserialize an object from its bytes representation.
T str(T... args)