mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 22:36:19 +08:00
49 lines
1.4 KiB
C++
49 lines
1.4 KiB
C++
#ifndef RFL_UBJSON_READ_HPP_
|
|
#define RFL_UBJSON_READ_HPP_
|
|
|
|
#include <bit>
|
|
#include <istream>
|
|
#include <jsoncons/json.hpp>
|
|
#include <jsoncons_ext/ubjson/decode_ubjson.hpp>
|
|
#include <string>
|
|
|
|
#include "../Processors.hpp"
|
|
#include "../concepts.hpp"
|
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
|
#include "Parser.hpp"
|
|
#include "Reader.hpp"
|
|
|
|
namespace rfl::ubjson {
|
|
|
|
using InputObjectType = typename Reader::InputObjectType;
|
|
using InputVarType = typename Reader::InputVarType;
|
|
|
|
/// Parses an object from UBJSON using reflection.
|
|
template <class T, class... Ps>
|
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
|
const concepts::ContiguousByteContainer auto& _bytes) {
|
|
auto result = jsoncons::ubjson::try_decode_ubjson<jsoncons::json>(_bytes);
|
|
if (result.has_value()) {
|
|
auto r = Reader();
|
|
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&result.value()});
|
|
} else {
|
|
return rfl::error("Could not parse UBJSON: " + result.error().message());
|
|
}
|
|
}
|
|
|
|
/// Parses an object from a stream.
|
|
template <class T, class... Ps>
|
|
Result<internal::wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
|
auto result = jsoncons::ubjson::try_decode_ubjson<jsoncons::json>(_stream);
|
|
if (result.has_value()) {
|
|
auto r = Reader();
|
|
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&result.value()});
|
|
} else {
|
|
return rfl::error("Could not parse UBJSON: " + result.error().message());
|
|
}
|
|
}
|
|
|
|
} // namespace rfl::ubjson
|
|
|
|
#endif
|