💄 overworked documentation and indentation
This commit is contained in:
parent
030cf674ef
commit
250e5bf43d
2 changed files with 486 additions and 436 deletions
253
src/json.hpp
253
src/json.hpp
|
@ -107,12 +107,15 @@ SOFTWARE.
|
||||||
*/
|
*/
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
{
|
{
|
||||||
// TODO update this doc
|
|
||||||
/*!
|
/*!
|
||||||
@brief unnamed namespace with internal helper functions
|
@brief unnamed namespace with internal helper functions
|
||||||
@since version 1.0.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
This namespace collects some functions that could not be defined inside the
|
||||||
|
@ref basic_json class.
|
||||||
|
|
||||||
|
@since version 2.1.0
|
||||||
|
*/
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
///////////////////////////
|
///////////////////////////
|
||||||
|
@ -122,22 +125,24 @@ namespace detail
|
||||||
/*!
|
/*!
|
||||||
@brief the JSON type enumeration
|
@brief the JSON type enumeration
|
||||||
|
|
||||||
This enumeration collects the different JSON types. It is internally used
|
This enumeration collects the different JSON types. It is internally used to
|
||||||
to distinguish the stored values, and the functions @ref basic_json::is_null(), @ref
|
distinguish the stored values, and the functions @ref basic_json::is_null(),
|
||||||
basic_json::is_object(), @ref basic_json::is_array(), @ref basic_json::is_string(), @ref basic_json::is_boolean(), @ref
|
@ref basic_json::is_object(), @ref basic_json::is_array(),
|
||||||
basic_json::is_number() (with @ref basic_json::is_number_integer(), @ref basic_json::is_number_unsigned(), and
|
@ref basic_json::is_string(), @ref basic_json::is_boolean(),
|
||||||
@ref basic_json::is_number_float()), @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
@ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
|
||||||
|
@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
|
||||||
|
@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
||||||
@ref basic_json::is_structured() rely on it.
|
@ref basic_json::is_structured() rely on it.
|
||||||
|
|
||||||
@note There are three enumeration entries (number_integer,
|
@note There are three enumeration entries (number_integer, number_unsigned, and
|
||||||
number_unsigned, and number_float), because the library distinguishes
|
number_float), because the library distinguishes these three types for numbers:
|
||||||
these three types for numbers: @ref basic_json::number_unsigned_t is used for unsigned
|
@ref basic_json::number_unsigned_t is used for unsigned integers,
|
||||||
integers, @ref basic_json::number_integer_t is used for signed integers, and @ref
|
@ref basic_json::number_integer_t is used for signed integers, and
|
||||||
basic_json::number_float_t is used for floating-point numbers or to approximate
|
@ref basic_json::number_float_t is used for floating-point numbers or to
|
||||||
integers which do not fit in the limits of their respective type.
|
approximate integers which do not fit in the limits of their respective type.
|
||||||
|
|
||||||
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON value with
|
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
|
||||||
the default value for a given type
|
value with the default value for a given type
|
||||||
|
|
||||||
@since version 1.0.0
|
@since version 1.0.0
|
||||||
*/
|
*/
|
||||||
|
@ -154,12 +159,6 @@ enum class value_t : uint8_t
|
||||||
discarded ///< discarded by the the parser callback function
|
discarded ///< discarded by the the parser callback function
|
||||||
};
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////
|
|
||||||
// lexicographical comparison operators //
|
|
||||||
//////////////////////////////////////////
|
|
||||||
|
|
||||||
/// @name lexicographical comparison operators
|
|
||||||
/// @{
|
|
||||||
/*!
|
/*!
|
||||||
@brief comparison operator for JSON types
|
@brief comparison operator for JSON types
|
||||||
|
|
||||||
|
@ -193,6 +192,11 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
|
||||||
order[static_cast<std::size_t>(rhs)];
|
order[static_cast<std::size_t>(rhs)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// helpers //
|
||||||
|
/////////////
|
||||||
|
|
||||||
// alias templates to reduce boilerplate
|
// alias templates to reduce boilerplate
|
||||||
template<bool B, typename T = void>
|
template<bool B, typename T = void>
|
||||||
using enable_if_t = typename std::enable_if<B, T>::type;
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
||||||
|
@ -206,32 +210,38 @@ using is_unscoped_enum =
|
||||||
std::integral_constant<bool, std::is_convertible<T, int>::value and
|
std::integral_constant<bool, std::is_convertible<T, int>::value and
|
||||||
std::is_enum<T>::value>;
|
std::is_enum<T>::value>;
|
||||||
|
|
||||||
// Implementation of 2 C++17 constructs: conjunction, negation.
|
/*
|
||||||
// This is needed to avoid evaluating all the traits in a condition
|
Implementation of 2 C++17 constructs: conjunction, negation. This is needed
|
||||||
//
|
to avoid evaluating all the traits in a condition
|
||||||
// For example: not std::is_same<void, T>::value and has_value_type<T>::value
|
|
||||||
// will not compile when T = void (on MSVC at least)
|
For example: not std::is_same<void, T>::value and has_value_type<T>::value
|
||||||
// Whereas conjunction<negation<std::is_same<void, T>>, has_value_type<T>>::value
|
will not compile when T = void (on MSVC at least). Whereas
|
||||||
// will stop evaluating if negation<...>::value == false
|
conjunction<negation<std::is_same<void, T>>, has_value_type<T>>::value will
|
||||||
//
|
stop evaluating if negation<...>::value == false
|
||||||
// Please note that those constructs must be used with caution, since symbols can
|
|
||||||
// become very long quickly (which can slow down compilation and cause MSVC internal compiler errors)
|
Please note that those constructs must be used with caution, since symbols can
|
||||||
// Only use it when you have too (see example ahead)
|
become very long quickly (which can slow down compilation and cause MSVC
|
||||||
|
internal compiler errors). Only use it when you have to (see example ahead).
|
||||||
|
*/
|
||||||
template<class...> struct conjunction : std::true_type {};
|
template<class...> struct conjunction : std::true_type {};
|
||||||
template<class B1> struct conjunction<B1> : B1 {};
|
template<class B1> struct conjunction<B1> : B1 {};
|
||||||
template<class B1, class... Bn>
|
template<class B1, class... Bn>
|
||||||
struct conjunction<B1, Bn...>
|
struct conjunction<B1, Bn...> : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
|
||||||
: std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
|
|
||||||
|
|
||||||
template<class B> struct negation : std::integral_constant < bool, !B::value > {};
|
template<class B> struct negation : std::integral_constant < bool, !B::value > {};
|
||||||
|
|
||||||
// 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> {};
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// constructors //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
// This is an experiment. I need this to move constructors out of basic_json.
|
// This is an experiment. I need this to move constructors out of basic_json.
|
||||||
// I'm sure there is a better way, but this might need a big basic_json refactoring
|
// I'm sure there is a better way, but this might need a big basic_json
|
||||||
|
// refactoring
|
||||||
template<value_t> struct external_constructor;
|
template<value_t> struct external_constructor;
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
@ -266,7 +276,9 @@ struct external_constructor<value_t::number_float>
|
||||||
{
|
{
|
||||||
// replace infinity and NAN by null
|
// replace infinity and NAN by null
|
||||||
if (not std::isfinite(val))
|
if (not std::isfinite(val))
|
||||||
|
{
|
||||||
j = BasicJsonType{};
|
j = BasicJsonType{};
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
j.m_type = value_t::number_float;
|
j.m_type = value_t::number_float;
|
||||||
|
@ -320,8 +332,7 @@ struct external_constructor<value_t::array>
|
||||||
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 BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -347,12 +358,16 @@ struct external_constructor<value_t::object>
|
||||||
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 BasicJsonType::object_t>(begin(obj), end(obj));
|
||||||
j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
// has_/is_ functions //
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief Helper to determine whether there's a key_type for T.
|
@brief Helper to determine whether there's a key_type for T.
|
||||||
|
|
||||||
|
@ -417,8 +432,6 @@ struct is_basic_json_nested_type
|
||||||
template<class BasicJsonType, class CompatibleArrayType>
|
template<class BasicJsonType, class CompatibleArrayType>
|
||||||
struct is_compatible_array_type
|
struct is_compatible_array_type
|
||||||
{
|
{
|
||||||
// TODO concept Container?
|
|
||||||
// this might not make VS happy
|
|
||||||
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<
|
||||||
|
@ -450,13 +463,14 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
|
||||||
template<typename RealIntegerType, typename CompatibleNumberIntegerType>
|
template<typename RealIntegerType, typename CompatibleNumberIntegerType>
|
||||||
struct is_compatible_integer_type
|
struct is_compatible_integer_type
|
||||||
{
|
{
|
||||||
static constexpr auto
|
static constexpr auto value =
|
||||||
value = is_compatible_integer_type_impl <
|
is_compatible_integer_type_impl <
|
||||||
std::is_integral<CompatibleNumberIntegerType>::value and
|
std::is_integral<CompatibleNumberIntegerType>::value and
|
||||||
not std::is_same<bool, CompatibleNumberIntegerType>::value,
|
not std::is_same<bool, CompatibleNumberIntegerType>::value,
|
||||||
RealIntegerType, CompatibleNumberIntegerType > ::value;
|
RealIntegerType, CompatibleNumberIntegerType > ::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 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 BasicJsonType, typename T>
|
template<typename BasicJsonType, typename T>
|
||||||
struct has_from_json
|
struct has_from_json
|
||||||
|
@ -506,34 +520,10 @@ struct has_to_json
|
||||||
std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
|
std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// overloads for basic_json template parameters
|
|
||||||
|
|
||||||
template <typename BasicJsonType, typename ArithmeticType,
|
/////////////
|
||||||
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
|
// to_json //
|
||||||
not std::is_same<ArithmeticType,
|
/////////////
|
||||||
typename BasicJsonType::boolean_t>::value,
|
|
||||||
int> = 0>
|
|
||||||
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 BasicJsonType::number_unsigned_t*>());
|
|
||||||
break;
|
|
||||||
case value_t::number_integer:
|
|
||||||
val = static_cast<ArithmeticType>(
|
|
||||||
*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 BasicJsonType::number_float_t*>());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
JSON_THROW(
|
|
||||||
std::domain_error("type must be number, but is " + j.type_name()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
||||||
|
@ -557,7 +547,6 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
|
||||||
external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
|
external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
||||||
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
|
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
|
||||||
|
@ -605,6 +594,47 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr)
|
||||||
external_constructor<value_t::object>::construct(j, arr);
|
external_constructor<value_t::object>::construct(j, arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
// from_json //
|
||||||
|
///////////////
|
||||||
|
|
||||||
|
// overloads for basic_json template parameters
|
||||||
|
template<typename BasicJsonType, typename ArithmeticType,
|
||||||
|
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
|
||||||
|
not std::is_same<ArithmeticType,
|
||||||
|
typename BasicJsonType::boolean_t>::value,
|
||||||
|
int> = 0>
|
||||||
|
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 BasicJsonType::number_unsigned_t*>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case value_t::number_integer:
|
||||||
|
{
|
||||||
|
val = static_cast<ArithmeticType>(
|
||||||
|
*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 BasicJsonType::number_float_t*>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
JSON_THROW(
|
||||||
|
std::domain_error("type must be number, but is " + j.type_name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
||||||
{
|
{
|
||||||
|
@ -662,7 +692,8 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
|
||||||
arr = *j.template get_ptr<const typename BasicJsonType::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 BasicJsonType, typename T, typename Allocator>
|
template<typename BasicJsonType, typename T, typename Allocator>
|
||||||
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
||||||
{
|
{
|
||||||
|
@ -691,11 +722,11 @@ void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri
|
||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
std::transform(
|
std::transform(j.begin(), j.end(),
|
||||||
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
||||||
{
|
{
|
||||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
||||||
// value_type is BasicJsonType
|
// method when value_type is BasicJsonType
|
||||||
return i.template get<typename CompatibleArrayType::value_type>();
|
return i.template get<typename CompatibleArrayType::value_type>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -713,24 +744,22 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri
|
||||||
std::transform(
|
std::transform(
|
||||||
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
||||||
{
|
{
|
||||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
||||||
// value_type is BasicJsonType
|
// method when value_type is BasicJsonType
|
||||||
return i.template get<typename CompatibleArrayType::value_type>();
|
return i.template get<typename CompatibleArrayType::value_type>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template<typename BasicJsonType, typename CompatibleArrayType,
|
||||||
typename BasicJsonType, typename CompatibleArrayType,
|
|
||||||
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
|
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
|
||||||
not std::is_same<typename BasicJsonType::array_t,
|
not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value, int> = 0>
|
||||||
CompatibleArrayType>::value,
|
|
||||||
int> = 0 >
|
|
||||||
void from_json(const BasicJsonType& 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 " + j.type_name()));
|
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// when T == BasicJsonType, 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, BasicJsonType>::value)
|
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
|
||||||
{
|
{
|
||||||
|
@ -742,11 +771,8 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
|
||||||
from_json_array_impl(j, arr, priority_tag<1> {});
|
from_json_array_impl(j, arr, priority_tag<1> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType, typename CompatibleObjectType,
|
||||||
template <
|
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
|
||||||
typename BasicJsonType, typename CompatibleObjectType,
|
|
||||||
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
|
|
||||||
int> = 0 >
|
|
||||||
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
|
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
|
||||||
{
|
{
|
||||||
if (!j.is_object())
|
if (!j.is_object())
|
||||||
|
@ -766,16 +792,12 @@ void from_json(const BasicJsonType& 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 BasicJsonType, 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 BasicJsonType::number_unsigned_t>::value and
|
||||||
typename BasicJsonType::number_unsigned_t>::value and
|
not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
|
||||||
not std::is_same<ArithmeticType,
|
not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
|
||||||
typename BasicJsonType::number_integer_t>::value and
|
|
||||||
not std::is_same<ArithmeticType,
|
|
||||||
typename BasicJsonType::number_float_t>::value and
|
|
||||||
not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
||||||
int> = 0>
|
int> = 0>
|
||||||
void from_json(const BasicJsonType& j, ArithmeticType& val)
|
void from_json(const BasicJsonType& j, ArithmeticType& val)
|
||||||
|
@ -783,34 +805,27 @@ 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 BasicJsonType::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 BasicJsonType::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 BasicJsonType::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 BasicJsonType::boolean_t*>());
|
||||||
*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
JSON_THROW(
|
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
|
||||||
std::domain_error("type must be number, but is " + j.type_name()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct to_json_fn
|
struct to_json_fn
|
||||||
{
|
{
|
||||||
template<typename BasicJsonType, typename T>
|
template<typename BasicJsonType, typename T>
|
||||||
auto call(BasicJsonType& 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)), void())
|
||||||
-> decltype(to_json(j, std::forward<T>(val)),
|
|
||||||
void())
|
|
||||||
{
|
{
|
||||||
return to_json(j, std::forward<T>(val));
|
return to_json(j, std::forward<T>(val));
|
||||||
}
|
}
|
||||||
|
@ -866,6 +881,7 @@ struct static_const
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T static_const<T>::value;
|
constexpr T static_const<T>::value;
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief helper class to create locales with decimal point
|
@brief helper class to create locales with decimal point
|
||||||
|
|
||||||
|
@ -885,7 +901,8 @@ struct DecimalSeparator : std::numpunct<char>
|
||||||
return '.';
|
return '.';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
} // namespace detail
|
||||||
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -912,6 +929,7 @@ struct adl_serializer
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief a class to store JSON values
|
@brief a class to store JSON values
|
||||||
|
|
||||||
|
@ -5714,6 +5732,13 @@ class basic_json
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
//////////////////////////////////////////
|
||||||
|
// lexicographical comparison operators //
|
||||||
|
//////////////////////////////////////////
|
||||||
|
|
||||||
|
/// @name lexicographical comparison operators
|
||||||
|
/// @{
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief comparison: equal
|
@brief comparison: equal
|
||||||
|
|
||||||
|
@ -10795,7 +10820,7 @@ basic_json_parser_66:
|
||||||
for (; curptr < m_cursor; curptr++)
|
for (; curptr < m_cursor; curptr++)
|
||||||
{
|
{
|
||||||
// quickly skip tests if a digit
|
// quickly skip tests if a digit
|
||||||
if (*curptr < '0' || *curptr > '9')
|
if (*curptr < '0' or* curptr > '9')
|
||||||
{
|
{
|
||||||
if (*curptr == '.')
|
if (*curptr == '.')
|
||||||
{
|
{
|
||||||
|
|
|
@ -107,12 +107,15 @@ SOFTWARE.
|
||||||
*/
|
*/
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
{
|
{
|
||||||
// TODO update this doc
|
|
||||||
/*!
|
/*!
|
||||||
@brief unnamed namespace with internal helper functions
|
@brief unnamed namespace with internal helper functions
|
||||||
@since version 1.0.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
This namespace collects some functions that could not be defined inside the
|
||||||
|
@ref basic_json class.
|
||||||
|
|
||||||
|
@since version 2.1.0
|
||||||
|
*/
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
///////////////////////////
|
///////////////////////////
|
||||||
|
@ -122,22 +125,24 @@ namespace detail
|
||||||
/*!
|
/*!
|
||||||
@brief the JSON type enumeration
|
@brief the JSON type enumeration
|
||||||
|
|
||||||
This enumeration collects the different JSON types. It is internally used
|
This enumeration collects the different JSON types. It is internally used to
|
||||||
to distinguish the stored values, and the functions @ref basic_json::is_null(), @ref
|
distinguish the stored values, and the functions @ref basic_json::is_null(),
|
||||||
basic_json::is_object(), @ref basic_json::is_array(), @ref basic_json::is_string(), @ref basic_json::is_boolean(), @ref
|
@ref basic_json::is_object(), @ref basic_json::is_array(),
|
||||||
basic_json::is_number() (with @ref basic_json::is_number_integer(), @ref basic_json::is_number_unsigned(), and
|
@ref basic_json::is_string(), @ref basic_json::is_boolean(),
|
||||||
@ref basic_json::is_number_float()), @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
@ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
|
||||||
|
@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
|
||||||
|
@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
||||||
@ref basic_json::is_structured() rely on it.
|
@ref basic_json::is_structured() rely on it.
|
||||||
|
|
||||||
@note There are three enumeration entries (number_integer,
|
@note There are three enumeration entries (number_integer, number_unsigned, and
|
||||||
number_unsigned, and number_float), because the library distinguishes
|
number_float), because the library distinguishes these three types for numbers:
|
||||||
these three types for numbers: @ref basic_json::number_unsigned_t is used for unsigned
|
@ref basic_json::number_unsigned_t is used for unsigned integers,
|
||||||
integers, @ref basic_json::number_integer_t is used for signed integers, and @ref
|
@ref basic_json::number_integer_t is used for signed integers, and
|
||||||
basic_json::number_float_t is used for floating-point numbers or to approximate
|
@ref basic_json::number_float_t is used for floating-point numbers or to
|
||||||
integers which do not fit in the limits of their respective type.
|
approximate integers which do not fit in the limits of their respective type.
|
||||||
|
|
||||||
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON value with
|
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
|
||||||
the default value for a given type
|
value with the default value for a given type
|
||||||
|
|
||||||
@since version 1.0.0
|
@since version 1.0.0
|
||||||
*/
|
*/
|
||||||
|
@ -154,12 +159,6 @@ enum class value_t : uint8_t
|
||||||
discarded ///< discarded by the the parser callback function
|
discarded ///< discarded by the the parser callback function
|
||||||
};
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////
|
|
||||||
// lexicographical comparison operators //
|
|
||||||
//////////////////////////////////////////
|
|
||||||
|
|
||||||
/// @name lexicographical comparison operators
|
|
||||||
/// @{
|
|
||||||
/*!
|
/*!
|
||||||
@brief comparison operator for JSON types
|
@brief comparison operator for JSON types
|
||||||
|
|
||||||
|
@ -193,6 +192,11 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
|
||||||
order[static_cast<std::size_t>(rhs)];
|
order[static_cast<std::size_t>(rhs)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// helpers //
|
||||||
|
/////////////
|
||||||
|
|
||||||
// alias templates to reduce boilerplate
|
// alias templates to reduce boilerplate
|
||||||
template<bool B, typename T = void>
|
template<bool B, typename T = void>
|
||||||
using enable_if_t = typename std::enable_if<B, T>::type;
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
||||||
|
@ -206,32 +210,38 @@ using is_unscoped_enum =
|
||||||
std::integral_constant<bool, std::is_convertible<T, int>::value and
|
std::integral_constant<bool, std::is_convertible<T, int>::value and
|
||||||
std::is_enum<T>::value>;
|
std::is_enum<T>::value>;
|
||||||
|
|
||||||
// Implementation of 2 C++17 constructs: conjunction, negation.
|
/*
|
||||||
// This is needed to avoid evaluating all the traits in a condition
|
Implementation of 2 C++17 constructs: conjunction, negation. This is needed
|
||||||
//
|
to avoid evaluating all the traits in a condition
|
||||||
// For example: not std::is_same<void, T>::value and has_value_type<T>::value
|
|
||||||
// will not compile when T = void (on MSVC at least)
|
For example: not std::is_same<void, T>::value and has_value_type<T>::value
|
||||||
// Whereas conjunction<negation<std::is_same<void, T>>, has_value_type<T>>::value
|
will not compile when T = void (on MSVC at least). Whereas
|
||||||
// will stop evaluating if negation<...>::value == false
|
conjunction<negation<std::is_same<void, T>>, has_value_type<T>>::value will
|
||||||
//
|
stop evaluating if negation<...>::value == false
|
||||||
// Please note that those constructs must be used with caution, since symbols can
|
|
||||||
// become very long quickly (which can slow down compilation and cause MSVC internal compiler errors)
|
Please note that those constructs must be used with caution, since symbols can
|
||||||
// Only use it when you have too (see example ahead)
|
become very long quickly (which can slow down compilation and cause MSVC
|
||||||
|
internal compiler errors). Only use it when you have to (see example ahead).
|
||||||
|
*/
|
||||||
template<class...> struct conjunction : std::true_type {};
|
template<class...> struct conjunction : std::true_type {};
|
||||||
template<class B1> struct conjunction<B1> : B1 {};
|
template<class B1> struct conjunction<B1> : B1 {};
|
||||||
template<class B1, class... Bn>
|
template<class B1, class... Bn>
|
||||||
struct conjunction<B1, Bn...>
|
struct conjunction<B1, Bn...> : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
|
||||||
: std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
|
|
||||||
|
|
||||||
template<class B> struct negation : std::integral_constant < bool, !B::value > {};
|
template<class B> struct negation : std::integral_constant < bool, !B::value > {};
|
||||||
|
|
||||||
// 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> {};
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// constructors //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
// This is an experiment. I need this to move constructors out of basic_json.
|
// This is an experiment. I need this to move constructors out of basic_json.
|
||||||
// I'm sure there is a better way, but this might need a big basic_json refactoring
|
// I'm sure there is a better way, but this might need a big basic_json
|
||||||
|
// refactoring
|
||||||
template<value_t> struct external_constructor;
|
template<value_t> struct external_constructor;
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
@ -266,7 +276,9 @@ struct external_constructor<value_t::number_float>
|
||||||
{
|
{
|
||||||
// replace infinity and NAN by null
|
// replace infinity and NAN by null
|
||||||
if (not std::isfinite(val))
|
if (not std::isfinite(val))
|
||||||
|
{
|
||||||
j = BasicJsonType{};
|
j = BasicJsonType{};
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
j.m_type = value_t::number_float;
|
j.m_type = value_t::number_float;
|
||||||
|
@ -320,8 +332,7 @@ struct external_constructor<value_t::array>
|
||||||
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 BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -347,12 +358,16 @@ struct external_constructor<value_t::object>
|
||||||
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 BasicJsonType::object_t>(begin(obj), end(obj));
|
||||||
j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
// has_/is_ functions //
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief Helper to determine whether there's a key_type for T.
|
@brief Helper to determine whether there's a key_type for T.
|
||||||
|
|
||||||
|
@ -417,8 +432,6 @@ struct is_basic_json_nested_type
|
||||||
template<class BasicJsonType, class CompatibleArrayType>
|
template<class BasicJsonType, class CompatibleArrayType>
|
||||||
struct is_compatible_array_type
|
struct is_compatible_array_type
|
||||||
{
|
{
|
||||||
// TODO concept Container?
|
|
||||||
// this might not make VS happy
|
|
||||||
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<
|
||||||
|
@ -450,13 +463,14 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
|
||||||
template<typename RealIntegerType, typename CompatibleNumberIntegerType>
|
template<typename RealIntegerType, typename CompatibleNumberIntegerType>
|
||||||
struct is_compatible_integer_type
|
struct is_compatible_integer_type
|
||||||
{
|
{
|
||||||
static constexpr auto
|
static constexpr auto value =
|
||||||
value = is_compatible_integer_type_impl <
|
is_compatible_integer_type_impl <
|
||||||
std::is_integral<CompatibleNumberIntegerType>::value and
|
std::is_integral<CompatibleNumberIntegerType>::value and
|
||||||
not std::is_same<bool, CompatibleNumberIntegerType>::value,
|
not std::is_same<bool, CompatibleNumberIntegerType>::value,
|
||||||
RealIntegerType, CompatibleNumberIntegerType > ::value;
|
RealIntegerType, CompatibleNumberIntegerType > ::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 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 BasicJsonType, typename T>
|
template<typename BasicJsonType, typename T>
|
||||||
struct has_from_json
|
struct has_from_json
|
||||||
|
@ -506,34 +520,10 @@ struct has_to_json
|
||||||
std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
|
std::declval<typename BasicJsonType::template json_serializer<T, void>>()))>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// overloads for basic_json template parameters
|
|
||||||
|
|
||||||
template <typename BasicJsonType, typename ArithmeticType,
|
/////////////
|
||||||
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
|
// to_json //
|
||||||
not std::is_same<ArithmeticType,
|
/////////////
|
||||||
typename BasicJsonType::boolean_t>::value,
|
|
||||||
int> = 0>
|
|
||||||
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 BasicJsonType::number_unsigned_t*>());
|
|
||||||
break;
|
|
||||||
case value_t::number_integer:
|
|
||||||
val = static_cast<ArithmeticType>(
|
|
||||||
*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 BasicJsonType::number_float_t*>());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
JSON_THROW(
|
|
||||||
std::domain_error("type must be number, but is " + j.type_name()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
||||||
|
@ -557,7 +547,6 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
|
||||||
external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
|
external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
||||||
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
|
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
|
||||||
|
@ -605,6 +594,47 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr)
|
||||||
external_constructor<value_t::object>::construct(j, arr);
|
external_constructor<value_t::object>::construct(j, arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
// from_json //
|
||||||
|
///////////////
|
||||||
|
|
||||||
|
// overloads for basic_json template parameters
|
||||||
|
template<typename BasicJsonType, typename ArithmeticType,
|
||||||
|
enable_if_t<std::is_arithmetic<ArithmeticType>::value and
|
||||||
|
not std::is_same<ArithmeticType,
|
||||||
|
typename BasicJsonType::boolean_t>::value,
|
||||||
|
int> = 0>
|
||||||
|
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 BasicJsonType::number_unsigned_t*>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case value_t::number_integer:
|
||||||
|
{
|
||||||
|
val = static_cast<ArithmeticType>(
|
||||||
|
*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 BasicJsonType::number_float_t*>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
JSON_THROW(
|
||||||
|
std::domain_error("type must be number, but is " + j.type_name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
||||||
{
|
{
|
||||||
|
@ -662,7 +692,8 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
|
||||||
arr = *j.template get_ptr<const typename BasicJsonType::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 BasicJsonType, typename T, typename Allocator>
|
template<typename BasicJsonType, typename T, typename Allocator>
|
||||||
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
||||||
{
|
{
|
||||||
|
@ -691,11 +722,11 @@ void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri
|
||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
std::transform(
|
std::transform(j.begin(), j.end(),
|
||||||
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
||||||
{
|
{
|
||||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
||||||
// value_type is BasicJsonType
|
// method when value_type is BasicJsonType
|
||||||
return i.template get<typename CompatibleArrayType::value_type>();
|
return i.template get<typename CompatibleArrayType::value_type>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -713,24 +744,22 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri
|
||||||
std::transform(
|
std::transform(
|
||||||
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i)
|
||||||
{
|
{
|
||||||
// get<BasicJsonType>() returns *this, this won't call a from_json method when
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
||||||
// value_type is BasicJsonType
|
// method when value_type is BasicJsonType
|
||||||
return i.template get<typename CompatibleArrayType::value_type>();
|
return i.template get<typename CompatibleArrayType::value_type>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template<typename BasicJsonType, typename CompatibleArrayType,
|
||||||
typename BasicJsonType, typename CompatibleArrayType,
|
|
||||||
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
|
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
|
||||||
not std::is_same<typename BasicJsonType::array_t,
|
not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value, int> = 0>
|
||||||
CompatibleArrayType>::value,
|
|
||||||
int> = 0 >
|
|
||||||
void from_json(const BasicJsonType& 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 " + j.type_name()));
|
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// when T == BasicJsonType, 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, BasicJsonType>::value)
|
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
|
||||||
{
|
{
|
||||||
|
@ -742,11 +771,8 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
|
||||||
from_json_array_impl(j, arr, priority_tag<1> {});
|
from_json_array_impl(j, arr, priority_tag<1> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType, typename CompatibleObjectType,
|
||||||
template <
|
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
|
||||||
typename BasicJsonType, typename CompatibleObjectType,
|
|
||||||
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
|
|
||||||
int> = 0 >
|
|
||||||
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
|
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
|
||||||
{
|
{
|
||||||
if (!j.is_object())
|
if (!j.is_object())
|
||||||
|
@ -766,16 +792,12 @@ void from_json(const BasicJsonType& 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 BasicJsonType, 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 BasicJsonType::number_unsigned_t>::value and
|
||||||
typename BasicJsonType::number_unsigned_t>::value and
|
not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
|
||||||
not std::is_same<ArithmeticType,
|
not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
|
||||||
typename BasicJsonType::number_integer_t>::value and
|
|
||||||
not std::is_same<ArithmeticType,
|
|
||||||
typename BasicJsonType::number_float_t>::value and
|
|
||||||
not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
||||||
int> = 0>
|
int> = 0>
|
||||||
void from_json(const BasicJsonType& j, ArithmeticType& val)
|
void from_json(const BasicJsonType& j, ArithmeticType& val)
|
||||||
|
@ -783,34 +805,27 @@ 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 BasicJsonType::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 BasicJsonType::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 BasicJsonType::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 BasicJsonType::boolean_t*>());
|
||||||
*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
JSON_THROW(
|
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
|
||||||
std::domain_error("type must be number, but is " + j.type_name()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct to_json_fn
|
struct to_json_fn
|
||||||
{
|
{
|
||||||
template<typename BasicJsonType, typename T>
|
template<typename BasicJsonType, typename T>
|
||||||
auto call(BasicJsonType& 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)), void())
|
||||||
-> decltype(to_json(j, std::forward<T>(val)),
|
|
||||||
void())
|
|
||||||
{
|
{
|
||||||
return to_json(j, std::forward<T>(val));
|
return to_json(j, std::forward<T>(val));
|
||||||
}
|
}
|
||||||
|
@ -866,6 +881,7 @@ struct static_const
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T static_const<T>::value;
|
constexpr T static_const<T>::value;
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief helper class to create locales with decimal point
|
@brief helper class to create locales with decimal point
|
||||||
|
|
||||||
|
@ -885,7 +901,8 @@ struct DecimalSeparator : std::numpunct<char>
|
||||||
return '.';
|
return '.';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
} // namespace detail
|
||||||
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -912,6 +929,7 @@ struct adl_serializer
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief a class to store JSON values
|
@brief a class to store JSON values
|
||||||
|
|
||||||
|
@ -5714,6 +5732,13 @@ class basic_json
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
//////////////////////////////////////////
|
||||||
|
// lexicographical comparison operators //
|
||||||
|
//////////////////////////////////////////
|
||||||
|
|
||||||
|
/// @name lexicographical comparison operators
|
||||||
|
/// @{
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief comparison: equal
|
@brief comparison: equal
|
||||||
|
|
||||||
|
@ -9945,7 +9970,7 @@ class basic_json
|
||||||
for (; curptr < m_cursor; curptr++)
|
for (; curptr < m_cursor; curptr++)
|
||||||
{
|
{
|
||||||
// quickly skip tests if a digit
|
// quickly skip tests if a digit
|
||||||
if (*curptr < '0' || *curptr > '9')
|
if (*curptr < '0' or* curptr > '9')
|
||||||
{
|
{
|
||||||
if (*curptr == '.')
|
if (*curptr == '.')
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue