2022-01-18 22:24:31 +01:00
|
|
|
#pragma once
|
|
|
|
// Example for plugging a (de)serialization implementation using the binary
|
|
|
|
// (de)serializer provided by https://github.com/Martchus/reflective-rapidjson.
|
|
|
|
|
2022-01-30 00:05:02 +01:00
|
|
|
#include "./lmdb-safe.hh"
|
2022-01-18 22:24:31 +01:00
|
|
|
|
2022-01-30 22:09:34 +01:00
|
|
|
#include <reflective_rapidjson/binary/reflector.h>
|
2022-01-18 22:24:31 +01:00
|
|
|
|
2022-01-30 21:14:43 +01:00
|
|
|
#include <boost/iostreams/device/back_inserter.hpp>
|
2022-01-18 22:24:31 +01:00
|
|
|
#include <boost/iostreams/stream.hpp>
|
|
|
|
#include <boost/iostreams/stream_buffer.hpp>
|
|
|
|
|
|
|
|
namespace LMDBSafe {
|
|
|
|
|
2022-01-30 21:14:43 +01:00
|
|
|
template <typename T> std::string serToString(const T &t)
|
2022-01-18 22:24:31 +01:00
|
|
|
{
|
2022-01-30 21:14:43 +01:00
|
|
|
auto ret = std::string();
|
|
|
|
auto inserter = boost::iostreams::back_insert_device<std::string>(ret);
|
|
|
|
auto stream = boost::iostreams::stream<boost::iostreams::back_insert_device<std::string>>(inserter);
|
|
|
|
auto deserializer = ReflectiveRapidJSON::BinaryReflector::BinarySerializer(&stream);
|
|
|
|
deserializer.write(t);
|
|
|
|
return ret;
|
2022-01-18 22:24:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 21:14:43 +01:00
|
|
|
template <typename T> void serFromString(string_view str, T &ret)
|
2022-01-18 22:24:31 +01:00
|
|
|
{
|
2022-01-30 21:14:43 +01:00
|
|
|
auto source = boost::iostreams::array_source(str.data(), str.size());
|
|
|
|
auto stream = boost::iostreams::stream<boost::iostreams::array_source>(source);
|
|
|
|
auto serializer = ReflectiveRapidJSON::BinaryReflector::BinaryDeserializer(&stream);
|
|
|
|
ret = T();
|
|
|
|
serializer.read(ret);
|
2022-01-18 22:24:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 21:14:43 +01:00
|
|
|
} // namespace LMDBSafe
|