Skip to content

Commit

Permalink
feat(serialization): handle NullableTrait in JSON Deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacops committed Jul 10, 2024
1 parent 77c4182 commit a84f27d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/data/des/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cubos/core/reflection/traits/dictionary.hpp>
#include <cubos/core/reflection/traits/enum.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/nullable.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/type.hpp>

Expand All @@ -19,6 +20,7 @@ using cubos::core::reflection::ConstructibleTrait;
using cubos::core::reflection::DictionaryTrait;
using cubos::core::reflection::EnumTrait;
using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::NullableTrait;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;

Expand Down Expand Up @@ -118,6 +120,13 @@ bool JSONDeserializer::decompose(const Type& type, void* value)
return true;
}

if (type.has<NullableTrait>() && mIterator->is_null())
{
const auto& trait = type.get<NullableTrait>();
trait.setToNull(value);
return true;
}

if (mIterator->is_string() && type.has<StringConversionTrait>())
{
const auto& trait = type.get<StringConversionTrait>();
Expand Down Expand Up @@ -247,7 +256,7 @@ bool JSONDeserializer::decompose(const Type& type, void* value)

if (!mIterator->is_object())
{
CUBOS_CRITICAL("Expected an object {}, found a {}", type.name(), mIterator->type_name());
CUBOS_WARN("Expected an object of type {}, found a {}", type.name(), mIterator->type_name());
return false;
}

Expand Down
29 changes: 29 additions & 0 deletions core/tests/data/des/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cubos/core/reflection/traits/constructible_utils.hpp>
#include <cubos/core/reflection/traits/enum.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/nullable.hpp>

#include "../utils.hpp"

Expand All @@ -20,6 +21,8 @@ using cubos::core::data::JSONDeserializer;
using cubos::core::reflection::autoConstructibleTrait;
using cubos::core::reflection::EnumTrait;
using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::NullableTrait;
using cubos::core::reflection::reflect;
using cubos::core::reflection::Type;

namespace
Expand Down Expand Up @@ -59,6 +62,15 @@ namespace
Green,
Blue
};

struct Nullable
{
CUBOS_REFLECT;

bool operator==(const Nullable& /*unused*/) const = default;

uint32_t value;
};
} // namespace

CUBOS_REFLECT_IMPL(NonConstructible)
Expand All @@ -83,6 +95,15 @@ CUBOS_REFLECT_EXTERNAL_IMPL(Color)
EnumTrait{}.withVariant<Color::Red>("Red").withVariant<Color::Green>("Green").withVariant<Color::Blue>("Blue"));
}

CUBOS_REFLECT_IMPL(Nullable)
{
return Type::create("Nullable")
.with(FieldsTrait{}.withField("value", &Nullable::value))
.with(NullableTrait{
[](const void* instance) -> bool { return static_cast<const Nullable*>(instance)->value == UINT32_MAX; },
[](void* instance) { static_cast<Nullable*>(instance)->value = UINT32_MAX; }});
}

#define AUTO_EXISTING(json, initial, expected) \
{ \
des.feed(json); \
Expand Down Expand Up @@ -169,5 +190,13 @@ TEST_CASE("data::JSONDeserializer")
AUTO_EXISTING((Json::array({1, 2, 3})), (std::vector<int>{4, 3, 2, 1}), (std::vector<int>{1, 2, 3}));
AUTO_SUCCESS((Json::array({1, 2, 3})), (std::vector<int>{1, 2, 3}));
AUTO_FAILURE((Json::array({1, 2, "3"})), std::vector<int>);

const auto& nullable = reflect<Nullable>();
const auto& nullableTrait = nullable.get<NullableTrait>();
Nullable null{144};
AUTO_SUCCESS(144, null);

nullableTrait.setToNull(&null);
AUTO_SUCCESS(nlohmann::json::value_t::null, null);
}
// NOLINTEND(readability-function-size)

0 comments on commit a84f27d

Please sign in to comment.