| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * Copyright 2022 Craig Edwards and D++ contributors |
| 6 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | ************************************************************************************/ |
| 21 | |
| 22 | #pragma once |
| 23 | #include <dpp/export.h> |
| 24 | #include <dpp/json.h> |
| 25 | |
| 26 | namespace dpp { |
| 27 | |
| 28 | /** |
| 29 | * @brief Represents an interface for an object that can optionally implement functions |
| 30 | * for converting to and from nlohmann::json. The methods are only present if the actual object |
| 31 | * also has those methods. |
| 32 | * |
| 33 | * @tparam T Type of class that implements the interface |
| 34 | */ |
| 35 | template<typename T> |
| 36 | struct json_interface { |
| 37 | /** |
| 38 | * @brief Convert object from nlohmann::json |
| 39 | * |
| 40 | * @param j nlohmann::json object |
| 41 | * @return T& Reference to self for fluent calling |
| 42 | */ |
| 43 | template <typename U = T, typename = decltype(std::declval<U&>().fill_from_json_impl(std::declval<nlohmann::json*>()))> |
| 44 | T& fill_from_json(nlohmann::json* j) { |
| 45 | return static_cast<T*>(this)->fill_from_json_impl(j); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @brief Convert object to nlohmann::json |
| 50 | * |
| 51 | * @param with_id Whether to include the ID or not |
| 52 | * @note Some fields are conditionally filled, do not rely on all fields being present |
| 53 | * @return json Json built from the structure |
| 54 | */ |
| 55 | template <typename U = T, typename = decltype(std::declval<U&>().to_json_impl(bool{}))> |
| 56 | auto to_json(bool with_id = false) const { |
| 57 | return static_cast<const T*>(this)->to_json_impl(with_id); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @brief Convert object to json string |
| 62 | * |
| 63 | * @param with_id Whether to include the ID or not |
| 64 | * @note Some fields are conditionally filled, do not rely on all fields being present |
| 65 | * @return std::string Json built from the structure |
| 66 | */ |
| 67 | template <typename U = T, typename = decltype(std::declval<U&>().to_json_impl(bool{}))> |
| 68 | std::string build_json(bool with_id = false) const { |
| 69 | return to_json(with_id).dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } |