rename template argument Json -> BasicJsonType
This commit is contained in:
parent
708eb9613b
commit
7f359017d2
3 changed files with 1183 additions and 705 deletions
1536
src/json.hpp
1536
src/json.hpp
File diff suppressed because it is too large
Load diff
|
@ -187,7 +187,7 @@ struct conjunction<B1, Bn...>
|
|||
|
||||
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)
|
||||
{
|
||||
|
@ -220,8 +220,8 @@ template <value_t> struct external_constructor;
|
|||
template <>
|
||||
struct external_constructor<value_t::boolean>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, typename Json::boolean_t b) noexcept
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
||||
{
|
||||
j.m_type = value_t::boolean;
|
||||
j.m_value = b;
|
||||
|
@ -232,8 +232,8 @@ struct external_constructor<value_t::boolean>
|
|||
template <>
|
||||
struct external_constructor<value_t::string>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, const typename Json::string_t& s)
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
||||
{
|
||||
j.m_type = value_t::string;
|
||||
j.m_value = s;
|
||||
|
@ -244,12 +244,12 @@ struct external_constructor<value_t::string>
|
|||
template <>
|
||||
struct external_constructor<value_t::number_float>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, typename Json::number_float_t val) noexcept
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
|
||||
{
|
||||
// replace infinity and NAN by null
|
||||
if (not std::isfinite(val))
|
||||
j = Json{};
|
||||
j = BasicJsonType{};
|
||||
else
|
||||
{
|
||||
j.m_type = value_t::number_float;
|
||||
|
@ -262,8 +262,8 @@ struct external_constructor<value_t::number_float>
|
|||
template <>
|
||||
struct external_constructor<value_t::number_unsigned>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, typename Json::number_unsigned_t val) noexcept
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
|
||||
{
|
||||
j.m_type = value_t::number_unsigned;
|
||||
j.m_value = val;
|
||||
|
@ -274,8 +274,8 @@ struct external_constructor<value_t::number_unsigned>
|
|||
template <>
|
||||
struct external_constructor<value_t::number_integer>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, typename Json::number_integer_t val) noexcept
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
|
||||
{
|
||||
j.m_type = value_t::number_integer;
|
||||
j.m_value = val;
|
||||
|
@ -286,25 +286,25 @@ struct external_constructor<value_t::number_integer>
|
|||
template <>
|
||||
struct external_constructor<value_t::array>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, const typename Json::array_t& arr)
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
||||
{
|
||||
j.m_type = value_t::array;
|
||||
j.m_value = arr;
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
template <typename Json, typename CompatibleArrayType,
|
||||
template <typename BasicJsonType, typename CompatibleArrayType,
|
||||
enable_if_t<not std::is_same<CompatibleArrayType,
|
||||
typename Json::array_t>::value,
|
||||
typename BasicJsonType::array_t>::value,
|
||||
int> = 0>
|
||||
static void construct(Json& j, const CompatibleArrayType& arr)
|
||||
static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
j.m_type = value_t::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();
|
||||
}
|
||||
};
|
||||
|
@ -312,26 +312,26 @@ struct external_constructor<value_t::array>
|
|||
template <>
|
||||
struct external_constructor<value_t::object>
|
||||
{
|
||||
template <typename Json>
|
||||
static void construct(Json& j, const typename Json::object_t& obj)
|
||||
template <typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
||||
{
|
||||
j.m_type = value_t::object;
|
||||
j.m_value = obj;
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
template <typename Json, typename CompatibleObjectType,
|
||||
template <typename BasicJsonType, typename CompatibleObjectType,
|
||||
enable_if_t<not std::is_same<CompatibleObjectType,
|
||||
typename Json::object_t>::value,
|
||||
typename BasicJsonType::object_t>::value,
|
||||
int> = 0>
|
||||
static void construct(Json& j, const CompatibleObjectType& obj)
|
||||
static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
|
||||
j.m_type = value_t::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();
|
||||
}
|
||||
};
|
||||
|
@ -377,27 +377,27 @@ struct is_compatible_object_type_impl<true, RealType, CompatibleObjectType>
|
|||
typename CompatibleObjectType::mapped_type>::value;
|
||||
};
|
||||
|
||||
template<class Json, class CompatibleObjectType>
|
||||
template<class BasicJsonType, class CompatibleObjectType>
|
||||
struct is_compatible_object_type
|
||||
{
|
||||
static auto constexpr value = is_compatible_object_type_impl <
|
||||
conjunction<negation<std::is_same<void, CompatibleObjectType>>,
|
||||
has_mapped_type<CompatibleObjectType>,
|
||||
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
|
||||
{
|
||||
static auto constexpr value = std::is_same<T, typename Json::iterator>::value or
|
||||
std::is_same<T, typename Json::const_iterator>::value or
|
||||
std::is_same<T, typename Json::reverse_iterator>::value or
|
||||
std::is_same<T, typename Json::const_reverse_iterator>::value or
|
||||
std::is_same<T, typename Json::json_pointer>::value;
|
||||
static auto constexpr value = std::is_same<T, typename BasicJsonType::iterator>::value or
|
||||
std::is_same<T, typename BasicJsonType::const_iterator>::value or
|
||||
std::is_same<T, typename BasicJsonType::reverse_iterator>::value or
|
||||
std::is_same<T, typename BasicJsonType::const_reverse_iterator>::value or
|
||||
std::is_same<T, typename BasicJsonType::json_pointer>::value;
|
||||
};
|
||||
|
||||
template <class Json, class CompatibleArrayType>
|
||||
template <class BasicJsonType, class CompatibleArrayType>
|
||||
struct is_compatible_array_type
|
||||
{
|
||||
// TODO concept Container?
|
||||
|
@ -405,10 +405,10 @@ struct is_compatible_array_type
|
|||
static auto constexpr value =
|
||||
conjunction<negation<std::is_same<void, CompatibleArrayType>>,
|
||||
negation<is_compatible_object_type<
|
||||
Json, CompatibleArrayType>>,
|
||||
negation<std::is_constructible<typename Json::string_t,
|
||||
BasicJsonType, CompatibleArrayType>>,
|
||||
negation<std::is_constructible<typename BasicJsonType::string_t,
|
||||
CompatibleArrayType>>,
|
||||
negation<is_basic_json_nested_type<Json, CompatibleArrayType>>,
|
||||
negation<is_basic_json_nested_type<BasicJsonType, CompatibleArrayType>>,
|
||||
has_value_type<CompatibleArrayType>,
|
||||
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
|
||||
template <typename Json, typename T>
|
||||
template <typename BasicJsonType, typename T>
|
||||
struct has_from_json
|
||||
{
|
||||
private:
|
||||
// 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(
|
||||
std::declval<Json>(), std::declval<T&>()))>::value>>
|
||||
std::declval<BasicJsonType>(), std::declval<T&>()))>::value>>
|
||||
static int detect(U&&);
|
||||
static void detect(...);
|
||||
|
||||
public:
|
||||
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 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
|
||||
{
|
||||
private:
|
||||
template <
|
||||
typename U,
|
||||
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 void detect(...);
|
||||
|
||||
public:
|
||||
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
|
||||
template <typename Json, typename T>
|
||||
// This trait checks if BasicJsonType::json_serializer<T>::to_json exists
|
||||
template <typename BasicJsonType, typename T>
|
||||
struct has_to_json
|
||||
{
|
||||
private:
|
||||
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 void detect(...);
|
||||
|
||||
public:
|
||||
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
|
||||
|
||||
template <typename Json, typename ArithmeticType,
|
||||
template <typename BasicJsonType, typename ArithmeticType,
|
||||
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
|
||||
not std::is_same<ArithmeticType,
|
||||
typename Json::boolean_t>::value,
|
||||
typename BasicJsonType::boolean_t>::value,
|
||||
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))
|
||||
{
|
||||
case value_t::number_unsigned:
|
||||
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;
|
||||
case value_t::number_integer:
|
||||
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;
|
||||
case value_t::number_float:
|
||||
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;
|
||||
default:
|
||||
JSON_THROW(
|
||||
|
@ -518,136 +518,136 @@ void get_arithmetic_value(const Json& j, ArithmeticType& val)
|
|||
}
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void to_json(Json& j, typename Json::boolean_t b) noexcept
|
||||
template <typename BasicJsonType>
|
||||
void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
||||
{
|
||||
external_constructor<value_t::boolean>::construct(j, b);
|
||||
}
|
||||
|
||||
template <typename Json, typename CompatibleString,
|
||||
enable_if_t<std::is_constructible<typename Json::string_t,
|
||||
template <typename BasicJsonType, typename CompatibleString,
|
||||
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
|
||||
CompatibleString>::value,
|
||||
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);
|
||||
}
|
||||
|
||||
template <typename Json, typename FloatType,
|
||||
template <typename BasicJsonType, typename FloatType,
|
||||
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 <
|
||||
typename Json, typename CompatibleNumberUnsignedType,
|
||||
enable_if_t<is_compatible_integer_type<typename Json::number_unsigned_t,
|
||||
typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
||||
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
|
||||
CompatibleNumberUnsignedType>::value,
|
||||
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 <
|
||||
typename Json, typename CompatibleNumberIntegerType,
|
||||
enable_if_t<is_compatible_integer_type<typename Json::number_integer_t,
|
||||
typename BasicJsonType, typename CompatibleNumberIntegerType,
|
||||
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t,
|
||||
CompatibleNumberIntegerType>::value,
|
||||
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>
|
||||
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);
|
||||
}
|
||||
|
||||
template <
|
||||
typename Json, typename CompatibleArrayType,
|
||||
typename BasicJsonType, typename CompatibleArrayType,
|
||||
enable_if_t <
|
||||
is_compatible_array_type<Json, CompatibleArrayType>::value or
|
||||
std::is_same<typename Json::array_t, CompatibleArrayType>::value,
|
||||
is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value or
|
||||
std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value,
|
||||
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);
|
||||
}
|
||||
|
||||
template <
|
||||
typename Json, typename CompatibleObjectType,
|
||||
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value,
|
||||
typename BasicJsonType, typename CompatibleObjectType,
|
||||
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
|
||||
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);
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, typename Json::boolean_t& b)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
||||
{
|
||||
if (!j.is_boolean())
|
||||
{
|
||||
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>
|
||||
void from_json(const Json& j, typename Json::string_t& s)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
|
||||
{
|
||||
if (!j.is_string())
|
||||
{
|
||||
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>
|
||||
void from_json(const Json& j, typename Json::number_float_t& val)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
|
||||
{
|
||||
get_arithmetic_value(j, val);
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, typename Json::number_unsigned_t& val)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
|
||||
{
|
||||
get_arithmetic_value(j, val);
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, typename Json::number_integer_t& val)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& 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>
|
||||
void from_json(const Json& j, UnscopedEnumType& e)
|
||||
void from_json(const BasicJsonType& j, UnscopedEnumType& e)
|
||||
{
|
||||
typename std::underlying_type<UnscopedEnumType>::type val = e;
|
||||
get_arithmetic_value(j, val);
|
||||
e = static_cast<UnscopedEnumType>(val);
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, typename Json::array_t& arr)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
|
||||
{
|
||||
if (!j.is_array())
|
||||
{
|
||||
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
|
||||
template <typename Json, typename T, typename Allocator>
|
||||
void from_json(const Json& j, std::forward_list<T, Allocator>& l)
|
||||
template <typename BasicJsonType, typename T, typename Allocator>
|
||||
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
||||
{
|
||||
// do not perform the check when user wants to retrieve jsons
|
||||
// (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)));
|
||||
}
|
||||
if (not std::is_same<T, Json>::value)
|
||||
if (not std::is_same<T, BasicJsonType>::value)
|
||||
{
|
||||
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>
|
||||
void from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag<0>)
|
||||
template <typename BasicJsonType, typename CompatibleArrayType>
|
||||
void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>)
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
|
||||
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
|
||||
// value_type is Json
|
||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
||||
// value_type is BasicJsonType
|
||||
return i.template get<typename CompatibleArrayType::value_type>();
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Json, typename CompatibleArrayType>
|
||||
auto from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag<1>)
|
||||
template <typename BasicJsonType, typename CompatibleArrayType>
|
||||
auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>)
|
||||
-> decltype(
|
||||
arr.reserve(std::declval<typename CompatibleArrayType::size_type>()),
|
||||
void())
|
||||
|
@ -694,28 +694,28 @@ auto from_json_array_impl(const Json& j, CompatibleArrayType& arr, priority_tag
|
|||
|
||||
arr.reserve(j.size());
|
||||
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
|
||||
// value_type is Json
|
||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
||||
// value_type is BasicJsonType
|
||||
return i.template get<typename CompatibleArrayType::value_type>();
|
||||
});
|
||||
}
|
||||
|
||||
template <
|
||||
typename Json, typename CompatibleArrayType,
|
||||
enable_if_t<is_compatible_array_type<Json, CompatibleArrayType>::value and
|
||||
not std::is_same<typename Json::array_t,
|
||||
typename BasicJsonType, typename CompatibleArrayType,
|
||||
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
|
||||
not std::is_same<typename BasicJsonType::array_t,
|
||||
CompatibleArrayType>::value,
|
||||
int> = 0 >
|
||||
void from_json(const Json& j, CompatibleArrayType& arr)
|
||||
void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
|
||||
{
|
||||
if (j.is_null())
|
||||
{
|
||||
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
|
||||
if (not std::is_same<typename CompatibleArrayType::value_type, Json>::value)
|
||||
// when T == BasicJsonType, do not check if value_t is correct
|
||||
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
|
||||
{
|
||||
if (!j.is_array())
|
||||
{
|
||||
|
@ -727,17 +727,17 @@ void from_json(const Json& j, CompatibleArrayType& arr)
|
|||
|
||||
|
||||
template <
|
||||
typename Json, typename CompatibleObjectType,
|
||||
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value,
|
||||
typename BasicJsonType, typename CompatibleObjectType,
|
||||
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
|
||||
int> = 0 >
|
||||
void from_json(const Json& j, CompatibleObjectType& obj)
|
||||
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
|
||||
{
|
||||
if (!j.is_object())
|
||||
{
|
||||
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::end;
|
||||
// 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..
|
||||
// in case of a custom BooleanType which is not an arithmetic type?
|
||||
template <
|
||||
typename Json, typename ArithmeticType,
|
||||
typename BasicJsonType, typename ArithmeticType,
|
||||
enable_if_t <
|
||||
std::is_arithmetic<ArithmeticType>::value and
|
||||
not std::is_same<ArithmeticType,
|
||||
typename Json::number_unsigned_t>::value and
|
||||
typename BasicJsonType::number_unsigned_t>::value and
|
||||
not std::is_same<ArithmeticType,
|
||||
typename Json::number_integer_t>::value and
|
||||
typename BasicJsonType::number_integer_t>::value and
|
||||
not std::is_same<ArithmeticType,
|
||||
typename Json::number_float_t>::value and
|
||||
not std::is_same<ArithmeticType, typename Json::boolean_t>::value,
|
||||
typename BasicJsonType::number_float_t>::value and
|
||||
not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
||||
int > = 0 >
|
||||
void from_json(const Json& j, ArithmeticType& val)
|
||||
void from_json(const BasicJsonType& j, ArithmeticType& val)
|
||||
{
|
||||
switch (static_cast<value_t>(j))
|
||||
{
|
||||
case value_t::number_unsigned:
|
||||
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;
|
||||
case value_t::number_integer:
|
||||
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;
|
||||
case value_t::number_float:
|
||||
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;
|
||||
case value_t::boolean:
|
||||
val = static_cast<ArithmeticType>(
|
||||
*j.template get_ptr<const typename Json::boolean_t*>());
|
||||
*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
|
||||
break;
|
||||
default:
|
||||
JSON_THROW(
|
||||
|
@ -789,8 +789,8 @@ void from_json(const Json& j, ArithmeticType& val)
|
|||
|
||||
struct to_json_fn
|
||||
{
|
||||
template <typename Json, typename T>
|
||||
auto call(Json& j, T&& val, priority_tag<1>) const
|
||||
template <typename BasicJsonType, typename T>
|
||||
auto call(BasicJsonType& j, T&& val, priority_tag<1>) const
|
||||
noexcept(noexcept(to_json(j, std::forward<T>(val))))
|
||||
-> decltype(to_json(j, std::forward<T>(val)),
|
||||
void())
|
||||
|
@ -798,15 +798,15 @@ struct to_json_fn
|
|||
return to_json(j, std::forward<T>(val));
|
||||
}
|
||||
|
||||
template <typename Json, typename T>
|
||||
void call(Json&, T&&, priority_tag<0>) const noexcept
|
||||
template <typename BasicJsonType, typename T>
|
||||
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:
|
||||
template <typename Json, typename T>
|
||||
void operator()(Json& j, T&& val) const
|
||||
template <typename BasicJsonType, typename T>
|
||||
void operator()(BasicJsonType& j, T&& val) const
|
||||
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> {});
|
||||
|
@ -816,23 +816,23 @@ struct to_json_fn
|
|||
struct from_json_fn
|
||||
{
|
||||
private:
|
||||
template <typename Json, typename T>
|
||||
auto call(const Json& j, T& val, priority_tag<1>) const
|
||||
template <typename BasicJsonType, typename T>
|
||||
auto call(const BasicJsonType& j, T& val, priority_tag<1>) const
|
||||
noexcept(noexcept(from_json(j, val)))
|
||||
-> decltype(from_json(j, val), void())
|
||||
{
|
||||
return from_json(j, val);
|
||||
}
|
||||
|
||||
template <typename Json, typename T>
|
||||
void call(const Json&, T&, priority_tag<0>) const noexcept
|
||||
template <typename BasicJsonType, typename T>
|
||||
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:
|
||||
template <typename Json, typename T>
|
||||
void operator()(const Json& j, T& val) const
|
||||
template <typename BasicJsonType, typename T>
|
||||
void operator()(const BasicJsonType& j, T& val) const
|
||||
noexcept(noexcept(std::declval<from_json_fn>().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>
|
||||
struct adl_serializer
|
||||
{
|
||||
template <typename Json, typename T>
|
||||
static void from_json(Json&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward<Json>(j), val)))
|
||||
template <typename BasicJsonType, typename T>
|
||||
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>
|
||||
static void to_json(Json& j, T&& val) noexcept(
|
||||
template <typename BasicJsonType, typename T>
|
||||
static void to_json(BasicJsonType& j, T&& val) noexcept(
|
||||
noexcept(::nlohmann::to_json(j, std::forward<T>(val))))
|
||||
{
|
||||
::nlohmann::to_json(j, std::forward<T>(val));
|
||||
|
@ -989,7 +989,7 @@ class basic_json
|
|||
{
|
||||
private:
|
||||
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
|
||||
using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
|
||||
BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||
|
|
|
@ -84,20 +84,20 @@ struct contact_book
|
|||
namespace udt
|
||||
{
|
||||
// templates because of the custom_json tests (see below)
|
||||
template <typename Json>
|
||||
void to_json(Json& j, age a)
|
||||
template <typename BasicJsonType>
|
||||
void to_json(BasicJsonType& j, age a)
|
||||
{
|
||||
j = a.m_val;
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void to_json(Json& j, const name& n)
|
||||
template <typename BasicJsonType>
|
||||
void to_json(BasicJsonType& j, const name& n)
|
||||
{
|
||||
j = n.m_val;
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void to_json(Json& j, country c)
|
||||
template <typename BasicJsonType>
|
||||
void to_json(BasicJsonType& j, country c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
|
@ -113,10 +113,10 @@ void to_json(Json& j, country c)
|
|||
}
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void to_json(Json& j, const person& p)
|
||||
template <typename BasicJsonType>
|
||||
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)
|
||||
|
@ -171,20 +171,20 @@ bool operator==(const contact_book& lhs, const contact_book& rhs)
|
|||
// from_json methods
|
||||
namespace udt
|
||||
{
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, age& a)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, age& a)
|
||||
{
|
||||
a.m_val = j.template get<int>();
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, name& n)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, name& n)
|
||||
{
|
||||
n.m_val = j.template get<std::string>();
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, country& c)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, country& c)
|
||||
{
|
||||
const auto str = j.template get<std::string>();
|
||||
static const std::map<std::string, country> m =
|
||||
|
@ -199,8 +199,8 @@ void from_json(const Json& j, country& c)
|
|||
c = it->second;
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, person& p)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, person& p)
|
||||
{
|
||||
p.m_age = j["age"].template get<age>();
|
||||
p.m_name = j["name"].template get<name>();
|
||||
|
@ -493,20 +493,20 @@ struct pod_serializer
|
|||
{
|
||||
// use adl for non-pods, or scalar types
|
||||
template <
|
||||
typename Json, typename U = T,
|
||||
typename BasicJsonType, typename U = T,
|
||||
typename std::enable_if <
|
||||
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;
|
||||
from_json(j, t);
|
||||
}
|
||||
|
||||
// special behaviour for pods
|
||||
template <typename Json, typename U = T,
|
||||
template <typename BasicJsonType, typename U = T,
|
||||
typename std::enable_if<
|
||||
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;
|
||||
// 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 <
|
||||
typename Json, typename U = T,
|
||||
typename BasicJsonType, typename U = T,
|
||||
typename std::enable_if <
|
||||
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;
|
||||
to_json(j, t);
|
||||
}
|
||||
|
||||
template <typename Json, typename U = T,
|
||||
template <typename BasicJsonType, typename U = T,
|
||||
typename std::enable_if<
|
||||
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));
|
||||
std::uint64_t value = bytes[0];
|
||||
|
@ -565,14 +565,14 @@ struct non_pod
|
|||
std::string s;
|
||||
};
|
||||
|
||||
template <typename Json>
|
||||
void to_json(Json& j, const non_pod& np)
|
||||
template <typename BasicJsonType>
|
||||
void to_json(BasicJsonType& j, const non_pod& np)
|
||||
{
|
||||
j = np.s;
|
||||
}
|
||||
|
||||
template <typename Json>
|
||||
void from_json(const Json& j, non_pod& np)
|
||||
template <typename BasicJsonType>
|
||||
void from_json(const BasicJsonType& j, non_pod& np)
|
||||
{
|
||||
np.s = j.template get<std::string>();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue