diff --git a/src/json.hpp b/src/json.hpp index 06c5683b..6909553a 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) template void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { - if (!j.is_boolean()) + if (not j.is_boolean()) { JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name())); } @@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) template void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { - if (!j.is_string()) + if (not j.is_string()) { JSON_THROW(std::domain_error("type must be string, but is " + j.type_name())); } @@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e) template void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) } if (not std::is_same::value) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr) // when T == BasicJsonType, do not check if value_t is correct if (not std::is_same::value) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -769,7 +769,7 @@ template::value, int> = 0> void from_json(const BasicJsonType& j, CompatibleObjectType& obj) { - if (!j.is_object()) + if (not j.is_object()) { JSON_THROW(std::domain_error("type must be object, but is " + j.type_name())); } @@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) struct to_json_fn { + private: 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()) @@ -887,52 +888,57 @@ struct static_const template constexpr T static_const::value; - - -/*! -@brief helper class to create locales with decimal point - -This struct is used a default locale during the JSON serialization. JSON -requires the decimal point to be `.`, so this function overloads the -`do_decimal_point()` function to return `.`. This function is called by -float-to-string conversions to retrieve the decimal separator between integer -and fractional parts. - -@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315 -@since version 2.0.0 -*/ -struct DecimalSeparator : std::numpunct -{ - char do_decimal_point() const - { - return '.'; - } -}; } // namespace detail +/// namespace to hold default `to_json` / `from_json` functions namespace { constexpr const auto& to_json = detail::static_const::value; constexpr const auto& from_json = detail::static_const::value; } -// default JSONSerializer template argument, doesn't care about template argument -// will use ADL for serialization + +/*! +@brief default JSONSerializer template argument + +This serializer ignores the template arguments and uses ADL +([argument-dependent lookup](http://en.cppreference.com/w/cpp/language/adl)) +for serialization. +*/ template struct adl_serializer { - template - static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward(j), val))) + /*! + @brief convert a JSON value to any value type + + This function is usually called by the `get()` function of the + @ref basic_json class (either explicit or via conversion operators). + + @param[in] j JSON value to read from + @param[in, out] val value to write to + */ + template + static void from_json(BasicJsonType&& j, ValueType& val) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), val))) { ::nlohmann::from_json(std::forward(j), val); } - template - static void to_json(BasicJsonType& j, T&& val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) + /*! + @brief convert any value type to a JSON value + + This function is usually called by the constructors of the @ref basic_json + class. + + @param[in, out] j JSON value to write to + @param[in] val value to read from + */ + template + static void to_json(BasicJsonType& j, ValueType&& val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; @@ -957,7 +963,7 @@ default; will be used in @ref number_float_t) @tparam AllocatorType type of the allocator to use (`std::allocator` by default) @tparam JSONSerializer the serializer to resolve internal calls to `to_json()` -and `from_json()` +and `from_json()` (@ref adl_serializer by default) @requirement The class satisfies the following concept requirements: - Basic diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 0bf3db1d..b1df2bf7 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) template void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { - if (!j.is_boolean()) + if (not j.is_boolean()) { JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name())); } @@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) template void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { - if (!j.is_string()) + if (not j.is_string()) { JSON_THROW(std::domain_error("type must be string, but is " + j.type_name())); } @@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e) template void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) } if (not std::is_same::value) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr) // when T == BasicJsonType, do not check if value_t is correct if (not std::is_same::value) { - if (!j.is_array()) + if (not j.is_array()) { JSON_THROW(std::domain_error("type must be array, but is " + j.type_name())); } @@ -769,7 +769,7 @@ template::value, int> = 0> void from_json(const BasicJsonType& j, CompatibleObjectType& obj) { - if (!j.is_object()) + if (not j.is_object()) { JSON_THROW(std::domain_error("type must be object, but is " + j.type_name())); } @@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) struct to_json_fn { + private: 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()) @@ -887,52 +888,57 @@ struct static_const template constexpr T static_const::value; - - -/*! -@brief helper class to create locales with decimal point - -This struct is used a default locale during the JSON serialization. JSON -requires the decimal point to be `.`, so this function overloads the -`do_decimal_point()` function to return `.`. This function is called by -float-to-string conversions to retrieve the decimal separator between integer -and fractional parts. - -@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315 -@since version 2.0.0 -*/ -struct DecimalSeparator : std::numpunct -{ - char do_decimal_point() const - { - return '.'; - } -}; } // namespace detail +/// namespace to hold default `to_json` / `from_json` functions namespace { constexpr const auto& to_json = detail::static_const::value; constexpr const auto& from_json = detail::static_const::value; } -// default JSONSerializer template argument, doesn't care about template argument -// will use ADL for serialization + +/*! +@brief default JSONSerializer template argument + +This serializer ignores the template arguments and uses ADL +([argument-dependent lookup](http://en.cppreference.com/w/cpp/language/adl)) +for serialization. +*/ template struct adl_serializer { - template - static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward(j), val))) + /*! + @brief convert a JSON value to any value type + + This function is usually called by the `get()` function of the + @ref basic_json class (either explicit or via conversion operators). + + @param[in] j JSON value to read from + @param[in, out] val value to write to + */ + template + static void from_json(BasicJsonType&& j, ValueType& val) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), val))) { ::nlohmann::from_json(std::forward(j), val); } - template - static void to_json(BasicJsonType& j, T&& val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) + /*! + @brief convert any value type to a JSON value + + This function is usually called by the constructors of the @ref basic_json + class. + + @param[in, out] j JSON value to write to + @param[in] val value to read from + */ + template + static void to_json(BasicJsonType& j, ValueType&& val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; @@ -957,7 +963,7 @@ default; will be used in @ref number_float_t) @tparam AllocatorType type of the allocator to use (`std::allocator` by default) @tparam JSONSerializer the serializer to resolve internal calls to `to_json()` -and `from_json()` +and `from_json()` (@ref adl_serializer by default) @requirement The class satisfies the following concept requirements: - Basic