1 #pragma once 2 3 #include <type_traits> 4 #include <utility> 5 6 #include <nlohmann/detail/conversions/from_json.hpp> 7 #include <nlohmann/detail/conversions/to_json.hpp> 8 #include <nlohmann/detail/meta/identity_tag.hpp> 9 #include <nlohmann/detail/meta/type_traits.hpp> 10 11 namespace nlohmann 12 { 13 14 template<typename ValueType, typename> 15 struct adl_serializer 16 { 17 /*! 18 @brief convert a JSON value to any value type 19 20 This function is usually called by the `get()` function of the 21 @ref basic_json class (either explicit or via conversion operators). 22 23 @note This function is chosen for default-constructible value types. 24 25 @param[in] j JSON value to read from 26 @param[in,out] val value to write to 27 */ 28 template<typename BasicJsonType, typename TargetType = ValueType> from_jsonnlohmann::adl_serializer29 static auto from_json(BasicJsonType && j, TargetType& val) noexcept( 30 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val))) 31 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void()) 32 { 33 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val); 34 } 35 36 /*! 37 @brief convert a JSON value to any value type 38 39 This function is usually called by the `get()` function of the 40 @ref basic_json class (either explicit or via conversion operators). 41 42 @note This function is chosen for value types which are not default-constructible. 43 44 @param[in] j JSON value to read from 45 46 @return copy of the JSON value, converted to @a ValueType 47 */ 48 template<typename BasicJsonType, typename TargetType = ValueType> from_jsonnlohmann::adl_serializer49 static auto from_json(BasicJsonType && j) noexcept( 50 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))) 51 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})) 52 { 53 return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}); 54 } 55 56 /*! 57 @brief convert any value type to a JSON value 58 59 This function is usually called by the constructors of the @ref basic_json 60 class. 61 62 @param[in,out] j JSON value to write to 63 @param[in] val value to read from 64 */ 65 template<typename BasicJsonType, typename TargetType = ValueType> to_jsonnlohmann::adl_serializer66 static auto to_json(BasicJsonType& j, TargetType && val) noexcept( 67 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val)))) 68 -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void()) 69 { 70 ::nlohmann::to_json(j, std::forward<TargetType>(val)); 71 } 72 }; 73 } // namespace nlohmann 74