rename template argument Json -> BasicJsonType

This commit is contained in:
Théo DELRIEU 2017-01-21 16:41:14 +01:00
parent 708eb9613b
commit 7f359017d2
3 changed files with 1183 additions and 705 deletions

File diff suppressed because it is too large Load diff

View file

@ -187,7 +187,7 @@ struct conjunction<B1, Bn...>
template <class B> struct negation : std::integral_constant < bool, !B::value > {}; template <class B> struct negation : std::integral_constant < bool, !B::value > {};
template <typename Json> std::string type_name(const Json& j) template <typename BasicJsonType> std::string type_name(const BasicJsonType& j)
{ {
switch (j.m_type) switch (j.m_type)
{ {
@ -220,8 +220,8 @@ template <value_t> struct external_constructor;
template <> template <>
struct external_constructor<value_t::boolean> struct external_constructor<value_t::boolean>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, typename Json::boolean_t b) noexcept static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
{ {
j.m_type = value_t::boolean; j.m_type = value_t::boolean;
j.m_value = b; j.m_value = b;
@ -232,8 +232,8 @@ struct external_constructor<value_t::boolean>
template <> template <>
struct external_constructor<value_t::string> struct external_constructor<value_t::string>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, const typename Json::string_t& s) static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
{ {
j.m_type = value_t::string; j.m_type = value_t::string;
j.m_value = s; j.m_value = s;
@ -244,12 +244,12 @@ struct external_constructor<value_t::string>
template <> template <>
struct external_constructor<value_t::number_float> struct external_constructor<value_t::number_float>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, typename Json::number_float_t val) noexcept static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
{ {
// replace infinity and NAN by null // replace infinity and NAN by null
if (not std::isfinite(val)) if (not std::isfinite(val))
j = Json{}; j = BasicJsonType{};
else else
{ {
j.m_type = value_t::number_float; j.m_type = value_t::number_float;
@ -262,8 +262,8 @@ struct external_constructor<value_t::number_float>
template <> template <>
struct external_constructor<value_t::number_unsigned> struct external_constructor<value_t::number_unsigned>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, typename Json::number_unsigned_t val) noexcept static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
{ {
j.m_type = value_t::number_unsigned; j.m_type = value_t::number_unsigned;
j.m_value = val; j.m_value = val;
@ -274,8 +274,8 @@ struct external_constructor<value_t::number_unsigned>
template <> template <>
struct external_constructor<value_t::number_integer> struct external_constructor<value_t::number_integer>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, typename Json::number_integer_t val) noexcept static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
{ {
j.m_type = value_t::number_integer; j.m_type = value_t::number_integer;
j.m_value = val; j.m_value = val;
@ -286,25 +286,25 @@ struct external_constructor<value_t::number_integer>
template <> template <>
struct external_constructor<value_t::array> struct external_constructor<value_t::array>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, const typename Json::array_t& arr) static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
{ {
j.m_type = value_t::array; j.m_type = value_t::array;
j.m_value = arr; j.m_value = arr;
j.assert_invariant(); j.assert_invariant();
} }
template <typename Json, typename CompatibleArrayType, template <typename BasicJsonType, typename CompatibleArrayType,
enable_if_t<not std::is_same<CompatibleArrayType, enable_if_t<not std::is_same<CompatibleArrayType,
typename Json::array_t>::value, typename BasicJsonType::array_t>::value,
int> = 0> int> = 0>
static void construct(Json& j, const CompatibleArrayType& arr) static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
j.m_type = value_t::array; j.m_type = value_t::array;
j.m_value.array = j.m_value.array =
j.template create<typename Json::array_t>(begin(arr), end(arr)); j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
j.assert_invariant(); j.assert_invariant();
} }
}; };
@ -312,26 +312,26 @@ struct external_constructor<value_t::array>
template <> template <>
struct external_constructor<value_t::object> struct external_constructor<value_t::object>
{ {
template <typename Json> template <typename BasicJsonType>
static void construct(Json& j, const typename Json::object_t& obj) static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
{ {
j.m_type = value_t::object; j.m_type = value_t::object;
j.m_value = obj; j.m_value = obj;
j.assert_invariant(); j.assert_invariant();
} }
template <typename Json, typename CompatibleObjectType, template <typename BasicJsonType, typename CompatibleObjectType,
enable_if_t<not std::is_same<CompatibleObjectType, enable_if_t<not std::is_same<CompatibleObjectType,
typename Json::object_t>::value, typename BasicJsonType::object_t>::value,
int> = 0> int> = 0>
static void construct(Json& j, const CompatibleObjectType& obj) static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
j.m_type = value_t::object; j.m_type = value_t::object;
j.m_value.object = j.m_value.object =
j.template create<typename Json::object_t>(begin(obj), end(obj)); j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
j.assert_invariant(); j.assert_invariant();
} }
}; };
@ -377,27 +377,27 @@ struct is_compatible_object_type_impl<true, RealType, CompatibleObjectType>
typename CompatibleObjectType::mapped_type>::value; typename CompatibleObjectType::mapped_type>::value;
}; };
template<class Json, class CompatibleObjectType> template<class BasicJsonType, class CompatibleObjectType>
struct is_compatible_object_type struct is_compatible_object_type
{ {
static auto constexpr value = is_compatible_object_type_impl < static auto constexpr value = is_compatible_object_type_impl <
conjunction<negation<std::is_same<void, CompatibleObjectType>>, conjunction<negation<std::is_same<void, CompatibleObjectType>>,
has_mapped_type<CompatibleObjectType>, has_mapped_type<CompatibleObjectType>,
has_key_type<CompatibleObjectType>>::value, has_key_type<CompatibleObjectType>>::value,
typename Json::object_t, CompatibleObjectType >::value; typename BasicJsonType::object_t, CompatibleObjectType >::value;
}; };
template <typename Json, typename T> template <typename BasicJsonType, typename T>
struct is_basic_json_nested_type struct is_basic_json_nested_type
{ {
static auto constexpr value = std::is_same<T, typename Json::iterator>::value or static auto constexpr value = std::is_same<T, typename BasicJsonType::iterator>::value or
std::is_same<T, typename Json::const_iterator>::value or std::is_same<T, typename BasicJsonType::const_iterator>::value or
std::is_same<T, typename Json::reverse_iterator>::value or std::is_same<T, typename BasicJsonType::reverse_iterator>::value or
std::is_same<T, typename Json::const_reverse_iterator>::value or std::is_same<T, typename BasicJsonType::const_reverse_iterator>::value or
std::is_same<T, typename Json::json_pointer>::value; std::is_same<T, typename BasicJsonType::json_pointer>::value;
}; };
template <class Json, class CompatibleArrayType> template <class BasicJsonType, class CompatibleArrayType>
struct is_compatible_array_type struct is_compatible_array_type
{ {
// TODO concept Container? // TODO concept Container?
@ -405,10 +405,10 @@ struct is_compatible_array_type
static auto constexpr value = static auto constexpr value =
conjunction<negation<std::is_same<void, CompatibleArrayType>>, conjunction<negation<std::is_same<void, CompatibleArrayType>>,
negation<is_compatible_object_type< negation<is_compatible_object_type<
Json, CompatibleArrayType>>, BasicJsonType, CompatibleArrayType>>,
negation<std::is_constructible<typename Json::string_t, negation<std::is_constructible<typename BasicJsonType::string_t,
CompatibleArrayType>>, CompatibleArrayType>>,
negation<is_basic_json_nested_type<Json, CompatibleArrayType>>, negation<is_basic_json_nested_type<BasicJsonType, CompatibleArrayType>>,
has_value_type<CompatibleArrayType>, has_value_type<CompatibleArrayType>,
has_iterator<CompatibleArrayType>>::value; has_iterator<CompatibleArrayType>>::value;
}; };
@ -441,76 +441,76 @@ struct is_compatible_integer_type
}; };
// This trait checks if JSONSerializer<T>::from_json(json const&, udt&) exists // This trait checks if JSONSerializer<T>::from_json(json const&, udt&) exists
template <typename Json, typename T> template <typename BasicJsonType, typename T>
struct has_from_json struct has_from_json
{ {
private: private:
// also check the return type of from_json // also check the return type of from_json
template <typename U, typename = enable_if_t<std::is_same<void, decltype(uncvref_t<U>::from_json( template <typename U, typename = enable_if_t<std::is_same<void, decltype(uncvref_t<U>::from_json(
std::declval<Json>(), std::declval<T&>()))>::value>> std::declval<BasicJsonType>(), std::declval<T&>()))>::value>>
static int detect(U&&); static int detect(U&&);
static void detect(...); static void detect(...);
public: public:
static constexpr bool value = std::is_integral<decltype( static constexpr bool value = std::is_integral<decltype(
detect(std::declval<typename Json::template json_serializer<T, void>>()))>::value; detect(std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
}; };
// This trait checks if JSONSerializer<T>::from_json(json const&) exists // This trait checks if JSONSerializer<T>::from_json(json const&) exists
// this overload is used for non-default-constructible user-defined-types // this overload is used for non-default-constructible user-defined-types
template <typename Json, typename T> template <typename BasicJsonType, typename T>
struct has_non_default_from_json struct has_non_default_from_json
{ {
private: private:
template < template <
typename U, typename U,
typename = enable_if_t<std::is_same< typename = enable_if_t<std::is_same<
T, decltype(uncvref_t<U>::from_json(std::declval<Json>()))>::value >> T, decltype(uncvref_t<U>::from_json(std::declval<BasicJsonType>()))>::value >>
static int detect(U&&); static int detect(U&&);
static void detect(...); static void detect(...);
public: public:
static constexpr bool value = std::is_integral<decltype(detect( static constexpr bool value = std::is_integral<decltype(detect(
std::declval<typename Json::template json_serializer<T, void>>()))>::value; std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
}; };
// This trait checks if Json::json_serializer<T>::to_json exists // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
template <typename Json, typename T> template <typename BasicJsonType, typename T>
struct has_to_json struct has_to_json
{ {
private: private:
template <typename U, typename = decltype(uncvref_t<U>::to_json( template <typename U, typename = decltype(uncvref_t<U>::to_json(
std::declval<Json&>(), std::declval<T>()))> std::declval<BasicJsonType&>(), std::declval<T>()))>
static int detect(U&&); static int detect(U&&);
static void detect(...); static void detect(...);
public: public:
static constexpr bool value = std::is_integral<decltype(detect( static constexpr bool value = std::is_integral<decltype(detect(
std::declval<typename Json::template json_serializer<T, void>>()))>::value; std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
}; };
// overloads for basic_json template parameters // overloads for basic_json template parameters
template <typename Json, typename ArithmeticType, template <typename BasicJsonType, typename ArithmeticType,
enable_if_t<std::is_arithmetic<ArithmeticType>::value and enable_if_t<std::is_arithmetic<ArithmeticType>::value and
not std::is_same<ArithmeticType, not std::is_same<ArithmeticType,
typename Json::boolean_t>::value, typename BasicJsonType::boolean_t>::value,
int> = 0> int> = 0>
void get_arithmetic_value(const Json& j, ArithmeticType& val) void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
{ {
switch (static_cast<value_t>(j)) switch (static_cast<value_t>(j))
{ {
case value_t::number_unsigned: case value_t::number_unsigned:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_unsigned_t*>()); *j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
break; break;
case value_t::number_integer: case value_t::number_integer:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_integer_t*>()); *j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
break; break;
case value_t::number_float: case value_t::number_float:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_float_t*>()); *j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break; break;
default: default:
JSON_THROW( JSON_THROW(
@ -518,136 +518,136 @@ void get_arithmetic_value(const Json& j, ArithmeticType& val)
} }
} }
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, typename Json::boolean_t b) noexcept void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
{ {
external_constructor<value_t::boolean>::construct(j, b); external_constructor<value_t::boolean>::construct(j, b);
} }
template <typename Json, typename CompatibleString, template <typename BasicJsonType, typename CompatibleString,
enable_if_t<std::is_constructible<typename Json::string_t, enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, CompatibleString>::value,
int> = 0> int> = 0>
void to_json(Json& j, const CompatibleString& s) void to_json(BasicJsonType& j, const CompatibleString& s)
{ {
external_constructor<value_t::string>::construct(j, s); external_constructor<value_t::string>::construct(j, s);
} }
template <typename Json, typename FloatType, template <typename BasicJsonType, typename FloatType,
enable_if_t<std::is_floating_point<FloatType>::value, int> = 0> enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
void to_json(Json& j, FloatType val) noexcept void to_json(BasicJsonType& j, FloatType val) noexcept
{ {
external_constructor<value_t::number_float>::construct(j, static_cast<typename Json::number_float_t>(val)); external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
} }
template < template <
typename Json, typename CompatibleNumberUnsignedType, typename BasicJsonType, typename CompatibleNumberUnsignedType,
enable_if_t<is_compatible_integer_type<typename Json::number_unsigned_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
CompatibleNumberUnsignedType>::value, CompatibleNumberUnsignedType>::value,
int> = 0 > int> = 0 >
void to_json(Json& j, CompatibleNumberUnsignedType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
{ {
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename Json::number_unsigned_t>(val)); external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
} }
template < template <
typename Json, typename CompatibleNumberIntegerType, typename BasicJsonType, typename CompatibleNumberIntegerType,
enable_if_t<is_compatible_integer_type<typename Json::number_integer_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t,
CompatibleNumberIntegerType>::value, CompatibleNumberIntegerType>::value,
int> = 0 > int> = 0 >
void to_json(Json& j, CompatibleNumberIntegerType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
{ {
external_constructor<value_t::number_integer>::construct(j, static_cast<typename Json::number_integer_t>(val)); external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
} }
template <typename Json, typename UnscopedEnumType, template <typename BasicJsonType, typename UnscopedEnumType,
enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0> enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0>
void to_json(Json& j, UnscopedEnumType e) noexcept void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept
{ {
external_constructor<value_t::number_integer>::construct(j, e); external_constructor<value_t::number_integer>::construct(j, e);
} }
template < template <
typename Json, typename CompatibleArrayType, typename BasicJsonType, typename CompatibleArrayType,
enable_if_t < enable_if_t <
is_compatible_array_type<Json, CompatibleArrayType>::value or is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value or
std::is_same<typename Json::array_t, CompatibleArrayType>::value, std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value,
int > = 0 > int > = 0 >
void to_json(Json& j, const CompatibleArrayType& arr) void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
{ {
external_constructor<value_t::array>::construct(j, arr); external_constructor<value_t::array>::construct(j, arr);
} }
template < template <
typename Json, typename CompatibleObjectType, typename BasicJsonType, typename CompatibleObjectType,
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value, enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
int> = 0 > int> = 0 >
void to_json(Json& j, const CompatibleObjectType& arr) void to_json(BasicJsonType& j, const CompatibleObjectType& arr)
{ {
external_constructor<value_t::object>::construct(j, arr); external_constructor<value_t::object>::construct(j, arr);
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::boolean_t& b) void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
{ {
if (!j.is_boolean()) if (!j.is_boolean())
{ {
JSON_THROW(std::domain_error("type must be boolean, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be boolean, but is " + type_name(j)));
} }
b = *j.template get_ptr<const typename Json::boolean_t*>(); b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::string_t& s) void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{ {
if (!j.is_string()) if (!j.is_string())
{ {
JSON_THROW(std::domain_error("type must be string, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be string, but is " + type_name(j)));
} }
s = *j.template get_ptr<const typename Json::string_t*>(); s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::number_float_t& val) void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::number_unsigned_t& val) void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::number_integer_t& val) void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json, typename UnscopedEnumType, template <typename BasicJsonType, typename UnscopedEnumType,
enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0> enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0>
void from_json(const Json& j, UnscopedEnumType& e) void from_json(const BasicJsonType& j, UnscopedEnumType& e)
{ {
typename std::underlying_type<UnscopedEnumType>::type val = e; typename std::underlying_type<UnscopedEnumType>::type val = e;
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
e = static_cast<UnscopedEnumType>(val); e = static_cast<UnscopedEnumType>(val);
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, typename Json::array_t& arr) void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
{ {
if (!j.is_array()) if (!j.is_array())
{ {
JSON_THROW(std::domain_error("type must be array, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be array, but is " + type_name(j)));
} }
arr = *j.template get_ptr<const typename Json::array_t*>(); arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
} }
// forward_list doesn't have an insert method, TODO find a way to avoid including forward_list // forward_list doesn't have an insert method, TODO find a way to avoid including forward_list
template <typename Json, typename T, typename Allocator> template <typename BasicJsonType, typename T, typename Allocator>
void from_json(const Json& j, std::forward_list<T, Allocator>& l) void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{ {
// do not perform the check when user wants to retrieve jsons // do not perform the check when user wants to retrieve jsons
// (except when it's null.. ?) // (except when it's null.. ?)
@ -655,7 +655,7 @@ void from_json(const Json& j, std::forward_list<T, Allocator>& l)
{ {
JSON_THROW(std::domain_error("type must be array, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be array, but is " + type_name(j)));
} }
if (not std::is_same<T, Json>::value) if (not std::is_same<T, BasicJsonType>::value)
{ {
if (!j.is_array()) if (!j.is_array())
{ {
@ -668,23 +668,23 @@ void from_json(const Json& j, std::forward_list<T, Allocator>& l)
} }
} }
template <typename Json, typename CompatibleArrayType> template <typename BasicJsonType, typename CompatibleArrayType>
void from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag<0>) void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
std::transform( std::transform(
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const Json & i) j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
{ {
// get<Json>() returns *this, this won't call a from_json method when // get<BasicJsonType>() returns *this, this won't call a from_json method when
// value_type is Json // value_type is BasicJsonType
return i.template get<typename CompatibleArrayType::value_type>(); return i.template get<typename CompatibleArrayType::value_type>();
}); });
} }
template <typename Json, typename CompatibleArrayType> template <typename BasicJsonType, typename CompatibleArrayType>
auto from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag<1>) auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>)
-> decltype( -> decltype(
arr.reserve(std::declval<typename CompatibleArrayType::size_type>()), arr.reserve(std::declval<typename CompatibleArrayType::size_type>()),
void()) void())
@ -694,28 +694,28 @@ auto from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag
arr.reserve(j.size()); arr.reserve(j.size());
std::transform( std::transform(
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const Json & i) j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
{ {
// get<Json>() returns *this, this won't call a from_json method when // get<BasicJsonType>() returns *this, this won't call a from_json method when
// value_type is Json // value_type is BasicJsonType
return i.template get<typename CompatibleArrayType::value_type>(); return i.template get<typename CompatibleArrayType::value_type>();
}); });
} }
template < template <
typename Json, typename CompatibleArrayType, typename BasicJsonType, typename CompatibleArrayType,
enable_if_t<is_compatible_array_type<Json, CompatibleArrayType>::value and enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
not std::is_same<typename Json::array_t, not std::is_same<typename BasicJsonType::array_t,
CompatibleArrayType>::value, CompatibleArrayType>::value,
int> = 0 > int> = 0 >
void from_json(const Json& j, CompatibleArrayType& arr) void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{ {
if (j.is_null()) if (j.is_null())
{ {
JSON_THROW(std::domain_error("type must be array, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be array, but is " + type_name(j)));
} }
// when T == Json, do not check if value_t is correct // when T == BasicJsonType, do not check if value_t is correct
if (not std::is_same<typename CompatibleArrayType::value_type, Json>::value) if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
{ {
if (!j.is_array()) if (!j.is_array())
{ {
@ -727,17 +727,17 @@ void from_json(const Json& j, CompatibleArrayType& arr)
template < template <
typename Json, typename CompatibleObjectType, typename BasicJsonType, typename CompatibleObjectType,
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value, enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
int> = 0 > int> = 0 >
void from_json(const Json& j, CompatibleObjectType& obj) void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
{ {
if (!j.is_object()) if (!j.is_object())
{ {
JSON_THROW(std::domain_error("type must be object, but is " + type_name(j))); JSON_THROW(std::domain_error("type must be object, but is " + type_name(j)));
} }
auto inner_object = j.template get_ptr<const typename Json::object_t*>(); auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
using std::begin; using std::begin;
using std::end; using std::end;
// we could avoid the assignment, but this might require a for loop, which // we could avoid the assignment, but this might require a for loop, which
@ -750,36 +750,36 @@ void from_json(const Json& j, CompatibleObjectType& obj)
// note: Is it really necessary to provide explicit overloads for boolean_t etc.. // note: Is it really necessary to provide explicit overloads for boolean_t etc..
// in case of a custom BooleanType which is not an arithmetic type? // in case of a custom BooleanType which is not an arithmetic type?
template < template <
typename Json, typename ArithmeticType, typename BasicJsonType, typename ArithmeticType,
enable_if_t < enable_if_t <
std::is_arithmetic<ArithmeticType>::value and std::is_arithmetic<ArithmeticType>::value and
not std::is_same<ArithmeticType, not std::is_same<ArithmeticType,
typename Json::number_unsigned_t>::value and typename BasicJsonType::number_unsigned_t>::value and
not std::is_same<ArithmeticType, not std::is_same<ArithmeticType,
typename Json::number_integer_t>::value and typename BasicJsonType::number_integer_t>::value and
not std::is_same<ArithmeticType, not std::is_same<ArithmeticType,
typename Json::number_float_t>::value and typename BasicJsonType::number_float_t>::value and
not std::is_same<ArithmeticType, typename Json::boolean_t>::value, not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
int > = 0 > int > = 0 >
void from_json(const Json& j, ArithmeticType& val) void from_json(const BasicJsonType& j, ArithmeticType& val)
{ {
switch (static_cast<value_t>(j)) switch (static_cast<value_t>(j))
{ {
case value_t::number_unsigned: case value_t::number_unsigned:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_unsigned_t*>()); *j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
break; break;
case value_t::number_integer: case value_t::number_integer:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_integer_t*>()); *j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
break; break;
case value_t::number_float: case value_t::number_float:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::number_float_t*>()); *j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break; break;
case value_t::boolean: case value_t::boolean:
val = static_cast<ArithmeticType>( val = static_cast<ArithmeticType>(
*j.template get_ptr<const typename Json::boolean_t*>()); *j.template get_ptr<const typename BasicJsonType::boolean_t*>());
break; break;
default: default:
JSON_THROW( JSON_THROW(
@ -789,8 +789,8 @@ void from_json(const Json& j, ArithmeticType& val)
struct to_json_fn struct to_json_fn
{ {
template <typename Json, typename T> template <typename BasicJsonType, typename T>
auto call(Json& j, T&& val, priority_tag<1>) const auto call(BasicJsonType& j, T&& val, priority_tag<1>) const
noexcept(noexcept(to_json(j, std::forward<T>(val)))) noexcept(noexcept(to_json(j, std::forward<T>(val))))
-> decltype(to_json(j, std::forward<T>(val)), -> decltype(to_json(j, std::forward<T>(val)),
void()) void())
@ -798,15 +798,15 @@ struct to_json_fn
return to_json(j, std::forward<T>(val)); return to_json(j, std::forward<T>(val));
} }
template <typename Json, typename T> template <typename BasicJsonType, typename T>
void call(Json&, T&&, priority_tag<0>) const noexcept void call(BasicJsonType&, T&&, priority_tag<0>) const noexcept
{ {
static_assert(sizeof(Json) == 0, "to_json method in T's namespace can not be called"); static_assert(sizeof(BasicJsonType) == 0, "to_json method in T's namespace can not be called");
} }
public: public:
template <typename Json, typename T> template <typename BasicJsonType, typename T>
void operator()(Json& j, T&& val) const void operator()(BasicJsonType& j, T&& val) const
noexcept(noexcept(std::declval<to_json_fn>().call(j, std::forward<T>(val), priority_tag<1> {}))) noexcept(noexcept(std::declval<to_json_fn>().call(j, std::forward<T>(val), priority_tag<1> {})))
{ {
return call(j, std::forward<T>(val), priority_tag<1> {}); return call(j, std::forward<T>(val), priority_tag<1> {});
@ -816,23 +816,23 @@ struct to_json_fn
struct from_json_fn struct from_json_fn
{ {
private: private:
template <typename Json, typename T> template <typename BasicJsonType, typename T>
auto call(const Json& j, T& val, priority_tag<1>) const auto call(const BasicJsonType& j, T& val, priority_tag<1>) const
noexcept(noexcept(from_json(j, val))) noexcept(noexcept(from_json(j, val)))
-> decltype(from_json(j, val), void()) -> decltype(from_json(j, val), void())
{ {
return from_json(j, val); return from_json(j, val);
} }
template <typename Json, typename T> template <typename BasicJsonType, typename T>
void call(const Json&, T&, priority_tag<0>) const noexcept void call(const BasicJsonType&, T&, priority_tag<0>) const noexcept
{ {
static_assert(sizeof(Json) == 0, "from_json method in T's namespace can not be called"); static_assert(sizeof(BasicJsonType) == 0, "from_json method in T's namespace can not be called");
} }
public: public:
template <typename Json, typename T> template <typename BasicJsonType, typename T>
void operator()(const Json& j, T& val) const void operator()(const BasicJsonType& j, T& val) const
noexcept(noexcept(std::declval<from_json_fn>().call(j, val, priority_tag<1> {}))) noexcept(noexcept(std::declval<from_json_fn>().call(j, val, priority_tag<1> {})))
{ {
return call(j, val, priority_tag<1> {}); return call(j, val, priority_tag<1> {});
@ -881,14 +881,14 @@ constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::va
template <typename = void, typename = void> template <typename = void, typename = void>
struct adl_serializer struct adl_serializer
{ {
template <typename Json, typename T> template <typename BasicJsonType, typename T>
static void from_json(Json&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward<Json>(j), val))) static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
{ {
::nlohmann::from_json(std::forward<Json>(j), val); ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
} }
template <typename Json, typename T> template <typename BasicJsonType, typename T>
static void to_json(Json& j, T&& val) noexcept( static void to_json(BasicJsonType& j, T&& val) noexcept(
noexcept(::nlohmann::to_json(j, std::forward<T>(val)))) noexcept(::nlohmann::to_json(j, std::forward<T>(val))))
{ {
::nlohmann::to_json(j, std::forward<T>(val)); ::nlohmann::to_json(j, std::forward<T>(val));
@ -989,7 +989,7 @@ class basic_json
{ {
private: private:
template <::nlohmann::value_t> friend struct detail::external_constructor; template <::nlohmann::value_t> friend struct detail::external_constructor;
template <typename Json> friend std::string detail::type_name(const Json&); template <typename BasicJsonType> friend std::string detail::type_name(const BasicJsonType&);
/// workaround type for MSVC /// workaround type for MSVC
using basic_json_t = basic_json<ObjectType, ArrayType, StringType, using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType,

View file

@ -84,20 +84,20 @@ struct contact_book
namespace udt namespace udt
{ {
// templates because of the custom_json tests (see below) // templates because of the custom_json tests (see below)
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, age a) void to_json(BasicJsonType& j, age a)
{ {
j = a.m_val; j = a.m_val;
} }
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, const name& n) void to_json(BasicJsonType& j, const name& n)
{ {
j = n.m_val; j = n.m_val;
} }
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, country c) void to_json(BasicJsonType& j, country c)
{ {
switch (c) switch (c)
{ {
@ -113,10 +113,10 @@ void to_json(Json& j, country c)
} }
} }
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, const person& p) void to_json(BasicJsonType& j, const person& p)
{ {
j = Json{{"age", p.m_age}, {"name", p.m_name}, {"country", p.m_country}}; j = BasicJsonType{{"age", p.m_age}, {"name", p.m_name}, {"country", p.m_country}};
} }
void to_json(nlohmann::json& j, const address& a) void to_json(nlohmann::json& j, const address& a)
@ -171,20 +171,20 @@ bool operator==(const contact_book& lhs, const contact_book& rhs)
// from_json methods // from_json methods
namespace udt namespace udt
{ {
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, age& a) void from_json(const BasicJsonType& j, age& a)
{ {
a.m_val = j.template get<int>(); a.m_val = j.template get<int>();
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, name& n) void from_json(const BasicJsonType& j, name& n)
{ {
n.m_val = j.template get<std::string>(); n.m_val = j.template get<std::string>();
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, country& c) void from_json(const BasicJsonType& j, country& c)
{ {
const auto str = j.template get<std::string>(); const auto str = j.template get<std::string>();
static const std::map<std::string, country> m = static const std::map<std::string, country> m =
@ -199,8 +199,8 @@ void from_json(const Json& j, country& c)
c = it->second; c = it->second;
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, person& p) void from_json(const BasicJsonType& j, person& p)
{ {
p.m_age = j["age"].template get<age>(); p.m_age = j["age"].template get<age>();
p.m_name = j["name"].template get<name>(); p.m_name = j["name"].template get<name>();
@ -493,20 +493,20 @@ struct pod_serializer
{ {
// use adl for non-pods, or scalar types // use adl for non-pods, or scalar types
template < template <
typename Json, typename U = T, typename BasicJsonType, typename U = T,
typename std::enable_if < typename std::enable_if <
not(std::is_pod<U>::value and std::is_class<U>::value), int >::type = 0 > not(std::is_pod<U>::value and std::is_class<U>::value), int >::type = 0 >
static void from_json(const Json& j, U& t) static void from_json(const BasicJsonType& j, U& t)
{ {
using nlohmann::from_json; using nlohmann::from_json;
from_json(j, t); from_json(j, t);
} }
// special behaviour for pods // special behaviour for pods
template <typename Json, typename U = T, template <typename BasicJsonType, typename U = T,
typename std::enable_if< typename std::enable_if<
std::is_pod<U>::value and std::is_class<U>::value, int>::type = 0> std::is_pod<U>::value and std::is_class<U>::value, int>::type = 0>
static void from_json(const Json& j, U& t) static void from_json(const BasicJsonType& j, U& t)
{ {
std::uint64_t value; std::uint64_t value;
// TODO The following block is no longer relevant in this serializer, make another one that shows the issue // TODO The following block is no longer relevant in this serializer, make another one that shows the issue
@ -529,19 +529,19 @@ struct pod_serializer
} }
template < template <
typename Json, typename U = T, typename BasicJsonType, typename U = T,
typename std::enable_if < typename std::enable_if <
not(std::is_pod<U>::value and std::is_class<U>::value), int >::type = 0 > not(std::is_pod<U>::value and std::is_class<U>::value), int >::type = 0 >
static void to_json(Json& j, const T& t) static void to_json(BasicJsonType& j, const T& t)
{ {
using nlohmann::to_json; using nlohmann::to_json;
to_json(j, t); to_json(j, t);
} }
template <typename Json, typename U = T, template <typename BasicJsonType, typename U = T,
typename std::enable_if< typename std::enable_if<
std::is_pod<U>::value and std::is_class<U>::value, int>::type = 0> std::is_pod<U>::value and std::is_class<U>::value, int>::type = 0>
static void to_json(Json& j, const T& t) noexcept static void to_json(BasicJsonType& j, const T& t) noexcept
{ {
auto bytes = static_cast< const unsigned char*>(static_cast<const void*>(&t)); auto bytes = static_cast< const unsigned char*>(static_cast<const void*>(&t));
std::uint64_t value = bytes[0]; std::uint64_t value = bytes[0];
@ -565,14 +565,14 @@ struct non_pod
std::string s; std::string s;
}; };
template <typename Json> template <typename BasicJsonType>
void to_json(Json& j, const non_pod& np) void to_json(BasicJsonType& j, const non_pod& np)
{ {
j = np.s; j = np.s;
} }
template <typename Json> template <typename BasicJsonType>
void from_json(const Json& j, non_pod& np) void from_json(const BasicJsonType& j, non_pod& np)
{ {
np.s = j.template get<std::string>(); np.s = j.template get<std::string>();
} }