9 #include <nop/serializer.h>
10 #include <nop/structure.h>
11 #include <nop/utility/buffer_reader.h>
12 #include <nop/utility/stream_writer.h>
18 #ifndef __has_feature // Optional of course.
19 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
21 #if __has_feature(cxx_exceptions) || defined(__cpp_exceptions) || (defined(_MSC_VER) && defined(_CPPUNWIND)) || defined(__EXCEPTIONS)
22 #define DEPTHAI_EXCEPTIONS
37 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::JSON_MSGPACK,
bool> = true>
39 nlohmann::json j = obj;
40 data = nlohmann::json::to_msgpack(j);
43 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::JSON_MSGPACK,
bool> = true>
45 nlohmann::from_json(nlohmann::json::from_msgpack(
data,
data +
size), obj);
50 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::JSON,
bool> = true>
51 inline bool serialize(
const T& obj, std::vector<std::uint8_t>&
data) {
52 nlohmann::json j = obj;
54 data = std::vector<std::uint8_t>(
reinterpret_cast<const std::uint8_t*
>(json.data()),
reinterpret_cast<const std::uint8_t*
>(json.data()) + json.size());
57 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::JSON,
bool> = true>
59 nlohmann::from_json(nlohmann::json::parse(
data,
data +
size), obj);
66 template <
typename... Args>
75 nop::Status<void>
Write(std::uint8_t
byte) {
80 nop::Status<void>
Write(
const void* begin,
const void* end) {
81 vector.insert(
vector.end(),
static_cast<const std::uint8_t*
>(begin),
static_cast<const std::uint8_t*
>(end));
85 nop::Status<void>
Skip(std::size_t padding_bytes, std::uint8_t padding_value = 0x00) {
86 for(std::size_t i = 0; i < padding_bytes; i++) {
87 vector.push_back(padding_value);
95 const std::vector<std::uint8_t>&
ref()
const {
98 std::vector<std::uint8_t>&
ref() {
101 std::vector<std::uint8_t>&&
take() {
117 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::LIBNOP,
bool> = true>
118 inline bool serialize(
const T& obj, std::vector<std::uint8_t>&
data) {
119 nop::Serializer<VectorWriter> serializer{std::move(
data)};
120 auto status = serializer.Write(obj);
122 #ifdef DEPTHAI_EXCEPTIONS
123 throw std::runtime_error(
status.GetErrorMessage());
128 data = std::move(serializer.writer().take());
131 template <SerializationType TYPE,
typename T, std::enable_if_t<TYPE == SerializationType::LIBNOP,
bool> = true>
133 nop::Deserializer<nop::BufferReader> deserializer{
data,
size};
134 auto status = deserializer.Read(&obj);
136 #ifdef DEPTHAI_EXCEPTIONS
137 throw std::runtime_error(
status.GetErrorMessage());
146 template <
typename T>
150 return serialize<SerializationType::LIBNOP>(obj,
data);
152 return serialize<SerializationType::JSON>(obj,
data);
154 return serialize<SerializationType::JSON_MSGPACK>(obj,
data);
156 throw std::invalid_argument(
"Unknown serialization type");
159 template <
typename T>
161 std::vector<std::uint8_t>
data;
169 template <
typename T>
173 return deserialize<SerializationType::LIBNOP>(
data,
size, obj);
175 return deserialize<SerializationType::JSON>(
data,
size, obj);
177 return deserialize<SerializationType::JSON_MSGPACK>(
data,
size, obj);
179 throw std::invalid_argument(
"Unknown serialization type");
182 template <
typename T>
188 template <SerializationType TYPE,
typename T>
189 inline std::vector<std::uint8_t>
serialize(
const T& obj) {
190 std::vector<std::uint8_t>
data;
191 if(serialize<TYPE>(obj,
data)) {
197 template <SerializationType TYPE,
typename T>
199 return deserialize<TYPE>(
data.data(),
data.size(), obj);
203 template <
typename T>
205 return serialize<DEFAULT_SERIALIZATION_TYPE>(obj,
data);
207 template <
typename T>
208 inline std::vector<std::uint8_t>
serialize(
const T& obj) {
209 return serialize<DEFAULT_SERIALIZATION_TYPE>(obj);
211 template <
typename T>
213 return deserialize<DEFAULT_SERIALIZATION_TYPE>(
data,
size, obj);
215 template <
typename T>
217 return deserialize<DEFAULT_SERIALIZATION_TYPE>(
data, obj);
232 #define DEPTHAI_DEFERRED_EXPAND(x) x
233 #if defined(_MSC_VER) && (!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL)
237 #pragma warning(disable : 4003)
240 #define DEPTHAI_NLOHMANN_JSON_OPTIONAL_TO(v1) nlohmann::to_json(nlohmann_json_j[#v1], nlohmann_json_t.v1);
241 #define DEPTHAI_NLOHMANN_JSON_OPTIONAL_FROM(v1) \
242 if(nlohmann_json_j.contains(#v1)) nlohmann_json_j[#v1].get_to(nlohmann_json_t.v1);
243 #define DEPTHAI_NLOHMANN_DEFINE_TYPE_OPTIONAL_NON_INTRUSIVE(Type, ...) \
244 inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { \
245 DEPTHAI_NLOHMANN_JSON_EXPAND(DEPTHAI_NLOHMANN_JSON_PASTE(DEPTHAI_NLOHMANN_JSON_OPTIONAL_TO, __VA_ARGS__)) \
247 inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { \
248 DEPTHAI_NLOHMANN_JSON_EXPAND(DEPTHAI_NLOHMANN_JSON_PASTE(DEPTHAI_NLOHMANN_JSON_OPTIONAL_FROM, __VA_ARGS__)) \
250 #define DEPTHAI_NLOHMANN_DEFINE_TYPE_OPTIONAL_INTRUSIVE(Type, ...) \
251 friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { \
252 DEPTHAI_NLOHMANN_JSON_EXPAND(DEPTHAI_NLOHMANN_JSON_PASTE(DEPTHAI_NLOHMANN_JSON_OPTIONAL_TO, __VA_ARGS__)) \
254 friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { \
255 DEPTHAI_NLOHMANN_JSON_EXPAND(DEPTHAI_NLOHMANN_JSON_PASTE(DEPTHAI_NLOHMANN_JSON_OPTIONAL_FROM, __VA_ARGS__)) \
259 #define DEPTHAI_SERIALIZE_OPTIONAL_EXT(...) \
260 DEPTHAI_DEFERRED_EXPAND(DEPTHAI_NLOHMANN_DEFINE_TYPE_OPTIONAL_NON_INTRUSIVE(__VA_ARGS__)) \
261 DEPTHAI_DEFERRED_EXPAND(NOP_EXTERNAL_STRUCTURE(__VA_ARGS__))
263 #define DEPTHAI_SERIALIZE_OPTIONAL(...) \
264 DEPTHAI_DEFERRED_EXPAND(DEPTHAI_NLOHMANN_DEFINE_TYPE_OPTIONAL_INTRUSIVE(__VA_ARGS__)) \
265 DEPTHAI_DEFERRED_EXPAND(NOP_EXTERNAL_STRUCTURE(__VA_ARGS__))
267 #define DEPTHAI_SERIALIZE_EXT(...) \
268 DEPTHAI_DEFERRED_EXPAND(DEPTHAI_NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(__VA_ARGS__)) \
269 DEPTHAI_DEFERRED_EXPAND(NOP_EXTERNAL_STRUCTURE(__VA_ARGS__))
271 #define DEPTHAI_SERIALIZE(...) \
272 DEPTHAI_DEFERRED_EXPAND(DEPTHAI_NLOHMANN_DEFINE_TYPE_INTRUSIVE(__VA_ARGS__)) \
273 DEPTHAI_DEFERRED_EXPAND(NOP_STRUCTURE(__VA_ARGS__))