irccd  3.0.3
json_util.hpp
1 /*
2  * json_util.hpp -- utilities for JSON
3  *
4  * Copyright (c) 2018-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_JSON_UTIL_HPP
20 #define IRCCD_JSON_UTIL_HPP
21 
27 #include <cstdint>
28 #include <optional>
29 #include <string>
30 
31 #include <json.hpp>
32 
33 namespace irccd {
34 
38 namespace json_util {
39 
62 template <typename T>
63 struct type_traits;
64 
68 template <>
69 struct type_traits<bool> {
76  static auto get(const nlohmann::json& value) noexcept -> std::optional<bool>;
77 };
78 
82 template <>
83 struct type_traits<double> {
90  static auto get(const nlohmann::json& value) noexcept -> std::optional<double>;
91 };
92 
96 template <>
97 struct type_traits<std::string> {
104  static auto get(const nlohmann::json& value) -> std::optional<std::string>;
105 };
106 
110 template <>
111 struct type_traits<std::int8_t> {
118  static auto get(const nlohmann::json& value) -> std::optional<std::int8_t>;
119 };
120 
124 template <>
125 struct type_traits<std::int16_t> {
132  static auto get(const nlohmann::json& value) -> std::optional<std::int16_t>;
133 };
134 
138 template <>
139 struct type_traits<std::int32_t> {
146  static auto get(const nlohmann::json& value) -> std::optional<std::int32_t>;
147 };
148 
152 template <>
153 struct type_traits<std::int64_t> {
160  static auto get(const nlohmann::json& value) noexcept -> std::optional<std::int64_t>;
161 };
162 
166 template <>
167 struct type_traits<std::uint8_t> {
174  static auto get(const nlohmann::json& value) -> std::optional<std::uint8_t>;
175 };
176 
180 template <>
181 struct type_traits<std::uint16_t> {
188  static auto get(const nlohmann::json& value) -> std::optional<std::uint16_t>;
189 };
190 
194 template <>
195 struct type_traits<std::uint32_t> {
202  static auto get(const nlohmann::json& value) -> std::optional<std::uint32_t>;
203 };
204 
208 template <>
209 struct type_traits<std::uint64_t> {
216  static auto get(const nlohmann::json& value) noexcept -> std::optional<std::uint64_t>;
217 };
218 
225 class deserializer : public nlohmann::json {
226 public:
232  deserializer(const nlohmann::json& obj);
233 
240  template <typename Type>
241  auto get(const std::string& key) const noexcept -> std::optional<Type>
242  {
243  const auto it = find(key);
244 
245  if (it == end())
246  return std::nullopt;
247 
248  return type_traits<Type>::get(*it);
249  }
250 
261  template <typename Type, typename DefaultValue>
262  auto optional(const std::string& key, DefaultValue&& def) const noexcept -> std::optional<Type>
263  {
264  const auto it = find(key);
265 
266  if (it == end())
267  return std::optional<Type>(std::forward<DefaultValue>(def));
268 
269  return type_traits<Type>::get(*it);
270  }
271 };
272 
281 auto pretty(const nlohmann::json& value, int indent = 4) -> std::string;
282 
290 auto contains(const nlohmann::json& array, const nlohmann::json& value) noexcept -> bool;
291 
292 } // !json_util
293 
294 } // !irccd
295 
296 #endif // !IRCCD_JSON_UTIL_HPP
irccd::json_util::pretty
auto pretty(const nlohmann::json &value, int indent=4) -> std::string
irccd::json_util::deserializer::get
auto get(const std::string &key) const noexcept -> std::optional< Type >
Definition: json_util.hpp:241
irccd::json_util::type_traits
Describe how to convert a JSON value.
Definition: json_util.hpp:63
irccd::json_util::contains
auto contains(const nlohmann::json &array, const nlohmann::json &value) noexcept -> bool
irccd::json_util::deserializer::deserializer
deserializer(const nlohmann::json &obj)
irccd::json_util::deserializer
Convenient JSON object parser.
Definition: json_util.hpp:225
irccd
Parent namespace.
Definition: acceptor.hpp:43
std
Definition: bot.hpp:253
irccd::json_util::deserializer::optional
auto optional(const std::string &key, DefaultValue &&def) const noexcept -> std::optional< Type >
Definition: json_util.hpp:262