From c0a8b45bbb4c9d857f1672a6199c1e8659ea9cd2 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sat, 9 Jan 2021 17:45:56 +0100 Subject: [PATCH] Renamed template parameter and added some comments. --- include/nlohmann/adl_serializer.hpp | 36 ++++++++++++------ .../nlohmann/detail/conversions/from_json.hpp | 2 + single_include/nlohmann/json.hpp | 38 +++++++++++++------ 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 8200c2809..9eb751b70 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -19,23 +19,37 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). + @note This function is chosen for value types which can be default constructed. + @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType && j, U& val) noexcept( + template + static auto from_json(BasicJsonType && j, TargetType& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } - template + /*! + @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). + + @note This function is chosen for value types which can not be default constructed. + + @param[in] j JSON value to read from + + @return copy of the JSON value, converted to @a ValueType + */ + template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::tag {}); } /*! @@ -47,12 +61,12 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, U && val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, TargetType && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; } // namespace nlohmann diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 557faa560..c9b88ce5b 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -462,6 +462,8 @@ struct from_json_fn return from_json(j, val); } + // overload to pass calls to built-in from_json functions for non-default constructible STL + // types (e.g. std::array, where X is not default constructible). template auto operator()(const BasicJsonType& j, detail::tag t) const noexcept(noexcept(from_json(j, t))) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fd2811503..06f7caa37 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3959,6 +3959,8 @@ struct from_json_fn return from_json(j, val); } + // overload to pass calls to built-in from_json functions for non-default constructible STL + // types (e.g. std::array, where X is not default constructible). template auto operator()(const BasicJsonType& j, detail::tag t) const noexcept(noexcept(from_json(j, t))) @@ -4553,23 +4555,37 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). + @note This function is chosen for value types which can be default constructed. + @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType && j, U& val) noexcept( + template + static auto from_json(BasicJsonType && j, TargetType& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } - template + /*! + @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). + + @note This function is chosen for value types which can not be default constructed. + + @param[in] j JSON value to read from + + @return copy of the JSON value, converted to @a ValueType + */ + template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::tag {}); } /*! @@ -4581,12 +4597,12 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, U && val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, TargetType && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; } // namespace nlohmann