From 6d427acdde352db6b48630fddd6d5e3d4bd542dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Sun, 8 Jan 2017 14:07:10 +0100 Subject: [PATCH] replace constructor by from/to_json: unscoped enum types this also means that one can do: j.get(); --- src/json.hpp | 26 ++++++++++++++++---------- src/json.hpp.re2c | 26 ++++++++++++++++---------- test/src/unit-regression.cpp | 10 +++++++++- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 9f8f3145..74e6338f 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -432,7 +432,6 @@ template struct is_compatible_basic_json_type { static auto constexpr value = - is_unscoped_enum::value or std::is_same::value or is_compatible_array_type::value or is_compatible_object_type::value; @@ -569,6 +568,13 @@ void to_json(Json &j, CompatibleNumberIntegerType val) noexcept external_constructor::construct(j, val); } +template ::value, int> = 0> +void to_json(Json &j, UnscopedEnumType e) +{ + external_constructor::construct(j, e); +} + template void from_json(Json const& j, typename Json::boolean_t& b) { @@ -603,6 +609,15 @@ void from_json(Json const& j, typename Json::number_integer_t& val) get_arithmetic_value(j, val); } +template ::value, int> = 0> +void from_json(Json const &j, UnscopedEnumType& e) +{ + typename std::underlying_type::type val = e; + get_arithmetic_value(j, val); + e = static_cast(val); +} + // overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..) // // note: Is it really necessary to provide explicit overloads for boolean_t etc.. @@ -1848,15 +1863,6 @@ class basic_json JSONSerializer>::to_json(*this, std::forward(val)); } - // Constructor for unscoped enums (not enum classes) - template ::value, int> = 0> - basic_json(T val) noexcept - : m_type(value_t::number_integer), - m_value(static_cast(val)) - { - assert_invariant(); - } - /*! @brief create a container (array or object) from an initializer list diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 909ce90d..c4182652 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -432,7 +432,6 @@ template struct is_compatible_basic_json_type { static auto constexpr value = - is_unscoped_enum::value or std::is_same::value or is_compatible_array_type::value or is_compatible_object_type::value; @@ -569,6 +568,13 @@ void to_json(Json &j, CompatibleNumberIntegerType val) noexcept external_constructor::construct(j, val); } +template ::value, int> = 0> +void to_json(Json &j, UnscopedEnumType e) +{ + external_constructor::construct(j, e); +} + template void from_json(Json const& j, typename Json::boolean_t& b) { @@ -603,6 +609,15 @@ void from_json(Json const& j, typename Json::number_integer_t& val) get_arithmetic_value(j, val); } +template ::value, int> = 0> +void from_json(Json const &j, UnscopedEnumType& e) +{ + typename std::underlying_type::type val = e; + get_arithmetic_value(j, val); + e = static_cast(val); +} + // overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..) // // note: Is it really necessary to provide explicit overloads for boolean_t etc.. @@ -1849,15 +1864,6 @@ class basic_json JSONSerializer>::to_json(*this, std::forward(val)); } - // Constructor for unscoped enums (not enum classes) - template ::value, int> = 0> - basic_json(T val) noexcept - : m_type(value_t::number_integer), - m_value(static_cast(val)) - { - assert_invariant(); - } - /*! @brief create a container (array or object) from an initializer list diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 7cb9169f..07055bc1 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -63,10 +63,18 @@ TEST_CASE("regression tests") SECTION("pull request #71 - handle enum type") { - enum { t = 0 }; + enum { t = 0 , u = 1}; json j = json::array(); j.push_back(t); + // maybe this is not the place to test this? + json j2 = u; + + auto anon_enum_value = j2.get(); + CHECK(u == anon_enum_value); + + static_assert(std::is_same::value, ""); + j.push_back(json::object( { {"game_type", t}