Skip to content

Commit

Permalink
feat: improve SerializationError
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Nov 21, 2023
1 parent e5acb3d commit ae5ac04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/ll/api/reflection/Serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ inline J serialize(T const& obj)
forEachMember(obj, [&](std::string_view name, auto& member) {
using MemberType = std::remove_cvref_t<decltype(member)>;
if constexpr (requires(MemberType& m) { serialize<J>(m); }) {
auto j = serialize<J>(member);
if (!j.is_null()) res[std::string{name}] = j;
try {
auto j = serialize<J>(member);
if (!j.is_null()) res[std::string{name}] = j;
} catch (...) {
std::throw_with_nested(SerializationError("error in serialize field: " + std::string{name}));
}
} else {
static_assert(ll::concepts::always_false<MemberType>, "this type can't serialize");
}
Expand All @@ -84,7 +88,9 @@ inline void deserialize(T& obj, J const& j) {
auto sname = std::string{name};
if (j.contains(sname)) {
if constexpr (requires(MemberType& o, J const& s) { deserialize<J>(o, s); }) {
deserialize<J>(member, j[sname]);
try {
deserialize<J>(member, j[sname]);
} catch (...) { std::throw_with_nested(SerializationError("error in deserialize field: " + sname)); }
} else {
static_assert(ll::concepts::always_false<MemberType>, "this type can't deserialize");
}
Expand Down
13 changes: 11 additions & 2 deletions src/ll/test/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "ll/api/base/Version.h"

#include "ll/api/base/ErrorInfo.h"

// #include "ll/api/schedule/Scheduler.h"
// #include "ll/api/service/GlobalService.h"
// #include "mc/world/level/BlockPos.h"
Expand Down Expand Up @@ -67,12 +69,19 @@ LL_AUTO_TYPED_INSTANCE_HOOK(
) {
origin();

// auto helloReflection = TestClass<int>{};
auto helloReflection = TestClass<int>{};

// ll::config::saveConfig(helloReflection, "plugins/Test/testconfig.json");
// ll::config::saveConfig(helloReflection, "plugins/Test/config/testconfig.json");

std::lock_guard lock(ll::Logger::loggerMutex); // test logger order

try {
ll::reflection::deserialize(
helloReflection,
nlohmann::ordered_json::parse(R"({"version":""})", nullptr, false, true)
);
} catch (...) { ll::error_info::printCurrentException(); }

// ll::logger.debug("{} for load config", ll::config::loadConfig(helloReflection, "plugins/Test/testconfig.json"));
// ll::logger.debug("\n{}", ll::reflection::serialize<nlohmann::ordered_json>(helloReflection).dump(4));

Expand Down

0 comments on commit ae5ac04

Please sign in to comment.