diff --git a/src/json.hpp b/src/json.hpp index 7f17a555..5987b385 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -107,12 +107,15 @@ SOFTWARE. */ namespace nlohmann { -// TODO update this doc + /*! @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 { /////////////////////////// @@ -122,22 +125,24 @@ namespace detail /*! @brief the JSON type enumeration -This enumeration collects the different JSON types. It is internally used -to distinguish the stored values, and the functions @ref basic_json::is_null(), @ref -basic_json::is_object(), @ref basic_json::is_array(), @ref basic_json::is_string(), @ref basic_json::is_boolean(), @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 +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@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. -@note There are three enumeration entries (number_integer, -number_unsigned, and number_float), because the library distinguishes -these three types for numbers: @ref basic_json::number_unsigned_t is used for unsigned -integers, @ref basic_json::number_integer_t is used for signed integers, and @ref -basic_json::number_float_t is used for floating-point numbers or to approximate -integers which do not fit in the limits of their respective type. +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +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 -the default value for a given type +@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type @since version 1.0.0 */ @@ -154,12 +159,6 @@ enum class value_t : uint8_t discarded ///< discarded by the the parser callback function }; -////////////////////////////////////////// -// lexicographical comparison operators // -////////////////////////////////////////// - -/// @name lexicographical comparison operators -/// @{ /*! @brief comparison operator for JSON types @@ -193,51 +192,62 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept order[static_cast(rhs)]; } + +///////////// +// helpers // +///////////// + // alias templates to reduce boilerplate -template +template using enable_if_t = typename std::enable_if::type; -template +template using uncvref_t = typename std::remove_cv::type>::type; // Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait -template +template using is_unscoped_enum = std::integral_constant::value and std::is_enum::value>; -// 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::value and has_value_type::value -// will not compile when T = void (on MSVC at least) -// Whereas conjunction>, has_value_type>::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) -// Only use it when you have too (see example ahead) -template struct conjunction : std::true_type {}; -template struct conjunction : B1 {}; -template -struct conjunction -: std::conditional, B1>::type {}; +/* +Implementation of 2 C++17 constructs: conjunction, negation. This is needed +to avoid evaluating all the traits in a condition -template struct negation : std::integral_constant < bool, !B::value > {}; +For example: not std::is_same::value and has_value_type::value +will not compile when T = void (on MSVC at least). Whereas +conjunction>, has_value_type>::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). Only use it when you have to (see example ahead). +*/ +template struct conjunction : std::true_type {}; +template struct conjunction : B1 {}; +template +struct conjunction : std::conditional, B1>::type {}; + +template struct negation : std::integral_constant < bool, !B::value > {}; // dispatch utility (taken from ranges-v3) -template struct priority_tag : priority_tag < N - 1 > {}; +template 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. -// I'm sure there is a better way, but this might need a big basic_json refactoring -template struct external_constructor; +// I'm sure there is a better way, but this might need a big basic_json +// refactoring +template struct external_constructor; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { j.m_type = value_t::boolean; @@ -246,10 +256,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s) { j.m_type = value_t::string; @@ -258,15 +268,17 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept { // replace infinity and NAN by null if (not std::isfinite(val)) + { j = BasicJsonType{}; + } else { j.m_type = value_t::number_float; @@ -276,10 +288,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept { j.m_type = value_t::number_unsigned; @@ -288,10 +300,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept { j.m_type = value_t::number_integer; @@ -300,10 +312,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr) { j.m_type = value_t::array; @@ -311,25 +323,24 @@ struct external_constructor j.assert_invariant(); } - template ::value, - int> = 0> + template::value, + int> = 0> static void construct(BasicJsonType& j, const CompatibleArrayType& arr) { using std::begin; using std::end; j.m_type = value_t::array; - j.m_value.array = - j.template create(begin(arr), end(arr)); + j.m_value.array = j.template create(begin(arr), end(arr)); j.assert_invariant(); } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj) { j.m_type = value_t::object; @@ -337,22 +348,26 @@ struct external_constructor j.assert_invariant(); } - template ::value, - int> = 0> + template::value, + int> = 0> static void construct(BasicJsonType& j, const CompatibleObjectType& obj) { using std::begin; using std::end; j.m_type = value_t::object; - j.m_value.object = - j.template create(begin(obj), end(obj)); + j.m_value.object = j.template create(begin(obj), end(obj)); j.assert_invariant(); } }; + +//////////////////////// +// has_/is_ functions // +//////////////////////// + /*! @brief Helper to determine whether there's a key_type for T. @@ -364,9 +379,9 @@ contains a `mapped_type`, whereas `std::vector` fails the test. @since version 1.0.0, overworked in version 2.0.6 */ #define NLOHMANN_JSON_HAS_HELPER(type) \ - template struct has_##type { \ + template struct has_##type { \ private: \ - template \ + template \ static int detect(U &&); \ static void detect(...); \ public: \ @@ -381,10 +396,10 @@ NLOHMANN_JSON_HAS_HELPER(iterator); #undef NLOHMANN_JSON_HAS_HELPER -template +template struct is_compatible_object_type_impl : std::false_type {}; -template +template struct is_compatible_object_type_impl { static constexpr auto value = @@ -404,7 +419,7 @@ struct is_compatible_object_type typename BasicJsonType::object_t, CompatibleObjectType >::value; }; -template +template struct is_basic_json_nested_type { static auto constexpr value = std::is_same::value or @@ -414,11 +429,9 @@ struct is_basic_json_nested_type std::is_same::value; }; -template +template struct is_compatible_array_type { - // TODO concept Container? - // this might not make VS happy static auto constexpr value = conjunction>, negation>::value; }; -template +template struct is_compatible_integer_type_impl : std::false_type {}; -template +template struct is_compatible_integer_type_impl { // is there an assert somewhere on overflows? @@ -447,24 +460,25 @@ struct is_compatible_integer_type_impl +template struct is_compatible_integer_type { - static constexpr auto - value = is_compatible_integer_type_impl < - std::is_integral::value and - not std::is_same::value, - RealIntegerType, CompatibleNumberIntegerType > ::value; + static constexpr auto value = + is_compatible_integer_type_impl < + std::is_integral::value and + not std::is_same::value, + RealIntegerType, CompatibleNumberIntegerType > ::value; }; + // This trait checks if JSONSerializer::from_json(json const&, udt&) exists -template +template struct has_from_json { private: // also check the return type of from_json - template ::from_json( - std::declval(), std::declval()))>::value>> + template::from_json( + std::declval(), std::declval()))>::value>> static int detect(U&&); static void detect(...); @@ -475,7 +489,7 @@ struct has_from_json // This trait checks if JSONSerializer::from_json(json const&) exists // this overload is used for non-default-constructible user-defined-types -template +template struct has_non_default_from_json { private: @@ -492,12 +506,12 @@ struct has_non_default_from_json }; // This trait checks if BasicJsonType::json_serializer::to_json exists -template +template struct has_to_json { private: - template ::to_json( - std::declval(), std::declval()))> + template::to_json( + std::declval(), std::declval()))> static int detect(U&&); static void detect(...); @@ -506,58 +520,33 @@ struct has_to_json std::declval>()))>::value; }; -// overloads for basic_json template parameters -template ::value and - not std::is_same::value, - int> = 0> -void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) -{ - switch (static_cast(j)) - { - case value_t::number_unsigned: - val = static_cast( - *j.template get_ptr()); - break; - case value_t::number_integer: - val = static_cast( - *j.template get_ptr()); - break; - case value_t::number_float: - val = static_cast( - *j.template get_ptr()); - break; - default: - JSON_THROW( - std::domain_error("type must be number, but is " + j.type_name())); - } -} +///////////// +// to_json // +///////////// -template +template void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { external_constructor::construct(j, b); } -template ::value, - int> = 0> +template::value, + int> = 0> void to_json(BasicJsonType& j, const CompatibleString& s) { external_constructor::construct(j, s); } -template ::value, int> = 0> +template::value, int> = 0> void to_json(BasicJsonType& j, FloatType val) noexcept { external_constructor::construct(j, static_cast(val)); } - template < typename BasicJsonType, typename CompatibleNumberUnsignedType, enable_if_t::construct(j, static_cast(val)); } -template ::value, int> = 0> +template::value, int> = 0> void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept { external_constructor::construct(j, e); @@ -605,7 +594,48 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr) external_constructor::construct(j, arr); } -template + +/////////////// +// from_json // +/////////////// + +// overloads for basic_json template parameters +template::value and + not std::is_same::value, + int> = 0> +void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +{ + switch (static_cast(j)) + { + case value_t::number_unsigned: + { + val = static_cast( + *j.template get_ptr()); + break; + } + case value_t::number_integer: + { + val = static_cast( + *j.template get_ptr()); + break; + } + case value_t::number_float: + { + val = static_cast( + *j.template get_ptr()); + break; + } + default: + { + JSON_THROW( + std::domain_error("type must be number, but is " + j.type_name())); + } + } +} + +template void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (!j.is_boolean()) @@ -615,7 +645,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) b = *j.template get_ptr(); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (!j.is_string()) @@ -625,35 +655,35 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) s = *j.template get_ptr(); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) { get_arithmetic_value(j, val); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) { get_arithmetic_value(j, val); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) { get_arithmetic_value(j, val); } -template ::value, int> = 0> -void from_json(const BasicJsonType& j, UnscopedEnumType& e) +template::value, int> = 0> +void from_json(const BasicJsonType& j, UnscopedEnumType& e) { typename std::underlying_type::type val = e; get_arithmetic_value(j, val); e = static_cast(val); } -template -void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) +template +void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) { if (!j.is_array()) { @@ -662,8 +692,9 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) arr = *j.template get_ptr(); } -// forward_list doesn't have an insert method, TODO find a way to avoid including forward_list -template +// forward_list doesn't have an insert method +// TODO find a way to avoid including forward_list +template void from_json(const BasicJsonType& j, std::forward_list& l) { // do not perform the check when user wants to retrieve jsons @@ -685,23 +716,23 @@ void from_json(const BasicJsonType& j, std::forward_list& l) } } -template -void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>) +template +void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>) { using std::begin; using std::end; - std::transform( - j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i) + std::transform(j.begin(), j.end(), + std::inserter(arr, end(arr)), [](const BasicJsonType & i) { - // get() returns *this, this won't call a from_json method when - // value_type is BasicJsonType + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType return i.template get(); }); } -template -auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>) +template +auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>) -> decltype( arr.reserve(std::declval()), void()) @@ -711,26 +742,24 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri arr.reserve(j.size()); 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() returns *this, this won't call a from_json method when - // value_type is BasicJsonType + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType return i.template get(); }); } -template < - typename BasicJsonType, typename CompatibleArrayType, - enable_if_t::value and - not std::is_same::value, - int> = 0 > -void from_json(const BasicJsonType& j, CompatibleArrayType& arr) +template::value and + not std::is_same::value, int> = 0> +void from_json(const BasicJsonType& j, CompatibleArrayType& arr) { if (j.is_null()) { 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 if (not std::is_same::value) { @@ -742,12 +771,9 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr) from_json_array_impl(j, arr, priority_tag<1> {}); } - -template < - typename BasicJsonType, typename CompatibleObjectType, - enable_if_t::value, - int> = 0 > -void from_json(const BasicJsonType& j, CompatibleObjectType& obj) +template::value, int> = 0> +void from_json(const BasicJsonType& j, CompatibleObjectType& obj) { if (!j.is_object()) { @@ -766,63 +792,52 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj) // // note: Is it really necessary to provide explicit overloads for boolean_t etc.. // in case of a custom BooleanType which is not an arithmetic type? -template < - typename BasicJsonType, typename ArithmeticType, - enable_if_t < - std::is_arithmetic::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value, - int > = 0 > +template::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value, + int> = 0> void from_json(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) { case value_t::number_unsigned: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::number_integer: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::number_float: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::boolean: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; default: - JSON_THROW( - std::domain_error("type must be number, but is " + j.type_name())); + JSON_THROW(std::domain_error("type must be number, but is " + j.type_name())); } } struct to_json_fn { - template - auto call(BasicJsonType& j, T&& val, priority_tag<1>) const - noexcept(noexcept(to_json(j, std::forward(val)))) - -> decltype(to_json(j, std::forward(val)), - void()) + template + auto call(BasicJsonType& j, T&& val, priority_tag<1>) const noexcept(noexcept(to_json(j, std::forward(val)))) + -> decltype(to_json(j, std::forward(val)), void()) { return to_json(j, std::forward(val)); } - template + template void call(BasicJsonType&, T&&, priority_tag<0>) const noexcept { static_assert(sizeof(BasicJsonType) == 0, "to_json method in T's namespace can not be called"); } public: - template + template void operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(std::declval().call(j, std::forward(val), priority_tag<1> {}))) { @@ -833,23 +848,23 @@ struct to_json_fn struct from_json_fn { private: - template - auto call(const BasicJsonType& j, T& val, priority_tag<1>) const + template + auto call(const BasicJsonType& j, T& val, priority_tag<1>) const noexcept(noexcept(from_json(j, val))) -> decltype(from_json(j, val), void()) { return from_json(j, val); } - template + template void call(const BasicJsonType&, T&, priority_tag<0>) const noexcept { static_assert(sizeof(BasicJsonType) == 0, "from_json method in T's namespace can not be called"); } public: - template - void operator()(const BasicJsonType& j, T& val) const + template + void operator()(const BasicJsonType& j, T& val) const noexcept(noexcept(std::declval().call(j, val, priority_tag<1> {}))) { return call(j, val, priority_tag<1> {}); @@ -857,15 +872,16 @@ struct from_json_fn }; // taken from ranges-v3 -template +template struct static_const { static constexpr T value{}; }; -template +template constexpr T static_const::value; + /*! @brief helper class to create locales with decimal point @@ -885,7 +901,8 @@ struct DecimalSeparator : std::numpunct return '.'; } }; -} +} // namespace detail + namespace { @@ -895,16 +912,16 @@ constexpr const auto& from_json = detail::static_const::va // default JSONSerializer template argument, doesn't care about template argument // will use ADL for serialization -template +template struct adl_serializer { - template + template static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward(j), val))) { ::nlohmann::from_json(std::forward(j), val); } - template + template static void to_json(BasicJsonType& j, T&& val) noexcept( noexcept(::nlohmann::to_json(j, std::forward(val)))) { @@ -912,6 +929,7 @@ struct adl_serializer } }; + /*! @brief a class to store JSON values @@ -1005,7 +1023,7 @@ template < class basic_json { private: - template friend struct detail::external_constructor; + template friend struct detail::external_constructor; /// workaround type for MSVC using basic_json_t = basic_json class iter_impl; template class json_reverse_iterator; class json_pointer; - template + template using json_serializer = JSONSerializer; ///////////////////// @@ -1904,13 +1922,13 @@ class basic_json @since version 2.1.0 */ - template , - detail::enable_if_t::value and - not std::is_same::value and - not detail::is_basic_json_nested_type< - basic_json_t, U>::value and - detail::has_to_json::value, - int> = 0> + template, + detail::enable_if_t::value and + not std::is_same::value and + not detail::is_basic_json_nested_type< + basic_json_t, U>::value and + detail::has_to_json::value, + int> = 0> basic_json(T && val) noexcept(noexcept(JSONSerializer::to_json( std::declval(), std::forward(val)))) { @@ -5714,6 +5732,13 @@ class basic_json /// @} public: + ////////////////////////////////////////// + // lexicographical comparison operators // + ////////////////////////////////////////// + + /// @name lexicographical comparison operators + /// @{ + /*! @brief comparison: equal @@ -10795,7 +10820,7 @@ basic_json_parser_66: for (; curptr < m_cursor; curptr++) { // quickly skip tests if a digit - if (*curptr < '0' || *curptr > '9') + if (*curptr < '0' or* curptr > '9') { if (*curptr == '.') { diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 8c670c90..0a7f207d 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -107,12 +107,15 @@ SOFTWARE. */ namespace nlohmann { -// TODO update this doc + /*! @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 { /////////////////////////// @@ -122,22 +125,24 @@ namespace detail /*! @brief the JSON type enumeration -This enumeration collects the different JSON types. It is internally used -to distinguish the stored values, and the functions @ref basic_json::is_null(), @ref -basic_json::is_object(), @ref basic_json::is_array(), @ref basic_json::is_string(), @ref basic_json::is_boolean(), @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 +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@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. -@note There are three enumeration entries (number_integer, -number_unsigned, and number_float), because the library distinguishes -these three types for numbers: @ref basic_json::number_unsigned_t is used for unsigned -integers, @ref basic_json::number_integer_t is used for signed integers, and @ref -basic_json::number_float_t is used for floating-point numbers or to approximate -integers which do not fit in the limits of their respective type. +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +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 -the default value for a given type +@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type @since version 1.0.0 */ @@ -154,12 +159,6 @@ enum class value_t : uint8_t discarded ///< discarded by the the parser callback function }; -////////////////////////////////////////// -// lexicographical comparison operators // -////////////////////////////////////////// - -/// @name lexicographical comparison operators -/// @{ /*! @brief comparison operator for JSON types @@ -193,51 +192,62 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept order[static_cast(rhs)]; } + +///////////// +// helpers // +///////////// + // alias templates to reduce boilerplate -template +template using enable_if_t = typename std::enable_if::type; -template +template using uncvref_t = typename std::remove_cv::type>::type; // Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait -template +template using is_unscoped_enum = std::integral_constant::value and std::is_enum::value>; -// 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::value and has_value_type::value -// will not compile when T = void (on MSVC at least) -// Whereas conjunction>, has_value_type>::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) -// Only use it when you have too (see example ahead) -template struct conjunction : std::true_type {}; -template struct conjunction : B1 {}; -template -struct conjunction -: std::conditional, B1>::type {}; +/* +Implementation of 2 C++17 constructs: conjunction, negation. This is needed +to avoid evaluating all the traits in a condition -template struct negation : std::integral_constant < bool, !B::value > {}; +For example: not std::is_same::value and has_value_type::value +will not compile when T = void (on MSVC at least). Whereas +conjunction>, has_value_type>::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). Only use it when you have to (see example ahead). +*/ +template struct conjunction : std::true_type {}; +template struct conjunction : B1 {}; +template +struct conjunction : std::conditional, B1>::type {}; + +template struct negation : std::integral_constant < bool, !B::value > {}; // dispatch utility (taken from ranges-v3) -template struct priority_tag : priority_tag < N - 1 > {}; +template 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. -// I'm sure there is a better way, but this might need a big basic_json refactoring -template struct external_constructor; +// I'm sure there is a better way, but this might need a big basic_json +// refactoring +template struct external_constructor; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { j.m_type = value_t::boolean; @@ -246,10 +256,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s) { j.m_type = value_t::string; @@ -258,15 +268,17 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept { // replace infinity and NAN by null if (not std::isfinite(val)) + { j = BasicJsonType{}; + } else { j.m_type = value_t::number_float; @@ -276,10 +288,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept { j.m_type = value_t::number_unsigned; @@ -288,10 +300,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept { j.m_type = value_t::number_integer; @@ -300,10 +312,10 @@ struct external_constructor } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr) { j.m_type = value_t::array; @@ -311,25 +323,24 @@ struct external_constructor j.assert_invariant(); } - template ::value, - int> = 0> + template::value, + int> = 0> static void construct(BasicJsonType& j, const CompatibleArrayType& arr) { using std::begin; using std::end; j.m_type = value_t::array; - j.m_value.array = - j.template create(begin(arr), end(arr)); + j.m_value.array = j.template create(begin(arr), end(arr)); j.assert_invariant(); } }; -template <> +template<> struct external_constructor { - template + template static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj) { j.m_type = value_t::object; @@ -337,22 +348,26 @@ struct external_constructor j.assert_invariant(); } - template ::value, - int> = 0> + template::value, + int> = 0> static void construct(BasicJsonType& j, const CompatibleObjectType& obj) { using std::begin; using std::end; j.m_type = value_t::object; - j.m_value.object = - j.template create(begin(obj), end(obj)); + j.m_value.object = j.template create(begin(obj), end(obj)); j.assert_invariant(); } }; + +//////////////////////// +// has_/is_ functions // +//////////////////////// + /*! @brief Helper to determine whether there's a key_type for T. @@ -364,9 +379,9 @@ contains a `mapped_type`, whereas `std::vector` fails the test. @since version 1.0.0, overworked in version 2.0.6 */ #define NLOHMANN_JSON_HAS_HELPER(type) \ - template struct has_##type { \ + template struct has_##type { \ private: \ - template \ + template \ static int detect(U &&); \ static void detect(...); \ public: \ @@ -381,10 +396,10 @@ NLOHMANN_JSON_HAS_HELPER(iterator); #undef NLOHMANN_JSON_HAS_HELPER -template +template struct is_compatible_object_type_impl : std::false_type {}; -template +template struct is_compatible_object_type_impl { static constexpr auto value = @@ -404,7 +419,7 @@ struct is_compatible_object_type typename BasicJsonType::object_t, CompatibleObjectType >::value; }; -template +template struct is_basic_json_nested_type { static auto constexpr value = std::is_same::value or @@ -414,11 +429,9 @@ struct is_basic_json_nested_type std::is_same::value; }; -template +template struct is_compatible_array_type { - // TODO concept Container? - // this might not make VS happy static auto constexpr value = conjunction>, negation>::value; }; -template +template struct is_compatible_integer_type_impl : std::false_type {}; -template +template struct is_compatible_integer_type_impl { // is there an assert somewhere on overflows? @@ -447,24 +460,25 @@ struct is_compatible_integer_type_impl +template struct is_compatible_integer_type { - static constexpr auto - value = is_compatible_integer_type_impl < - std::is_integral::value and - not std::is_same::value, - RealIntegerType, CompatibleNumberIntegerType > ::value; + static constexpr auto value = + is_compatible_integer_type_impl < + std::is_integral::value and + not std::is_same::value, + RealIntegerType, CompatibleNumberIntegerType > ::value; }; + // This trait checks if JSONSerializer::from_json(json const&, udt&) exists -template +template struct has_from_json { private: // also check the return type of from_json - template ::from_json( - std::declval(), std::declval()))>::value>> + template::from_json( + std::declval(), std::declval()))>::value>> static int detect(U&&); static void detect(...); @@ -475,7 +489,7 @@ struct has_from_json // This trait checks if JSONSerializer::from_json(json const&) exists // this overload is used for non-default-constructible user-defined-types -template +template struct has_non_default_from_json { private: @@ -492,12 +506,12 @@ struct has_non_default_from_json }; // This trait checks if BasicJsonType::json_serializer::to_json exists -template +template struct has_to_json { private: - template ::to_json( - std::declval(), std::declval()))> + template::to_json( + std::declval(), std::declval()))> static int detect(U&&); static void detect(...); @@ -506,58 +520,33 @@ struct has_to_json std::declval>()))>::value; }; -// overloads for basic_json template parameters -template ::value and - not std::is_same::value, - int> = 0> -void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) -{ - switch (static_cast(j)) - { - case value_t::number_unsigned: - val = static_cast( - *j.template get_ptr()); - break; - case value_t::number_integer: - val = static_cast( - *j.template get_ptr()); - break; - case value_t::number_float: - val = static_cast( - *j.template get_ptr()); - break; - default: - JSON_THROW( - std::domain_error("type must be number, but is " + j.type_name())); - } -} +///////////// +// to_json // +///////////// -template +template void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { external_constructor::construct(j, b); } -template ::value, - int> = 0> +template::value, + int> = 0> void to_json(BasicJsonType& j, const CompatibleString& s) { external_constructor::construct(j, s); } -template ::value, int> = 0> +template::value, int> = 0> void to_json(BasicJsonType& j, FloatType val) noexcept { external_constructor::construct(j, static_cast(val)); } - template < typename BasicJsonType, typename CompatibleNumberUnsignedType, enable_if_t::construct(j, static_cast(val)); } -template ::value, int> = 0> +template::value, int> = 0> void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept { external_constructor::construct(j, e); @@ -605,7 +594,48 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr) external_constructor::construct(j, arr); } -template + +/////////////// +// from_json // +/////////////// + +// overloads for basic_json template parameters +template::value and + not std::is_same::value, + int> = 0> +void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +{ + switch (static_cast(j)) + { + case value_t::number_unsigned: + { + val = static_cast( + *j.template get_ptr()); + break; + } + case value_t::number_integer: + { + val = static_cast( + *j.template get_ptr()); + break; + } + case value_t::number_float: + { + val = static_cast( + *j.template get_ptr()); + break; + } + default: + { + JSON_THROW( + std::domain_error("type must be number, but is " + j.type_name())); + } + } +} + +template void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (!j.is_boolean()) @@ -615,7 +645,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) b = *j.template get_ptr(); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (!j.is_string()) @@ -625,35 +655,35 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) s = *j.template get_ptr(); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) { get_arithmetic_value(j, val); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) { get_arithmetic_value(j, val); } -template +template void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) { get_arithmetic_value(j, val); } -template ::value, int> = 0> -void from_json(const BasicJsonType& j, UnscopedEnumType& e) +template::value, int> = 0> +void from_json(const BasicJsonType& j, UnscopedEnumType& e) { typename std::underlying_type::type val = e; get_arithmetic_value(j, val); e = static_cast(val); } -template -void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) +template +void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) { if (!j.is_array()) { @@ -662,8 +692,9 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) arr = *j.template get_ptr(); } -// forward_list doesn't have an insert method, TODO find a way to avoid including forward_list -template +// forward_list doesn't have an insert method +// TODO find a way to avoid including forward_list +template void from_json(const BasicJsonType& j, std::forward_list& l) { // do not perform the check when user wants to retrieve jsons @@ -685,23 +716,23 @@ void from_json(const BasicJsonType& j, std::forward_list& l) } } -template -void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>) +template +void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0>) { using std::begin; using std::end; - std::transform( - j.begin(), j.end(), std::inserter(arr, end(arr)), [](const BasicJsonType & i) + std::transform(j.begin(), j.end(), + std::inserter(arr, end(arr)), [](const BasicJsonType & i) { - // get() returns *this, this won't call a from_json method when - // value_type is BasicJsonType + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType return i.template get(); }); } -template -auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>) +template +auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1>) -> decltype( arr.reserve(std::declval()), void()) @@ -711,26 +742,24 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, pri arr.reserve(j.size()); 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() returns *this, this won't call a from_json method when - // value_type is BasicJsonType + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType return i.template get(); }); } -template < - typename BasicJsonType, typename CompatibleArrayType, - enable_if_t::value and - not std::is_same::value, - int> = 0 > -void from_json(const BasicJsonType& j, CompatibleArrayType& arr) +template::value and + not std::is_same::value, int> = 0> +void from_json(const BasicJsonType& j, CompatibleArrayType& arr) { if (j.is_null()) { 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 if (not std::is_same::value) { @@ -742,12 +771,9 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr) from_json_array_impl(j, arr, priority_tag<1> {}); } - -template < - typename BasicJsonType, typename CompatibleObjectType, - enable_if_t::value, - int> = 0 > -void from_json(const BasicJsonType& j, CompatibleObjectType& obj) +template::value, int> = 0> +void from_json(const BasicJsonType& j, CompatibleObjectType& obj) { if (!j.is_object()) { @@ -766,63 +792,52 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj) // // note: Is it really necessary to provide explicit overloads for boolean_t etc.. // in case of a custom BooleanType which is not an arithmetic type? -template < - typename BasicJsonType, typename ArithmeticType, - enable_if_t < - std::is_arithmetic::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value, - int > = 0 > +template::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value, + int> = 0> void from_json(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) { case value_t::number_unsigned: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::number_integer: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::number_float: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; case value_t::boolean: - val = static_cast( - *j.template get_ptr()); + val = static_cast(*j.template get_ptr()); break; default: - JSON_THROW( - std::domain_error("type must be number, but is " + j.type_name())); + JSON_THROW(std::domain_error("type must be number, but is " + j.type_name())); } } struct to_json_fn { - template - auto call(BasicJsonType& j, T&& val, priority_tag<1>) const - noexcept(noexcept(to_json(j, std::forward(val)))) - -> decltype(to_json(j, std::forward(val)), - void()) + template + auto call(BasicJsonType& j, T&& val, priority_tag<1>) const noexcept(noexcept(to_json(j, std::forward(val)))) + -> decltype(to_json(j, std::forward(val)), void()) { return to_json(j, std::forward(val)); } - template + template void call(BasicJsonType&, T&&, priority_tag<0>) const noexcept { static_assert(sizeof(BasicJsonType) == 0, "to_json method in T's namespace can not be called"); } public: - template + template void operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(std::declval().call(j, std::forward(val), priority_tag<1> {}))) { @@ -833,23 +848,23 @@ struct to_json_fn struct from_json_fn { private: - template - auto call(const BasicJsonType& j, T& val, priority_tag<1>) const + template + auto call(const BasicJsonType& j, T& val, priority_tag<1>) const noexcept(noexcept(from_json(j, val))) -> decltype(from_json(j, val), void()) { return from_json(j, val); } - template + template void call(const BasicJsonType&, T&, priority_tag<0>) const noexcept { static_assert(sizeof(BasicJsonType) == 0, "from_json method in T's namespace can not be called"); } public: - template - void operator()(const BasicJsonType& j, T& val) const + template + void operator()(const BasicJsonType& j, T& val) const noexcept(noexcept(std::declval().call(j, val, priority_tag<1> {}))) { return call(j, val, priority_tag<1> {}); @@ -857,15 +872,16 @@ struct from_json_fn }; // taken from ranges-v3 -template +template struct static_const { static constexpr T value{}; }; -template +template constexpr T static_const::value; + /*! @brief helper class to create locales with decimal point @@ -885,7 +901,8 @@ struct DecimalSeparator : std::numpunct return '.'; } }; -} +} // namespace detail + namespace { @@ -895,16 +912,16 @@ constexpr const auto& from_json = detail::static_const::va // default JSONSerializer template argument, doesn't care about template argument // will use ADL for serialization -template +template struct adl_serializer { - template + template static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward(j), val))) { ::nlohmann::from_json(std::forward(j), val); } - template + template static void to_json(BasicJsonType& j, T&& val) noexcept( noexcept(::nlohmann::to_json(j, std::forward(val)))) { @@ -912,6 +929,7 @@ struct adl_serializer } }; + /*! @brief a class to store JSON values @@ -1005,7 +1023,7 @@ template < class basic_json { private: - template friend struct detail::external_constructor; + template friend struct detail::external_constructor; /// workaround type for MSVC using basic_json_t = basic_json class iter_impl; template class json_reverse_iterator; class json_pointer; - template + template using json_serializer = JSONSerializer; ///////////////////// @@ -1904,13 +1922,13 @@ class basic_json @since version 2.1.0 */ - template , - detail::enable_if_t::value and - not std::is_same::value and - not detail::is_basic_json_nested_type< - basic_json_t, U>::value and - detail::has_to_json::value, - int> = 0> + template, + detail::enable_if_t::value and + not std::is_same::value and + not detail::is_basic_json_nested_type< + basic_json_t, U>::value and + detail::has_to_json::value, + int> = 0> basic_json(T && val) noexcept(noexcept(JSONSerializer::to_json( std::declval(), std::forward(val)))) { @@ -5714,6 +5732,13 @@ class basic_json /// @} public: + ////////////////////////////////////////// + // lexicographical comparison operators // + ////////////////////////////////////////// + + /// @name lexicographical comparison operators + /// @{ + /*! @brief comparison: equal @@ -9945,7 +9970,7 @@ class basic_json for (; curptr < m_cursor; curptr++) { // quickly skip tests if a digit - if (*curptr < '0' || *curptr > '9') + if (*curptr < '0' or* curptr > '9') { if (*curptr == '.') {