fixup compiler errors & finalize json (de-)serialization for values
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
a934564afc
commit
785e43d9e6
13 changed files with 116 additions and 58 deletions
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <boost/functional/overloaded_function.hpp>
|
||||
#include <boost/type_traits.hpp>
|
||||
|
||||
#include <fmt/format.h>
|
||||
// required for header-only inclusion
|
||||
#include <boost/json/src.hpp>
|
||||
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
|
||||
|
|
@ -17,7 +18,7 @@
|
|||
IkarusValue::IkarusValue(Data data):
|
||||
data(data) {}
|
||||
|
||||
cppbase::Result<IkarusValue, IkarusValue::FromJsonError> IkarusValue::from_json(boost::json::value const& json) {
|
||||
cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_json(boost::json::value const& json) {
|
||||
if (auto const * obj = json.if_object(); obj == nullptr) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
} else {
|
||||
|
|
@ -35,15 +36,23 @@ cppbase::Result<IkarusValue, IkarusValue::FromJsonError> IkarusValue::from_json(
|
|||
}
|
||||
|
||||
auto create_value = [data]<typename T>() -> cppbase::Result<IkarusValue *, FromJsonError> {
|
||||
auto * ret = new T{};
|
||||
auto res = boost::json::try_value_to<boost::container::vector<typename T::data_type>>(*data);
|
||||
T * ret = nullptr;
|
||||
|
||||
if (res.has_error()) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
if (data->is_null()) {
|
||||
ret = new T{};
|
||||
ret->data = boost::variant2::monostate{};
|
||||
} else {
|
||||
auto res = boost::json::try_value_to<
|
||||
boost::container::small_vector<typename T::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(*data);
|
||||
|
||||
if (res.has_error()) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
|
||||
ret = new T{};
|
||||
ret->data = std::move(res.value());
|
||||
}
|
||||
|
||||
ret->data = std::move(res.value());
|
||||
|
||||
return cppbase::ok(ret);
|
||||
};
|
||||
|
||||
|
|
@ -74,7 +83,18 @@ boost::json::value IkarusValue::to_json() const {
|
|||
data
|
||||
);
|
||||
|
||||
auto data_json = boost::variant2::visit([](auto const * value) { return boost::json::value_from(value->data); }, data);
|
||||
auto data_json = boost::variant2::visit(
|
||||
[]<typename T>(T const * value) -> boost::json::value {
|
||||
return boost::variant2::visit(
|
||||
boost::make_overloaded_function(
|
||||
[]([[maybe_unused]] boost::variant2::monostate const& data) -> boost::json::value { return nullptr; },
|
||||
[](auto const& data) -> boost::json::value { return boost::json::value_from(data); }
|
||||
),
|
||||
value->data
|
||||
);
|
||||
},
|
||||
data
|
||||
);
|
||||
|
||||
return {
|
||||
{"type", type},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue