adl_serializer.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <utility>
  10. #include <nlohmann/detail/abi_macros.hpp>
  11. #include <nlohmann/detail/conversions/from_json.hpp>
  12. #include <nlohmann/detail/conversions/to_json.hpp>
  13. #include <nlohmann/detail/meta/identity_tag.hpp>
  14. NLOHMANN_JSON_NAMESPACE_BEGIN
  15. /// @sa https://json.nlohmann.me/api/adl_serializer/
  16. template<typename ValueType, typename>
  17. struct adl_serializer
  18. {
  19. /// @brief convert a JSON value to any value type
  20. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  21. template<typename BasicJsonType, typename TargetType = ValueType>
  22. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  23. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  24. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  25. {
  26. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  27. }
  28. /// @brief convert a JSON value to any value type
  29. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  30. template<typename BasicJsonType, typename TargetType = ValueType>
  31. static auto from_json(BasicJsonType && j) noexcept(
  32. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  33. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  34. {
  35. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  36. }
  37. /// @brief convert any value type to a JSON value
  38. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  39. template<typename BasicJsonType, typename TargetType = ValueType>
  40. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  41. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  42. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  43. {
  44. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  45. }
  46. };
  47. NLOHMANN_JSON_NAMESPACE_END