Skip to content

Commit

Permalink
fix: enum serialization
Browse files Browse the repository at this point in the history
fix #57
  • Loading branch information
MistEO committed Apr 28, 2024
1 parent 75c6db7 commit f17ef8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
26 changes: 19 additions & 7 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class basic_value
{
}

template <typename enum_t, std::enable_if_t<std::is_enum_v<enum_t>, bool> = true>
basic_value(enum_t e)
: basic_value(static_cast<std::underlying_type_t<enum_t>>(e))
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
Expand Down Expand Up @@ -335,6 +341,12 @@ class basic_value
return dst;
}

template <typename enum_t, std::enable_if_t<std::is_enum_v<enum_t>, bool> = true>
explicit operator enum_t() const
{
return static_cast<enum_t>(static_cast<std::underlying_type_t<enum_t>>(*this));
}

private:
friend class basic_array<string_t>;
friend class basic_object<string_t>;
Expand Down Expand Up @@ -505,7 +517,7 @@ inline bool basic_value<string_t>::is() const noexcept
else if constexpr (std::is_same_v<bool, value_t>) {
return is_boolean();
}
else if constexpr (std::is_arithmetic_v<value_t>) {
else if constexpr (std::is_arithmetic_v<value_t> || std::is_enum_v<value_t>) {
return is_number();
}
else if constexpr (std::is_constructible_v<string_t, value_t>) {
Expand Down Expand Up @@ -599,16 +611,16 @@ inline auto basic_value<string_t>::get_helper(
{
if constexpr (std::is_constructible_v<string_t, first_key_t>) {
return is_object() ? as_object().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else if constexpr (std::is_integral_v<std::decay_t<first_key_t>>) {
return is_array() ? as_array().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else {
Expand Down
15 changes: 13 additions & 2 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,14 @@ bool jsonizing()
std::unordered_map<std::string, std::list<std::map<std::string, std::deque<int>>>> map;
std::array<int, 5> arr;

MEO_JSONIZATION(str1, str2, str3, vec, map, arr);
enum class W
{
A,
B,
C,
} w = W::A;

MEO_JSONIZATION(str1, str2, str3, vec, map, arr, w);
};

MyStruct mine;
Expand All @@ -276,12 +283,16 @@ bool jsonizing()
mine.vec.emplace_back(0.5);
mine.map = { { "key_1",
{ { { "inner_key_1", { 7, 8, 9 } } }, { { "inner_key_2", { 10 } } } } } };
mine.w = MyStruct::W::C;

json::value j_mine = mine;
std::cout << j_mine<< std::endl;

MyStruct new_mine = (MyStruct)j_mine;

bool ret = new_mine.str1 == "Hello" && new_mine.str2 == "World" && new_mine.str3 == "!"
&& new_mine.vec[0] == 0.5 && new_mine.map["key_1"].size() == 2;
&& new_mine.vec[0] == 0.5 && new_mine.map["key_1"].size() == 2
&& new_mine.w == MyStruct::W::C;
if (!ret) {
std::cerr << "error new_mine" << std::endl;
return false;
Expand Down

1 comment on commit f17ef8e

@MistEO
Copy link
Owner Author

@MistEO MistEO commented on f17ef8e Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit 写错了,应该是 feat

Please sign in to comment.