Commit 1bdb6acb authored by Niels's avatar Niels

overworked type conversion

...@@ -58,16 +58,15 @@ namespace nlohmann ...@@ -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 ...@@ -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 ...@@ -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 a string (explicit) /// get an array (explicit)
template <typename T, typename template <class T, typename
std::enable_if< std::enable_if<
std::is_constructible<T, string_t>::value, int>::type std::is_convertible<basic_json, T>::value and
= 0> not std::is_same<basic_json, T>::value
inline T get() const , int>::type = 0>
inline std::vector<T> get_impl(std::vector<T>*) const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::string): case (value_t::array):
return *m_value.string; {
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: 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) /// 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)
template <typename T, typename template <typename T, typename
std::enable_if< std::enable_if<
std::is_same<boolean_t, 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::boolean): case (value_t::string):
return m_value.boolean; {
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 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 ...@@ -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);
...@@ -3371,369 +3487,758 @@ class basic_json ...@@ -3371,369 +3487,758 @@ class basic_json
// remember the begin of the token // remember the begin of the token
m_start = m_cursor; m_start = m_cursor;
{
lexer_char_t yych;
unsigned int yyaccept = 0;
static const unsigned char yybm[] = {
0, 64, 64, 64, 64, 64, 64, 64,
64, 96, 96, 64, 64, 96, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
96, 64, 0, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
192, 192, 192, 192, 192, 192, 192, 192,
192, 192, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 0, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
};
if ((m_limit - m_cursor) < 5) { yyfill(); }; {
yych = *m_cursor; lexer_char_t yych;
if (yych <= '9') { unsigned int yyaccept = 0;
if (yych <= ' ') { static const unsigned char yybm[] =
if (yych <= '\n') { {
if (yych <= 0x00) goto basic_json_parser_27; 0, 64, 64, 64, 64, 64, 64, 64,
if (yych <= 0x08) goto basic_json_parser_29; 64, 96, 96, 64, 64, 96, 64, 64,
if (yych >= '\n') goto basic_json_parser_4; 64, 64, 64, 64, 64, 64, 64, 64,
} else { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych == '\r') goto basic_json_parser_2; 96, 64, 0, 64, 64, 64, 64, 64,
if (yych <= 0x1F) goto basic_json_parser_29; 64, 64, 64, 64, 64, 64, 64, 64,
} 192, 192, 192, 192, 192, 192, 192, 192,
} else { 192, 192, 64, 64, 64, 64, 64, 64,
if (yych <= ',') { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych == '"') goto basic_json_parser_26; 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= '+') goto basic_json_parser_29; 64, 64, 64, 64, 64, 64, 64, 64,
goto basic_json_parser_14; 64, 64, 64, 64, 0, 64, 64, 64,
} else { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= '-') goto basic_json_parser_22; 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= '/') goto basic_json_parser_29; 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= '0') goto basic_json_parser_23; 64, 64, 64, 64, 64, 64, 64, 64,
goto basic_json_parser_25; 64, 64, 64, 64, 64, 64, 64, 64,
} 64, 64, 64, 64, 64, 64, 64, 64,
} 64, 64, 64, 64, 64, 64, 64, 64,
} else { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= 'm') { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= '\\') { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= ':') goto basic_json_parser_16; 64, 64, 64, 64, 64, 64, 64, 64,
if (yych == '[') goto basic_json_parser_6; 64, 64, 64, 64, 64, 64, 64, 64,
goto basic_json_parser_29; 64, 64, 64, 64, 64, 64, 64, 64,
} else { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= ']') goto basic_json_parser_8; 64, 64, 64, 64, 64, 64, 64, 64,
if (yych == 'f') goto basic_json_parser_21; 64, 64, 64, 64, 64, 64, 64, 64,
goto basic_json_parser_29; 64, 64, 64, 64, 64, 64, 64, 64,
} 64, 64, 64, 64, 64, 64, 64, 64,
} else { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= 'z') { 64, 64, 64, 64, 64, 64, 64, 64,
if (yych <= 'n') goto basic_json_parser_18; };
if (yych == 't') goto basic_json_parser_20;
goto basic_json_parser_29; if ((m_limit - m_cursor) < 5)
} else { {
if (yych <= '{') goto basic_json_parser_10; yyfill();
if (yych == '}') goto basic_json_parser_12; };
goto basic_json_parser_29; yych = *m_cursor;
if (yych <= '9')
{
if (yych <= ' ')
{
if (yych <= '\n')
{
if (yych <= 0x00)
{
goto basic_json_parser_27;
}
if (yych <= 0x08)
{
goto basic_json_parser_29;
}
if (yych >= '\n')
{
goto basic_json_parser_4;
}
}
else
{
if (yych == '\r')
{
goto basic_json_parser_2;
}
if (yych <= 0x1F)
{
goto basic_json_parser_29;
}
}
}
else
{
if (yych <= ',')
{
if (yych == '"')
{
goto basic_json_parser_26;
}
if (yych <= '+')
{
goto basic_json_parser_29;
}
goto basic_json_parser_14;
}
else
{
if (yych <= '-')
{
goto basic_json_parser_22;
}
if (yych <= '/')
{
goto basic_json_parser_29;
}
if (yych <= '0')
{
goto basic_json_parser_23;
}
goto basic_json_parser_25;
}
}
}
else
{
if (yych <= 'm')
{
if (yych <= '\\')
{
if (yych <= ':')
{
goto basic_json_parser_16;
}
if (yych == '[')
{
goto basic_json_parser_6;
}
goto basic_json_parser_29;
}
else
{
if (yych <= ']')
{
goto basic_json_parser_8;
}
if (yych == 'f')
{
goto basic_json_parser_21;
}
goto basic_json_parser_29;
}
}
else
{
if (yych <= 'z')
{
if (yych <= 'n')
{
goto basic_json_parser_18;
}
if (yych == 't')
{
goto basic_json_parser_20;
}
goto basic_json_parser_29;
}
else
{
if (yych <= '{')
{
goto basic_json_parser_10;
}
if (yych == '}')
{
goto basic_json_parser_12;
}
goto basic_json_parser_29;
}
}
} }
}
}
basic_json_parser_2: basic_json_parser_2:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
goto basic_json_parser_5; goto basic_json_parser_5;
basic_json_parser_3: basic_json_parser_3:
{ return scan(); } {
return scan();
}
basic_json_parser_4: basic_json_parser_4:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
yyfill();
};
yych = *m_cursor;
basic_json_parser_5: basic_json_parser_5:
if (yybm[0+yych] & 32) { if (yybm[0 + yych] & 32)
goto basic_json_parser_4; {
} goto basic_json_parser_4;
goto basic_json_parser_3; }
goto basic_json_parser_3;
basic_json_parser_6: basic_json_parser_6:
++m_cursor; ++m_cursor;
{ return token_type::begin_array; } {
return token_type::begin_array;
}
basic_json_parser_8: basic_json_parser_8:
++m_cursor; ++m_cursor;
{ return token_type::end_array; } {
return token_type::end_array;
}
basic_json_parser_10: basic_json_parser_10:
++m_cursor; ++m_cursor;
{ return token_type::begin_object; } {
return token_type::begin_object;
}
basic_json_parser_12: basic_json_parser_12:
++m_cursor; ++m_cursor;
{ return token_type::end_object; } {
return token_type::end_object;
}
basic_json_parser_14: basic_json_parser_14:
++m_cursor; ++m_cursor;
{ return token_type::value_separator; } {
return token_type::value_separator;
}
basic_json_parser_16: basic_json_parser_16:
++m_cursor; ++m_cursor;
{ return token_type::name_separator; } {
return token_type::name_separator;
}
basic_json_parser_18: basic_json_parser_18:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'u') goto basic_json_parser_59; if (yych == 'u')
{
goto basic_json_parser_59;
}
basic_json_parser_19: basic_json_parser_19:
{ return token_type::parse_error; } {
return token_type::parse_error;
}
basic_json_parser_20: basic_json_parser_20:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'r') goto basic_json_parser_55; if (yych == 'r')
goto basic_json_parser_19; {
goto basic_json_parser_55;
}
goto basic_json_parser_19;
basic_json_parser_21: basic_json_parser_21:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'a') goto basic_json_parser_50; if (yych == 'a')
goto basic_json_parser_19; {
goto basic_json_parser_50;
}
goto basic_json_parser_19;
basic_json_parser_22: basic_json_parser_22:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') goto basic_json_parser_19; if (yych <= '/')
if (yych <= '0') goto basic_json_parser_49; {
if (yych <= '9') goto basic_json_parser_40; goto basic_json_parser_19;
goto basic_json_parser_19; }
if (yych <= '0')
{
goto basic_json_parser_49;
}
if (yych <= '9')
{
goto basic_json_parser_40;
}
goto basic_json_parser_19;
basic_json_parser_23: basic_json_parser_23:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 'D') { if (yych <= 'D')
if (yych == '.') goto basic_json_parser_42; {
} else { if (yych == '.')
if (yych <= 'E') goto basic_json_parser_43; {
if (yych == 'e') goto basic_json_parser_43; goto basic_json_parser_42;
} }
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
}
basic_json_parser_24: basic_json_parser_24:
{ return token_type::value_number; } {
return token_type::value_number;
}
basic_json_parser_25: basic_json_parser_25:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
goto basic_json_parser_41; goto basic_json_parser_41;
basic_json_parser_26: basic_json_parser_26:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 0x00) goto basic_json_parser_19; if (yych <= 0x00)
goto basic_json_parser_31; {
goto basic_json_parser_19;
}
goto basic_json_parser_31;
basic_json_parser_27: basic_json_parser_27:
++m_cursor; ++m_cursor;
{ return token_type::end_of_input; } {
return token_type::end_of_input;
}
basic_json_parser_29: basic_json_parser_29:
yych = *++m_cursor; yych = *++m_cursor;
goto basic_json_parser_19; goto basic_json_parser_19;
basic_json_parser_30: basic_json_parser_30:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
yyfill();
};
yych = *m_cursor;
basic_json_parser_31: basic_json_parser_31:
if (yybm[0+yych] & 64) { if (yybm[0 + yych] & 64)
goto basic_json_parser_30; {
}
if (yych <= 0x00) goto basic_json_parser_32;
if (yych <= '"') goto basic_json_parser_34;
goto basic_json_parser_33;
basic_json_parser_32:
m_cursor = m_marker;
if (yyaccept == 0) {
goto basic_json_parser_19;
} else {
goto basic_json_parser_24;
}
basic_json_parser_33:
++m_cursor;
if (m_limit <= m_cursor) { yyfill(); };
yych = *m_cursor;
if (yych <= 'e') {
if (yych <= '/') {
if (yych == '"') goto basic_json_parser_30;
if (yych <= '.') goto basic_json_parser_32;
goto basic_json_parser_30;
} else {
if (yych <= '\\') {
if (yych <= '[') goto basic_json_parser_32;
goto basic_json_parser_30; goto basic_json_parser_30;
} else {
if (yych == 'b') goto basic_json_parser_30;
goto basic_json_parser_32;
} }
} if (yych <= 0x00)
} else { {
if (yych <= 'q') {
if (yych <= 'f') goto basic_json_parser_30;
if (yych == 'n') goto basic_json_parser_30;
goto basic_json_parser_32;
} else {
if (yych <= 's') {
if (yych <= 'r') goto basic_json_parser_30;
goto basic_json_parser_32;
} else {
if (yych <= 't') goto basic_json_parser_30;
if (yych <= 'u') goto basic_json_parser_36;
goto basic_json_parser_32; goto basic_json_parser_32;
} }
} if (yych <= '"')
} {
goto basic_json_parser_34;
}
goto basic_json_parser_33;
basic_json_parser_32:
m_cursor = m_marker;
if (yyaccept == 0)
{
goto basic_json_parser_19;
}
else
{
goto basic_json_parser_24;
}
basic_json_parser_33:
++m_cursor;
if (m_limit <= m_cursor)
{
yyfill();
};
yych = *m_cursor;
if (yych <= 'e')
{
if (yych <= '/')
{
if (yych == '"')
{
goto basic_json_parser_30;
}
if (yych <= '.')
{
goto basic_json_parser_32;
}
goto basic_json_parser_30;
}
else
{
if (yych <= '\\')
{
if (yych <= '[')
{
goto basic_json_parser_32;
}
goto basic_json_parser_30;
}
else
{
if (yych == 'b')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
}
}
else
{
if (yych <= 'q')
{
if (yych <= 'f')
{
goto basic_json_parser_30;
}
if (yych == 'n')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
else
{
if (yych <= 's')
{
if (yych <= 'r')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
else
{
if (yych <= 't')
{
goto basic_json_parser_30;
}
if (yych <= 'u')
{
goto basic_json_parser_36;
}
goto basic_json_parser_32;
}
}
}
basic_json_parser_34: basic_json_parser_34:
++m_cursor; ++m_cursor;
{ return token_type::value_string; } {
return token_type::value_string;
}
basic_json_parser_36: basic_json_parser_36:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
if (yych <= '@') { yyfill();
if (yych <= '/') goto basic_json_parser_32; };
if (yych >= ':') goto basic_json_parser_32; yych = *m_cursor;
} else { if (yych <= '@')
if (yych <= 'F') goto basic_json_parser_37; {
if (yych <= '`') goto basic_json_parser_32; if (yych <= '/')
if (yych >= 'g') goto basic_json_parser_32; {
} goto basic_json_parser_32;
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_37;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_37: basic_json_parser_37:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
if (yych <= '@') { yyfill();
if (yych <= '/') goto basic_json_parser_32; };
if (yych >= ':') goto basic_json_parser_32; yych = *m_cursor;
} else { if (yych <= '@')
if (yych <= 'F') goto basic_json_parser_38; {
if (yych <= '`') goto basic_json_parser_32; if (yych <= '/')
if (yych >= 'g') goto basic_json_parser_32; {
} goto basic_json_parser_32;
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_38;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_38: basic_json_parser_38:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
if (yych <= '@') { yyfill();
if (yych <= '/') goto basic_json_parser_32; };
if (yych >= ':') goto basic_json_parser_32; yych = *m_cursor;
} else { if (yych <= '@')
if (yych <= 'F') goto basic_json_parser_39; {
if (yych <= '`') goto basic_json_parser_32; if (yych <= '/')
if (yych >= 'g') goto basic_json_parser_32; {
} goto basic_json_parser_32;
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_39;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_39: basic_json_parser_39:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
if (yych <= '@') { yyfill();
if (yych <= '/') goto basic_json_parser_32; };
if (yych <= '9') goto basic_json_parser_30; yych = *m_cursor;
goto basic_json_parser_32; if (yych <= '@')
} else { {
if (yych <= 'F') goto basic_json_parser_30; if (yych <= '/')
if (yych <= '`') goto basic_json_parser_32; {
if (yych <= 'f') goto basic_json_parser_30; goto basic_json_parser_32;
goto basic_json_parser_32; }
} if (yych <= '9')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_30;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych <= 'f')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
basic_json_parser_40: basic_json_parser_40:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
if ((m_limit - m_cursor) < 3) { yyfill(); }; if ((m_limit - m_cursor) < 3)
yych = *m_cursor; {
yyfill();
};
yych = *m_cursor;
basic_json_parser_41: basic_json_parser_41:
if (yybm[0+yych] & 128) { if (yybm[0 + yych] & 128)
goto basic_json_parser_40; {
} goto basic_json_parser_40;
if (yych <= 'D') { }
if (yych != '.') goto basic_json_parser_24; if (yych <= 'D')
} else { {
if (yych <= 'E') goto basic_json_parser_43; if (yych != '.')
if (yych == 'e') goto basic_json_parser_43; {
goto basic_json_parser_24; goto basic_json_parser_24;
} }
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_json_parser_42: basic_json_parser_42:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') goto basic_json_parser_32; if (yych <= '/')
if (yych <= '9') goto basic_json_parser_47; {
goto basic_json_parser_32; goto basic_json_parser_32;
}
if (yych <= '9')
{
goto basic_json_parser_47;
}
goto basic_json_parser_32;
basic_json_parser_43: basic_json_parser_43:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= ',') { if (yych <= ',')
if (yych != '+') goto basic_json_parser_32; {
} else { if (yych != '+')
if (yych <= '-') goto basic_json_parser_44; {
if (yych <= '/') goto basic_json_parser_32; goto basic_json_parser_32;
if (yych <= '9') goto basic_json_parser_45; }
goto basic_json_parser_32; }
} else
{
if (yych <= '-')
{
goto basic_json_parser_44;
}
if (yych <= '/')
{
goto basic_json_parser_32;
}
if (yych <= '9')
{
goto basic_json_parser_45;
}
goto basic_json_parser_32;
}
basic_json_parser_44: basic_json_parser_44:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') goto basic_json_parser_32; if (yych <= '/')
if (yych >= ':') goto basic_json_parser_32; {
goto basic_json_parser_32;
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
basic_json_parser_45: basic_json_parser_45:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) { yyfill(); }; if (m_limit <= m_cursor)
yych = *m_cursor; {
if (yych <= '/') goto basic_json_parser_24; yyfill();
if (yych <= '9') goto basic_json_parser_45; };
goto basic_json_parser_24; yych = *m_cursor;
if (yych <= '/')
{
goto basic_json_parser_24;
}
if (yych <= '9')
{
goto basic_json_parser_45;
}
goto basic_json_parser_24;
basic_json_parser_47: basic_json_parser_47:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
if ((m_limit - m_cursor) < 3) { yyfill(); }; if ((m_limit - m_cursor) < 3)
yych = *m_cursor; {
if (yych <= 'D') { yyfill();
if (yych <= '/') goto basic_json_parser_24; };
if (yych <= '9') goto basic_json_parser_47; yych = *m_cursor;
goto basic_json_parser_24; if (yych <= 'D')
} else { {
if (yych <= 'E') goto basic_json_parser_43; if (yych <= '/')
if (yych == 'e') goto basic_json_parser_43; {
goto basic_json_parser_24; goto basic_json_parser_24;
} }
if (yych <= '9')
{
goto basic_json_parser_47;
}
goto basic_json_parser_24;
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_json_parser_49: basic_json_parser_49:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 'D') { if (yych <= 'D')
if (yych == '.') goto basic_json_parser_42; {
goto basic_json_parser_24; if (yych == '.')
} else { {
if (yych <= 'E') goto basic_json_parser_43; goto basic_json_parser_42;
if (yych == 'e') goto basic_json_parser_43; }
goto basic_json_parser_24; goto basic_json_parser_24;
} }
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_json_parser_50: basic_json_parser_50:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') goto basic_json_parser_32; if (yych != 'l')
yych = *++m_cursor; {
if (yych != 's') goto basic_json_parser_32; goto basic_json_parser_32;
yych = *++m_cursor; }
if (yych != 'e') goto basic_json_parser_32; yych = *++m_cursor;
++m_cursor; if (yych != 's')
{ return token_type::literal_false; } {
goto basic_json_parser_32;
}
yych = *++m_cursor;
if (yych != 'e')
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_false;
}
basic_json_parser_55: basic_json_parser_55:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'u') goto basic_json_parser_32; if (yych != 'u')
yych = *++m_cursor; {
if (yych != 'e') goto basic_json_parser_32; goto basic_json_parser_32;
++m_cursor; }
{ return token_type::literal_true; } yych = *++m_cursor;
if (yych != 'e')
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_true;
}
basic_json_parser_59: basic_json_parser_59:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') goto basic_json_parser_32; if (yych != 'l')
yych = *++m_cursor; {
if (yych != 'l') goto basic_json_parser_32; goto basic_json_parser_32;
++m_cursor; }
{ return token_type::literal_null; } yych = *++m_cursor;
} if (yych != 'l')
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_null;
}
}
} }
......
...@@ -58,16 +58,15 @@ namespace nlohmann ...@@ -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 ...@@ -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 ...@@ -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 a string (explicit) /// get an array (explicit)
template <typename T, typename template <class T, typename
std::enable_if< std::enable_if<
std::is_constructible<T, string_t>::value, int>::type std::is_convertible<basic_json, T>::value and
= 0> not std::is_same<basic_json, T>::value
inline T get() const , int>::type = 0>
inline std::vector<T> get_impl(std::vector<T>*) const
{ {
switch (m_type) switch (m_type)
{ {
case (value_t::string): case (value_t::array):
return *m_value.string; {
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: 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) /// 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)
template <typename T, typename template <typename T, typename
std::enable_if< std::enable_if<
std::is_same<boolean_t, 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::boolean): case (value_t::string):
return m_value.boolean; {
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 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 ...@@ -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);
......
...@@ -2006,6 +2006,108 @@ TEST_CASE("value conversion") ...@@ -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")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment