run make pretty

This commit is contained in:
Théo DELRIEU 2017-01-17 10:32:35 +01:00
parent fbac056c38
commit 3e15b551e0
6 changed files with 6274 additions and 5716 deletions

File diff suppressed because it is too large Load diff

View file

@ -168,7 +168,7 @@ using is_unscoped_enum =
namespace detail namespace detail
{ {
template <typename Json> std::string type_name(const Json &j) template <typename Json> std::string type_name(const Json& j)
{ {
switch (j.m_type) switch (j.m_type)
{ {
@ -190,7 +190,7 @@ template <typename Json> std::string type_name(const Json &j)
} }
// dispatch utility (taken from ranges-v3) // dispatch utility (taken from ranges-v3)
template <unsigned N> struct priority_tag : priority_tag<N - 1> {}; template <unsigned N> struct priority_tag : priority_tag < N - 1 > {};
template <> struct priority_tag<0> {}; template <> struct priority_tag<0> {};
@ -202,7 +202,7 @@ template <>
struct external_constructor<value_t::boolean> struct external_constructor<value_t::boolean>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, typename Json::boolean_t b) noexcept static void construct(Json& j, typename Json::boolean_t b) noexcept
{ {
j.m_type = value_t::boolean; j.m_type = value_t::boolean;
j.m_value = b; j.m_value = b;
@ -214,7 +214,7 @@ template <>
struct external_constructor<value_t::string> struct external_constructor<value_t::string>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, const typename Json::string_t& s) static void construct(Json& j, const typename Json::string_t& s)
{ {
j.m_type = value_t::string; j.m_type = value_t::string;
j.m_value = s; j.m_value = s;
@ -226,7 +226,7 @@ template <>
struct external_constructor<value_t::number_float> struct external_constructor<value_t::number_float>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, typename Json::number_float_t val) noexcept static void construct(Json& j, typename Json::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))
@ -244,7 +244,7 @@ template <>
struct external_constructor<value_t::number_unsigned> struct external_constructor<value_t::number_unsigned>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, typename Json::number_unsigned_t val) noexcept static void construct(Json& j, typename Json::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;
@ -256,7 +256,7 @@ template <>
struct external_constructor<value_t::number_integer> struct external_constructor<value_t::number_integer>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, typename Json::number_integer_t val) noexcept static void construct(Json& j, typename Json::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;
@ -268,7 +268,7 @@ template <>
struct external_constructor<value_t::array> struct external_constructor<value_t::array>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, const typename Json::array_t& arr) static void construct(Json& j, const typename Json::array_t& arr)
{ {
j.m_type = value_t::array; j.m_type = value_t::array;
j.m_value = arr; j.m_value = arr;
@ -279,7 +279,7 @@ struct external_constructor<value_t::array>
enable_if_t<not std::is_same<CompatibleArrayType, enable_if_t<not std::is_same<CompatibleArrayType,
typename Json::array_t>::value, typename Json::array_t>::value,
int> = 0> int> = 0>
static void construct(Json &j, const CompatibleArrayType &arr) static void construct(Json& j, const CompatibleArrayType& arr)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
@ -294,7 +294,7 @@ template <>
struct external_constructor<value_t::object> struct external_constructor<value_t::object>
{ {
template <typename Json> template <typename Json>
static void construct(Json &j, const typename Json::object_t& obj) static void construct(Json& j, const typename Json::object_t& obj)
{ {
j.m_type = value_t::object; j.m_type = value_t::object;
j.m_value = obj; j.m_value = obj;
@ -305,7 +305,7 @@ struct external_constructor<value_t::object>
enable_if_t<not std::is_same<CompatibleObjectType, enable_if_t<not std::is_same<CompatibleObjectType,
typename Json::object_t>::value, typename Json::object_t>::value,
int> = 0> int> = 0>
static void construct(Json &j, const CompatibleObjectType &obj) static void construct(Json& j, const CompatibleObjectType& obj)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
@ -380,11 +380,11 @@ struct is_compatible_object_type_impl<true, RealType, CompatibleObjectType>
template<class BasicJson, class CompatibleObjectType> template<class BasicJson, 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 BasicJson::object_t, CompatibleObjectType>::value; typename BasicJson::object_t, CompatibleObjectType >::value;
}; };
template <typename BasicJson, typename T> template <typename BasicJson, typename T>
@ -461,15 +461,15 @@ struct has_from_json
template <typename Json, typename T> template <typename Json, 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<Json>()))>::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 Json::template json_serializer<T, void>>()))>::value;
}; };
@ -478,13 +478,13 @@ public:
template <typename Json, typename T> template <typename Json, 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<Json&>(), 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 Json::template json_serializer<T, void>>()))>::value;
}; };
@ -496,21 +496,29 @@ template <typename Json, typename ArithmeticType,
not std::is_same<ArithmeticType, not std::is_same<ArithmeticType,
typename Json::boolean_t>::value, typename Json::boolean_t>::value,
int> = 0> int> = 0>
void get_arithmetic_value(const Json &j, ArithmeticType &val) void get_arithmetic_value(const Json& j, ArithmeticType& val)
{ {
// unsigned must be checked first, since is_number_integer() == true for unsigned // unsigned must be checked first, since is_number_integer() == true for unsigned
if (j.is_number_unsigned()) if (j.is_number_unsigned())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>());
}
else if (j.is_number_integer()) else if (j.is_number_integer())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>());
}
else if (j.is_number_float()) else if (j.is_number_float())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>());
}
else else
{
throw std::domain_error("type must be number, but is " + type_name(j)); throw std::domain_error("type must be number, but is " + type_name(j));
}
} }
template <typename Json> template <typename Json>
void to_json(Json &j, typename Json::boolean_t b) noexcept void to_json(Json& j, typename Json::boolean_t b) noexcept
{ {
external_constructor<value_t::boolean>::construct(j, b); external_constructor<value_t::boolean>::construct(j, b);
} }
@ -519,14 +527,14 @@ template <typename Json, typename CompatibleString,
enable_if_t<std::is_constructible<typename Json::string_t, enable_if_t<std::is_constructible<typename Json::string_t,
CompatibleString>::value, CompatibleString>::value,
int> = 0> int> = 0>
void to_json(Json &j, const CompatibleString &s) void to_json(Json& 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 Json, 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(Json& 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 Json::number_float_t>(val));
} }
@ -536,8 +544,8 @@ template <
typename Json, typename CompatibleNumberUnsignedType, typename Json, typename CompatibleNumberUnsignedType,
enable_if_t<is_compatible_integer_type<typename Json::number_unsigned_t, enable_if_t<is_compatible_integer_type<typename Json::number_unsigned_t,
CompatibleNumberUnsignedType>::value, CompatibleNumberUnsignedType>::value,
int> = 0> int> = 0 >
void to_json(Json &j, CompatibleNumberUnsignedType val) noexcept void to_json(Json& 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 Json::number_unsigned_t>(val));
} }
@ -546,26 +554,26 @@ template <
typename Json, typename CompatibleNumberIntegerType, typename Json, typename CompatibleNumberIntegerType,
enable_if_t<is_compatible_integer_type<typename Json::number_integer_t, enable_if_t<is_compatible_integer_type<typename Json::number_integer_t,
CompatibleNumberIntegerType>::value, CompatibleNumberIntegerType>::value,
int> = 0> int> = 0 >
void to_json(Json &j, CompatibleNumberIntegerType val) noexcept void to_json(Json& 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 Json::number_integer_t>(val));
} }
template <typename Json, typename UnscopedEnumType, template <typename Json, 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(Json& 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 Json, typename CompatibleArrayType,
enable_if_t< enable_if_t <
is_compatible_array_type<Json, CompatibleArrayType>::value or is_compatible_array_type<Json, CompatibleArrayType>::value or
std::is_same<typename Json::array_t, CompatibleArrayType>::value, std::is_same<typename Json::array_t, CompatibleArrayType>::value,
int> = 0> int > = 0 >
void to_json(Json &j, const CompatibleArrayType &arr) void to_json(Json& j, const CompatibleArrayType& arr)
{ {
external_constructor<value_t::array>::construct(j, arr); external_constructor<value_t::array>::construct(j, arr);
} }
@ -573,49 +581,53 @@ void to_json(Json &j, const CompatibleArrayType &arr)
template < template <
typename Json, typename CompatibleObjectType, typename Json, typename CompatibleObjectType,
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value, enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value,
int> = 0> int> = 0 >
void to_json(Json &j, const CompatibleObjectType &arr) void to_json(Json& 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 Json>
void from_json(const Json & j, typename Json::boolean_t& b) void from_json(const Json& j, typename Json::boolean_t& b)
{ {
if (!j.is_boolean()) if (!j.is_boolean())
{
throw std::domain_error("type must be boolean, but is " + type_name(j)); 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 Json::boolean_t*>();
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, typename Json::string_t& s) void from_json(const Json& j, typename Json::string_t& s)
{ {
if (!j.is_string()) if (!j.is_string())
{
throw std::domain_error("type must be string, but is " + type_name(j)); 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 Json::string_t*>();
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, typename Json::number_float_t& val) void from_json(const Json& j, typename Json::number_float_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, typename Json::number_unsigned_t& val) void from_json(const Json& j, typename Json::number_unsigned_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, typename Json::number_integer_t& val) void from_json(const Json& j, typename Json::number_integer_t& val)
{ {
get_arithmetic_value(j, val); get_arithmetic_value(j, val);
} }
template <typename Json, typename UnscopedEnumType, template <typename Json, 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 Json& 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);
@ -623,38 +635,46 @@ void from_json(const Json &j, UnscopedEnumType& e)
} }
template <typename Json> template <typename Json>
void from_json(const Json &j, typename Json::array_t &arr) void from_json(const Json& j, typename Json::array_t& arr)
{ {
if (!j.is_array()) if (!j.is_array())
{
throw std::domain_error("type must be array, but is " + type_name(j)); 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 Json::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 Json, typename T, typename Allocator>
void from_json(const Json &j, std::forward_list<T, Allocator>& l) void from_json(const Json& 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.. ?)
if (j.is_null()) if (j.is_null())
{
throw std::domain_error("type must be array, but is " + type_name(j)); 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, Json>::value)
{ {
if (!j.is_array()) if (!j.is_array())
{
throw std::domain_error("type must be array, but is " + type_name(j)); throw std::domain_error("type must be array, but is " + type_name(j));
} }
}
for (auto it = j.rbegin(), end = j.rend(); it != end; ++it) for (auto it = j.rbegin(), end = j.rend(); it != end; ++it)
{
l.push_front(it->template get<T>()); l.push_front(it->template get<T>());
}
} }
template <typename Json, typename CompatibleArrayType> template <typename Json, typename CompatibleArrayType>
void from_json_array_impl(const Json &j, CompatibleArrayType &arr, priority_tag<0>) void from_json_array_impl(const Json& 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 Json & i)
{ {
// get<Json>() returns *this, this won't call a from_json method when // get<Json>() returns *this, this won't call a from_json method when
// value_type is Json // value_type is Json
@ -663,8 +683,8 @@ void from_json_array_impl(const Json &j, CompatibleArrayType &arr, priority_tag
} }
template <typename Json, typename CompatibleArrayType> template <typename Json, typename CompatibleArrayType>
auto from_json_array_impl(const Json &j, CompatibleArrayType &arr, priority_tag<1>) auto from_json_array_impl(const Json& 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())
{ {
@ -673,7 +693,7 @@ 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 Json & i)
{ {
// get<Json>() returns *this, this won't call a from_json method when // get<Json>() returns *this, this won't call a from_json method when
// value_type is Json // value_type is Json
@ -686,29 +706,35 @@ template <
enable_if_t<is_compatible_array_type<Json, CompatibleArrayType>::value and enable_if_t<is_compatible_array_type<Json, CompatibleArrayType>::value and
not std::is_same<typename Json::array_t, not std::is_same<typename Json::array_t,
CompatibleArrayType>::value, CompatibleArrayType>::value,
int> = 0> int> = 0 >
void from_json(const Json &j, CompatibleArrayType &arr) void from_json(const Json& j, CompatibleArrayType& arr)
{ {
if (j.is_null()) if (j.is_null())
{
throw std::domain_error("type must be array, but is " + type_name(j)); 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 == Json, 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, Json>::value)
{ {
if (!j.is_array()) if (!j.is_array())
{
throw std::domain_error("type must be array, but is " + type_name(j)); throw std::domain_error("type must be array, but is " + type_name(j));
} }
from_json_array_impl(j, arr, priority_tag<1>{}); }
from_json_array_impl(j, arr, priority_tag<1> {});
} }
template < template <
typename Json, typename CompatibleObjectType, typename Json, typename CompatibleObjectType,
enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value, enable_if_t<is_compatible_object_type<Json, CompatibleObjectType>::value,
int> = 0> int> = 0 >
void from_json(const Json &j, CompatibleObjectType &obj) void from_json(const Json& j, CompatibleObjectType& obj)
{ {
if (!j.is_object()) if (!j.is_object())
{
throw std::domain_error("type must be object, but is " + type_name(j)); 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 Json::object_t*>();
using std::begin; using std::begin;
@ -724,25 +750,35 @@ void from_json(const Json &j, CompatibleObjectType &obj)
// 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 Json, 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, typename Json::number_unsigned_t>::value and not std::is_same<ArithmeticType, typename Json::number_unsigned_t>::value and
not std::is_same<ArithmeticType, typename Json::number_integer_t>::value and not std::is_same<ArithmeticType, typename Json::number_integer_t>::value and
not std::is_same<ArithmeticType, typename Json::number_float_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, not std::is_same<ArithmeticType, typename Json::boolean_t>::value,
int> = 0> int > = 0 >
void from_json(const Json &j, ArithmeticType &val) void from_json(const Json& j, ArithmeticType& val)
{ {
if (j.is_number_unsigned()) if (j.is_number_unsigned())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>());
}
else if (j.is_number_integer()) else if (j.is_number_integer())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>());
}
else if (j.is_number_float()) else if (j.is_number_float())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>());
}
else if (j.is_boolean()) else if (j.is_boolean())
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::boolean_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::boolean_t*>());
}
else else
{
throw std::domain_error("type must be number, but is " + type_name(j)); throw std::domain_error("type must be number, but is " + type_name(j));
}
} }
struct to_json_fn struct to_json_fn
@ -762,20 +798,20 @@ struct to_json_fn
static_assert(sizeof(Json) == 0, "to_json method in T's namespace can not be called"); static_assert(sizeof(Json) == 0, "to_json method in T's namespace can not be called");
} }
public: public:
template <typename Json, typename T> template <typename Json, typename T>
void operator()(Json &j, T &&val) const void operator()(Json& 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> {});
} }
}; };
struct from_json_fn struct from_json_fn
{ {
private: private:
template <typename Json, typename T> template <typename Json, typename T>
auto call(const Json &j, T &val, priority_tag<1>) const auto call(const Json& 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())
{ {
@ -783,17 +819,17 @@ private:
} }
template <typename Json, typename T> template <typename Json, typename T>
void call(const Json &, T&, priority_tag<0>) const noexcept void call(const Json&, 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(Json) == 0, "from_json method in T's namespace can not be called");
} }
public: public:
template <typename Json, typename T> template <typename Json, typename T>
void operator()(const Json &j, T &val) const void operator()(const Json& 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> {});
} }
}; };
@ -830,8 +866,8 @@ struct DecimalSeparator : std::numpunct<char>
inline namespace inline namespace
{ {
constexpr const auto & to_json = detail::static_const<detail::to_json_fn>::value; constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
constexpr const auto & from_json = detail::static_const<detail::from_json_fn>::value; constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
} }
// default JSONSerializer template argument, doesn't care about template argument // default JSONSerializer template argument, doesn't care about template argument
@ -846,7 +882,7 @@ struct adl_serializer
} }
template <typename Json, typename T> template <typename Json, typename T>
static void to_json(Json &j, T &&val) noexcept( static void to_json(Json& 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));
@ -947,7 +983,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 Json> friend std::string detail::type_name(const Json&);
/// 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,
@ -1854,8 +1890,8 @@ class basic_json
basic_json_t, U>::value and basic_json_t, U>::value and
detail::has_to_json<basic_json, U>::value, detail::has_to_json<basic_json, U>::value,
int> = 0> int> = 0>
basic_json(T &&val) noexcept(noexcept(JSONSerializer<U>::to_json( basic_json(T && val) noexcept(noexcept(JSONSerializer<U>::to_json(
std::declval<basic_json_t &>(), std::forward<T>(val)))) std::declval<basic_json_t&>(), std::forward<T>(val))))
{ {
JSONSerializer<U>::to_json(*this, std::forward<T>(val)); JSONSerializer<U>::to_json(*this, std::forward<T>(val));
} }
@ -3041,17 +3077,17 @@ class basic_json
template < template <
typename T, typename T,
typename U = uncvref_t<T>, typename U = uncvref_t<T>,
enable_if_t< enable_if_t <
not std::is_same<basic_json_t, U>::value and not std::is_same<basic_json_t, U>::value and
detail::has_from_json<basic_json_t, U>::value and detail::has_from_json<basic_json_t, U>::value and
not detail::has_non_default_from_json<basic_json_t, not detail::has_non_default_from_json<basic_json_t,
U>::value, U>::value,
int> = 0> int > = 0 >
// do we really want the uncvref ? if a user call get<int &>, shouldn't we // do we really want the uncvref ? if a user call get<int &>, shouldn't we
// static assert ? // static assert ?
// i know there is a special behaviour for boolean_t* and such // i know there is a special behaviour for boolean_t* and such
auto get() const noexcept(noexcept(JSONSerializer<U>::from_json( auto get() const noexcept(noexcept(JSONSerializer<U>::from_json(
std::declval<const basic_json_t &>(), std::declval<U &>()))) std::declval<const basic_json_t&>(), std::declval<U&>())))
-> U -> U
{ {
static_assert(std::is_default_constructible<U>::value and static_assert(std::is_default_constructible<U>::value and
@ -3083,8 +3119,8 @@ class basic_json
enable_if_t<not std::is_same<basic_json_t, uncvref_t<T>>::value and enable_if_t<not std::is_same<basic_json_t, uncvref_t<T>>::value and
detail::has_non_default_from_json<basic_json_t, detail::has_non_default_from_json<basic_json_t,
uncvref_t<T>>::value, uncvref_t<T>>::value,
int> = 0> int> = 0 >
uncvref_t<T> get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t &>()))) uncvref_t<T> get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>())))
{ {
return JSONSerializer<T>::from_json(*this); return JSONSerializer<T>::from_json(*this);
} }
@ -7871,7 +7907,10 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
std::string type_name() const { return detail::type_name(*this); } std::string type_name() const
{
return detail::type_name(*this);
}
private: private:
/*! /*!
@ -10838,14 +10877,14 @@ class basic_json
} }
private: private:
friend bool operator==(json_pointer const &lhs, friend bool operator==(json_pointer const& lhs,
json_pointer const &rhs) noexcept json_pointer const& rhs) noexcept
{ {
return lhs.reference_tokens == rhs.reference_tokens; return lhs.reference_tokens == rhs.reference_tokens;
} }
friend bool operator!=(json_pointer const &lhs, friend bool operator!=(json_pointer const& lhs,
json_pointer const &rhs) noexcept json_pointer const& rhs) noexcept
{ {
return !(lhs == rhs); return !(lhs == rhs);
} }
@ -11536,7 +11575,8 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
2, // integer 2, // integer
2, // unsigned 2, // unsigned
2, // float 2, // float
}}; }
};
// discarded values are not comparable // discarded values are not comparable
if (lhs == value_t::discarded or rhs == value_t::discarded) if (lhs == value_t::discarded or rhs == value_t::discarded)
@ -11571,25 +11611,25 @@ using json = basic_json<>;
// specialization of std::swap, and std::hash // specialization of std::swap, and std::hash
namespace std namespace std
{ {
/*! /*!
@brief exchanges the values of two JSON objects @brief exchanges the values of two JSON objects
@since version 1.0.0 @since version 1.0.0
*/ */
template<> template<>
inline void swap(nlohmann::json& j1, inline void swap(nlohmann::json& j1,
nlohmann::json& j2) noexcept( nlohmann::json& j2) noexcept(
is_nothrow_move_constructible<nlohmann::json>::value and is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value is_nothrow_move_assignable<nlohmann::json>::value
) )
{ {
j1.swap(j2); j1.swap(j2);
} }
/// hash value for JSON objects /// hash value for JSON objects
template<> template<>
struct hash<nlohmann::json> struct hash<nlohmann::json>
{ {
/*! /*!
@brief return a hash value for a JSON object @brief return a hash value for a JSON object
@ -11601,7 +11641,7 @@ namespace std
const auto& h = hash<nlohmann::json::string_t>(); const auto& h = hash<nlohmann::json::string_t>();
return h(j.dump()); return h(j.dump());
} }
}; };
} // namespace std } // namespace std
/*! /*!

View file

@ -11,8 +11,8 @@ enum test
struct pod {}; struct pod {};
struct pod_bis {}; struct pod_bis {};
void to_json(json &, pod) noexcept; void to_json(json&, pod) noexcept;
void to_json(json &, pod_bis); void to_json(json&, pod_bis);
void from_json(const json&, pod) noexcept; void from_json(const json&, pod) noexcept;
void from_json(const json&, pod_bis); void from_json(const json&, pod_bis);
static json j; static json j;

View file

@ -63,7 +63,7 @@ TEST_CASE("regression tests")
SECTION("pull request #71 - handle enum type") SECTION("pull request #71 - handle enum type")
{ {
enum { t = 0 , u = 1}; enum { t = 0, u = 1};
json j = json::array(); json j = json::array();
j.push_back(t); j.push_back(t);

View file

@ -114,22 +114,22 @@ void to_json(Json& j, country c)
} }
template <typename Json> template <typename Json>
void to_json(Json& j, const person & p) void to_json(Json& j, const person& p)
{ {
j = Json{{"age", p.m_age}, {"name", p.m_name}, {"country", p.m_country}}; j = Json{{"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)
{ {
j = a.m_val; j = a.m_val;
} }
void to_json(nlohmann::json& j, const contact & c) void to_json(nlohmann::json& j, const contact& c)
{ {
j = json{{"person", c.m_person}, {"address", c.m_address}}; j = json{{"person", c.m_person}, {"address", c.m_address}};
} }
void to_json(nlohmann::json& j, const contact_book & cb) void to_json(nlohmann::json& j, const contact_book& cb)
{ {
j = json{{"name", cb.m_book_name}, {"contacts", cb.m_contacts}}; j = json{{"name", cb.m_book_name}, {"contacts", cb.m_contacts}};
} }
@ -140,28 +140,28 @@ bool operator==(age lhs, age rhs)
return lhs.m_val == rhs.m_val; return lhs.m_val == rhs.m_val;
} }
bool operator==(const address & lhs, const address & rhs) bool operator==(const address& lhs, const address& rhs)
{ {
return lhs.m_val == rhs.m_val; return lhs.m_val == rhs.m_val;
} }
bool operator==(const name & lhs, const name & rhs) bool operator==(const name& lhs, const name& rhs)
{ {
return lhs.m_val == rhs.m_val; return lhs.m_val == rhs.m_val;
} }
bool operator==(const person & lhs, const person & rhs) bool operator==(const person& lhs, const person& rhs)
{ {
return std::tie(lhs.m_name, lhs.m_age) == std::tie(rhs.m_name, rhs.m_age); return std::tie(lhs.m_name, lhs.m_age) == std::tie(rhs.m_name, rhs.m_age);
} }
bool operator==(const contact & lhs, const contact & rhs) bool operator==(const contact& lhs, const contact& rhs)
{ {
return std::tie(lhs.m_person, lhs.m_address) == return std::tie(lhs.m_person, lhs.m_address) ==
std::tie(rhs.m_person, rhs.m_address); std::tie(rhs.m_person, rhs.m_address);
} }
bool operator==(const contact_book & lhs, const contact_book & rhs) bool operator==(const contact_book& lhs, const contact_book& rhs)
{ {
return std::tie(lhs.m_book_name, lhs.m_contacts) == return std::tie(lhs.m_book_name, lhs.m_contacts) ==
std::tie(rhs.m_book_name, rhs.m_contacts); std::tie(rhs.m_book_name, rhs.m_contacts);
@ -172,19 +172,19 @@ bool operator==(const contact_book & lhs, const contact_book & rhs)
namespace udt namespace udt
{ {
template <typename Json> template <typename Json>
void from_json(const Json & j, age& a) void from_json(const Json& j, age& a)
{ {
a.m_val = j.template get<int>(); a.m_val = j.template get<int>();
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, name& n) void from_json(const Json& j, name& n)
{ {
n.m_val = j.template get<std::string>(); n.m_val = j.template get<std::string>();
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, country& c) void from_json(const Json& 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 =
@ -200,25 +200,25 @@ void from_json(const Json & j, country& c)
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, person& p) void from_json(const Json& 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>();
p.m_country = j["country"].template get<country>(); p.m_country = j["country"].template get<country>();
} }
void from_json(const nlohmann::json & j, address& a) void from_json(const nlohmann::json& j, address& a)
{ {
a.m_val = j.get<std::string>(); a.m_val = j.get<std::string>();
} }
void from_json(const nlohmann::json & j, contact& c) void from_json(const nlohmann::json& j, contact& c)
{ {
c.m_person = j["person"].get<person>(); c.m_person = j["person"].get<person>();
c.m_address = j["address"].get<address>(); c.m_address = j["address"].get<address>();
} }
void from_json(const nlohmann::json & j, contact_book& cb) void from_json(const nlohmann::json& j, contact_book& cb)
{ {
cb.m_book_name = j["name"].get<name>(); cb.m_book_name = j["name"].get<name>();
cb.m_contacts = j["contacts"].get<std::vector<contact>>(); cb.m_contacts = j["contacts"].get<std::vector<contact>>();
@ -297,7 +297,7 @@ namespace nlohmann
template <typename T> template <typename T>
struct adl_serializer<std::shared_ptr<T>> struct adl_serializer<std::shared_ptr<T>>
{ {
static void to_json(json& j, const std::shared_ptr<T> & opt) static void to_json(json& j, const std::shared_ptr<T>& opt)
{ {
if (opt) if (opt)
{ {
@ -309,7 +309,7 @@ struct adl_serializer<std::shared_ptr<T>>
} }
} }
static void from_json(const json & j, std::shared_ptr<T>& opt) static void from_json(const json& j, std::shared_ptr<T>& opt)
{ {
if (j.is_null()) if (j.is_null())
{ {
@ -325,12 +325,12 @@ struct adl_serializer<std::shared_ptr<T>>
template <> template <>
struct adl_serializer<udt::legacy_type> struct adl_serializer<udt::legacy_type>
{ {
static void to_json(json& j, const udt::legacy_type & l) static void to_json(json& j, const udt::legacy_type& l)
{ {
j = std::stoi(l.number); j = std::stoi(l.number);
} }
static void from_json(const json & j, udt::legacy_type& l) static void from_json(const json& j, udt::legacy_type& l)
{ {
l.number = std::to_string(j.get<int>()); l.number = std::to_string(j.get<int>());
} }
@ -395,18 +395,18 @@ template <>
struct adl_serializer<std::vector<float>> struct adl_serializer<std::vector<float>>
{ {
using type = std::vector<float>; using type = std::vector<float>;
static void to_json(json& j, const type &) static void to_json(json& j, const type&)
{ {
j = "hijacked!"; j = "hijacked!";
} }
static void from_json(const json &, type& opt) static void from_json(const json&, type& opt)
{ {
opt = {42.0, 42.0, 42.0}; opt = {42.0, 42.0, 42.0};
} }
// preferred version // preferred version
static type from_json(const json &) static type from_json(const json&)
{ {
return {4.0, 5.0, 6.0}; return {4.0, 5.0, 6.0};
} }
@ -419,7 +419,7 @@ TEST_CASE("even supported types can be specialized", "[udt]")
CHECK(j.dump() == R"("hijacked!")"); CHECK(j.dump() == R"("hijacked!")");
auto f = j.get<std::vector<float>>(); auto f = j.get<std::vector<float>>();
// the single argument from_json method is preferred // the single argument from_json method is preferred
CHECK((f == std::vector<float>{4.0, 5.0, 6.0})); CHECK((f == std::vector<float> {4.0, 5.0, 6.0}));
} }
namespace nlohmann namespace nlohmann
@ -427,7 +427,7 @@ namespace nlohmann
template <typename T> template <typename T>
struct adl_serializer<std::unique_ptr<T>> struct adl_serializer<std::unique_ptr<T>>
{ {
static void to_json(json& j, const std::unique_ptr<T> & opt) static void to_json(json& j, const std::unique_ptr<T>& opt)
{ {
if (opt) if (opt)
{ {
@ -440,7 +440,7 @@ struct adl_serializer<std::unique_ptr<T>>
} }
// this is the overload needed for non-copyable types, // this is the overload needed for non-copyable types,
static std::unique_ptr<T> from_json(const json & j) static std::unique_ptr<T> from_json(const json& j)
{ {
if (j.is_null()) if (j.is_null())
{ {
@ -494,9 +494,9 @@ 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 Json, 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 Json& j, U& t)
{ {
using nlohmann::from_json; using nlohmann::from_json;
from_json(j, t); from_json(j, t);
@ -506,7 +506,7 @@ struct pod_serializer
template <typename Json, typename U = T, template <typename Json, 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 Json& 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
@ -524,15 +524,15 @@ struct pod_serializer
// calling get calls from_json, for now, we cannot do this in custom // calling get calls from_json, for now, we cannot do this in custom
// serializers // serializers
nlohmann::from_json(j, value); nlohmann::from_json(j, value);
auto bytes = static_cast<char *>(static_cast<void *>(&value)); auto bytes = static_cast<char*>(static_cast<void*>(&value));
std::memcpy(&t, bytes, sizeof(value)); std::memcpy(&t, bytes, sizeof(value));
} }
template < template <
typename Json, typename U = T, typename Json, 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(Json& j, const T& t)
{ {
using nlohmann::to_json; using nlohmann::to_json;
to_json(j, t); to_json(j, t);
@ -541,9 +541,9 @@ struct pod_serializer
template <typename Json, typename U = T, template <typename Json, 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(Json& 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];
for (auto i = 1; i < 8; ++i) for (auto i = 1; i < 8; ++i)
value |= std::uint64_t{bytes[i]} << 8 * i; value |= std::uint64_t{bytes[i]} << 8 * i;
@ -566,13 +566,13 @@ struct non_pod
}; };
template <typename Json> template <typename Json>
void to_json(Json& j, const non_pod & np) void to_json(Json& j, const non_pod& np)
{ {
j = np.s; j = np.s;
} }
template <typename Json> template <typename Json>
void from_json(const Json & j, non_pod& np) void from_json(const Json& j, non_pod& np)
{ {
np.s = j.template get<std::string>(); np.s = j.template get<std::string>();
} }
@ -583,7 +583,7 @@ bool operator==(small_pod lhs, small_pod rhs) noexcept
std::tie(rhs.begin, rhs.middle, rhs.end); std::tie(rhs.begin, rhs.middle, rhs.end);
} }
bool operator==(const non_pod &lhs, const non_pod &rhs) noexcept bool operator==(const non_pod& lhs, const non_pod& rhs) noexcept
{ {
return lhs.s == rhs.s; return lhs.s == rhs.s;
} }
@ -622,13 +622,13 @@ using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, boo
template <typename T, typename> template <typename T, typename>
struct another_adl_serializer struct another_adl_serializer
{ {
static void from_json(const custom_json & j , T& t) static void from_json(const custom_json& j, T& t)
{ {
using nlohmann::from_json; using nlohmann::from_json;
from_json(j, t); from_json(j, t);
} }
static void to_json(custom_json& j , const T & t) static void to_json(custom_json& j, const T& t)
{ {
using nlohmann::to_json; using nlohmann::to_json;
to_json(j, t); to_json(j, t);