overworked type conversion

This commit is contained in:
Niels 2015-04-08 22:23:40 +02:00
parent 0a96116b0c
commit 1bdb6acb1f
3 changed files with 1116 additions and 393 deletions

File diff suppressed because it is too large Load diff

View file

@ -58,16 +58,15 @@ namespace nlohmann
// Helper to determine whether there's a key_type for T. // Helper to determine whether there's a key_type for T.
// http://stackoverflow.com/a/7728728/266378 // http://stackoverflow.com/a/7728728/266378
template<typename T> template<typename T>
struct has_key_type struct has_mapped_type
{ {
private: private:
template<typename C> static char test(typename C::key_type*); template<typename C> static char test(typename C::mapped_type*);
template<typename C> static int test(...); template<typename C> static int test(...);
public: public:
enum { value = sizeof(test<T>(0)) == sizeof(char) }; enum { value = sizeof(test<T>(0)) == sizeof(char) };
}; };
/*! /*!
@brief JSON @brief JSON
@ -814,7 +813,7 @@ class basic_json
return m_type; return m_type;
} }
private:
////////////////////// //////////////////////
// value conversion // // value conversion //
////////////////////// //////////////////////
@ -822,89 +821,206 @@ class basic_json
/// get an object (explicit) /// get an object (explicit)
template <class T, typename template <class T, typename
std::enable_if< std::enable_if<
std::is_constructible<typename T::key_type, typename object_t::key_type>::value and std::is_convertible<typename object_t::key_type, typename T::key_type>::value and
std::is_constructible<typename T::mapped_type, basic_json>::value, int>::type std::is_convertible<basic_json, typename T::mapped_type>::value
= 0> , int>::type = 0>
inline T get() const inline T get_impl(T*) const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::object): case (value_t::object):
{
return T(m_value.object->begin(), m_value.object->end()); return T(m_value.object->begin(), m_value.object->end());
}
default: default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an object (explicit)
inline object_t get_impl(object_t*) const
{
switch (m_type)
{
case (value_t::object):
{
return *(m_value.object);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to object");
}
} }
} }
/// get an array (explicit) /// get an array (explicit)
template <class T, typename template <class T, typename
std::enable_if< std::enable_if<
not std::is_same<T, string_t>::value and std::is_convertible<basic_json, typename T::value_type>::value and
not has_key_type<T>::value and not std::is_same<basic_json, typename T::value_type>::value and
std::is_constructible<typename T::value_type, basic_json>::value, int>::type not std::is_arithmetic<T>::value and
= 0> not std::is_convertible<std::string, T>::value and
inline T get() const not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::array): case (value_t::array):
return T(m_value.array->begin(), m_value.array->end()); {
T to_vector;
//to_vector.reserve(m_value.array->size());
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<typename T::value_type>();
});
return to_vector;
// return T(m_value.array->begin(), m_value.array->end());
}
default: default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_convertible<basic_json, T>::value and
not std::is_same<basic_json, T>::value
, int>::type = 0>
inline std::vector<T> get_impl(std::vector<T>*) const
{
switch (m_type)
{
case (value_t::array):
{
std::vector<T> to_vector;
to_vector.reserve(m_value.array->size());
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<T>();
});
return to_vector;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_same<basic_json, typename T::value_type>::value and
not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::array):
{
return T(m_value.array->begin(), m_value.array->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
inline array_t get_impl(array_t*) const
{
switch (m_type)
{
case (value_t::array):
{
return *(m_value.array);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to array");
}
} }
} }
/// get a string (explicit) /// get a string (explicit)
template <typename T, typename template <typename T, typename
std::enable_if< std::enable_if<
std::is_constructible<T, string_t>::value, int>::type std::is_convertible<string_t, T>::value
= 0> , int>::type = 0>
inline T get() const inline T get_impl(T*) const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::string): case (value_t::string):
{
return *m_value.string; return *m_value.string;
}
default: default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
} }
}
/// get a boolean (explicit)
template <typename T, typename
std::enable_if<
std::is_same<boolean_t, T>::value, int>::type
= 0>
inline T get() const
{
switch (m_type)
{
case (value_t::boolean):
return m_value.boolean;
default:
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
} }
} }
/// get a number (explicit) /// get a number (explicit)
template<typename T, typename template<typename T, typename
std::enable_if< std::enable_if<
not std::is_same<boolean_t, T>::value and std::is_arithmetic<T>::value
std::is_arithmetic<T>::value, int>::type , int>::type = 0>
= 0> inline T get_impl(T*) const
inline T get() const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::number_integer): case (value_t::number_integer):
{
return static_cast<T>(m_value.number_integer); return static_cast<T>(m_value.number_integer);
}
case (value_t::number_float): case (value_t::number_float):
{
return static_cast<T>(m_value.number_float); return static_cast<T>(m_value.number_float);
}
default: default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
} }
} }
/// get a boolean (explicit)
inline boolean_t get_impl(boolean_t*) const
{
switch (m_type)
{
case (value_t::boolean):
{
return m_value.boolean;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(boolean_t).name());
}
}
}
public:
/// get a value (explicit)
// <http://stackoverflow.com/a/8315197/266378>
template<typename T>
inline T get() const
{
return get_impl(static_cast<T*>(nullptr));
}
/// get a value (implicit) /// get a value (implicit)
template<typename T> template<typename T>
inline operator T() const inline operator T() const
@ -1077,7 +1193,7 @@ class basic_json
// at only works for objects // at only works for objects
if (m_type != value_t::object) if (m_type != value_t::object)
{ {
throw std::runtime_error("cannot use at with " + type_name()); throw std::runtime_error("cannot use erase with " + type_name());
} }
return m_value.object->erase(key); return m_value.object->erase(key);

View file

@ -2006,6 +2006,108 @@ TEST_CASE("value conversion")
CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float)); CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
} }
} }
SECTION("more involved conversions")
{
SECTION("object-like STL containers")
{
json j1 = {{"one", 1}, {"two", 2}, {"three", 3}};
json j2 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}};
json j3 = {{"one", true}, {"two", false}, {"three", true}};
json j4 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}};
SECTION("std::map")
{
auto m1 = j1.get<std::map<std::string, int>>();
auto m2 = j2.get<std::map<std::string, double>>();
auto m3 = j3.get<std::map<std::string, bool>>();
//auto m4 = j4.get<std::map<std::string, std::string>>();
}
SECTION("std::unordered_map")
{
auto m1 = j1.get<std::unordered_map<std::string, int>>();
auto m2 = j2.get<std::unordered_map<std::string, double>>();
auto m3 = j3.get<std::unordered_map<std::string, bool>>();
//auto m4 = j4.get<std::unordered_map<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
SECTION("std::multimap")
{
auto m1 = j1.get<std::multimap<std::string, int>>();
auto m2 = j2.get<std::multimap<std::string, double>>();
auto m3 = j3.get<std::multimap<std::string, bool>>();
//auto m4 = j4.get<std::multimap<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
SECTION("std::unordered_multimap")
{
auto m1 = j1.get<std::unordered_multimap<std::string, int>>();
auto m2 = j2.get<std::unordered_multimap<std::string, double>>();
auto m3 = j3.get<std::unordered_multimap<std::string, bool>>();
//auto m4 = j4.get<std::unordered_multimap<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
}
SECTION("array-like STL containers")
{
json j1 = {1, 2, 3, 4};
json j2 = {1.2, 2.3, 3.4, 4.5};
json j3 = {true, false, true};
json j4 = {"one", "two", "three"};
SECTION("std::list")
{
auto m1 = j1.get<std::list<int>>();
auto m2 = j2.get<std::list<double>>();
auto m3 = j3.get<std::list<bool>>();
auto m4 = j4.get<std::list<std::string>>();
}
//SECTION("std::forward_list")
//{
// auto m1 = j1.get<std::forward_list<int>>();
// auto m2 = j2.get<std::forward_list<double>>();
// auto m3 = j3.get<std::forward_list<bool>>();
// auto m4 = j4.get<std::forward_list<std::string>>();
//}
SECTION("std::vector")
{
auto m1 = j1.get<std::vector<int>>();
auto m2 = j2.get<std::vector<double>>();
auto m3 = j3.get<std::vector<bool>>();
auto m4 = j4.get<std::vector<std::string>>();
}
SECTION("std::deque")
{
auto m1 = j1.get<std::deque<int>>();
auto m2 = j2.get<std::deque<double>>();
auto m3 = j3.get<std::deque<bool>>();
auto m4 = j4.get<std::deque<std::string>>();
}
SECTION("std::set")
{
auto m1 = j1.get<std::set<int>>();
auto m2 = j2.get<std::set<double>>();
auto m3 = j3.get<std::set<bool>>();
auto m4 = j4.get<std::set<std::string>>();
}
SECTION("std::unordered_set")
{
auto m1 = j1.get<std::unordered_set<int>>();
auto m2 = j2.get<std::unordered_set<double>>();
auto m3 = j3.get<std::unordered_set<bool>>();
auto m4 = j4.get<std::unordered_set<std::string>>();
}
}
}
} }
TEST_CASE("element access") TEST_CASE("element access")