Skip to content

Commit

Permalink
add support for enum classes
Browse files Browse the repository at this point in the history
  • Loading branch information
theodelrieu committed Dec 13, 2016
1 parent f9e02e9 commit 13082c1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
21 changes: 18 additions & 3 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ using uncvref_t = remove_cv_t<remove_reference_t<T>>;
template <bool If, typename Then, typename Else>
using conditional_t = typename std::conditional<If, Then, Else>::type;

// Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait
template <typename T>
using is_scoped_enum =
std::integral_constant<bool, not std::is_convertible<T, int>::value and
std::is_enum<T>::value>;

template <typename T>
using is_unscoped_enum =
std::integral_constant<bool, std::is_convertible<T, int>::value and
std::is_enum<T>::value>;

// TODO update this doc
/*!
@brief unnamed namespace with internal helper functions
Expand All @@ -115,6 +126,10 @@ using conditional_t = typename std::conditional<If, Then, Else>::type;

namespace detail
{
// Very useful construct against boilerplate (more boilerplate needed than in C++17: http://en.cppreference.com/w/cpp/types/void_t)
template <typename...> struct make_void { using type = void; };
template <typename... Ts> using void_t = typename make_void<Ts...>::type;

// Implementation of 3 C++17 constructs: conjunction, disjunction, negation.
// This is needed to avoid evaluating all the traits in a condition
//
Expand Down Expand Up @@ -262,6 +277,7 @@ template <typename T, typename BasicJson>
struct is_compatible_basic_json_type
{
static auto constexpr value =
is_unscoped_enum<T>::value or
std::is_same<T, BasicJson>::value or
std::is_constructible<typename BasicJson::string_t, T>::value or
std::is_same<typename BasicJson::boolean_t, T>::value or
Expand Down Expand Up @@ -1512,7 +1528,6 @@ class basic_json
not detail::is_compatible_basic_json_type<
uncvref_t<T>, basic_json_t>::value and
not detail::is_basic_json_nested_class<uncvref_t<T>, basic_json_t, primitive_iterator_t>::value and
not std::is_enum<uncvref_t<T>>::value and
not std::is_same<uncvref_t<T>, typename basic_json_t::array_t::iterator>::value and
not std::is_same<uncvref_t<T>, typename basic_json_t::object_t::iterator>::value and
detail::has_to_json<JSONSerializer, basic_json,
Expand Down Expand Up @@ -1685,8 +1700,8 @@ class basic_json
@since version 1.0.0
*/

// Quickfix, accept every enum type, without looking if a to_json method is provided...
template <typename T, enable_if_t<std::is_enum<T>::value, int> = 0>
// Constructor for unscoped enums (not enum classes)
template <typename T, enable_if_t<is_unscoped_enum<T>::value, int> = 0>
basic_json(T val) noexcept
: m_type(value_t::number_integer),
m_value(static_cast<number_integer_t>(val))
Expand Down
62 changes: 53 additions & 9 deletions test/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ SOFTWARE.

namespace udt
{
enum class country
{
china,
france,
russia
};

struct age
{
int m_val;
Expand All @@ -54,6 +61,7 @@ namespace udt
{
age m_age;
name m_name;
country m_country;
};

struct contact
Expand All @@ -69,7 +77,7 @@ namespace udt
};
}

// to_json methods for default basic_json
// to_json methods
namespace udt
{
void to_json(nlohmann::json& j, age a)
Expand All @@ -82,10 +90,26 @@ namespace udt
j = n.m_val;
}

void to_json(nlohmann::json& j, country c)
{
switch (c)
{
case country::china:
j = u8"中华人民共和国";
return;
case country::france:
j = "France";
return;
case country::russia:
j = u8"Российская Федерация";
return;
}
}

void to_json(nlohmann::json& j, person const& p)
{
using nlohmann::json;
j = json{{"age", p.m_age}, {"name", p.m_name}};
j = json{{"age", p.m_age}, {"name", p.m_name}, {"country", p.m_country}};
}

void to_json(nlohmann::json& j, address const& a)
Expand Down Expand Up @@ -139,7 +163,7 @@ namespace udt
}
}

// from_json methods for default basic_json
// from_json methods
namespace udt
{
void from_json(nlohmann::json const& j, age &a)
Expand All @@ -152,10 +176,24 @@ namespace udt
n.m_val = j.get<std::string>();
}

void from_json(nlohmann::json const &j, country &c)
{
const auto str = j.get<std::string>();
static const std::map<std::string, country> m = {
{u8"中华人民共和国", country::china},
{"France", country::france},
{"Российская Федерация", country::russia}};

const auto it = m.find(str);
// TODO test exceptions
c = it->second;
}

void from_json(nlohmann::json const& j, person &p)
{
p.m_age = j["age"].get<age>();
p.m_name = j["name"].get<name>();
p.m_country = j["country"].get<country>();
}

void from_json(nlohmann::json const &j, address &a)
Expand Down Expand Up @@ -183,29 +221,33 @@ TEST_CASE("basic usage", "[udt]")
// a bit narcissic maybe :) ?
const udt::age a{23};
const udt::name n{"theo"};
const udt::person sfinae_addict{a, n};
const udt::country c{udt::country::france};
const udt::person sfinae_addict{a, n, c};
const udt::person senior_programmer{{42}, {u8"王芳"}, udt::country::china};
const udt::address addr{"Paris"};
const udt::contact cpp_programmer{sfinae_addict, addr};
const udt::contact_book book{{"C++"}, {cpp_programmer, cpp_programmer}};
const udt::contact_book book{{"C++"}, {cpp_programmer, {senior_programmer, addr}}};

SECTION("conversion to json via free-functions")
{
CHECK(json(a) == json(23));
CHECK(json(n) == json("theo"));
CHECK(json(sfinae_addict) == R"({"name":"theo", "age":23})"_json);
CHECK(json(c) == json("France"));
CHECK(json(sfinae_addict) == R"({"name":"theo", "age":23, "country":"France"})"_json);
CHECK(json("Paris") == json(addr));
CHECK(json(cpp_programmer) ==
R"({"person" : {"age":23, "name":"theo"}, "address":"Paris"})"_json);
R"({"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"})"_json);

CHECK(
json(book) ==
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo"}, "address":"Paris"}, {"person" : {"age":23, "name":"theo"}, "address":"Paris"}]})"_json);
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json);

}

SECTION("conversion from json via free-functions")
{
const auto big_json =
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo"}, "address":"Paris"}, {"person" : {"age":23, "name":"theo"}, "address":"Paris"}]})"_json;
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json;
const auto parsed_book = big_json.get<udt::contact_book>();
const auto book_name = big_json["name"].get<udt::name>();
const auto contacts = big_json["contacts"].get<std::vector<udt::contact>>();
Expand All @@ -214,10 +256,12 @@ TEST_CASE("basic usage", "[udt]")
const auto person = contact_json["person"].get<udt::person>();
const auto address = contact_json["address"].get<udt::address>();
const auto age = contact_json["person"]["age"].get<udt::age>();
const auto country = contact_json["person"]["country"].get<udt::country>();
const auto name = contact_json["person"]["name"].get<udt::name>();

CHECK(age == a);
CHECK(name == n);
CHECK(country == c);
CHECK(address == addr);
CHECK(person == sfinae_addict);
CHECK(contact == cpp_programmer);
Expand Down

0 comments on commit 13082c1

Please sign in to comment.