Mercurial > code
diff cpp/json_util/json_util.hpp @ 648:5bd9424a523a
misc: extreme cleanup
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 04 Oct 2018 21:17:55 +0200 |
parents | 0a947aec477c |
children | 3129f59002d2 |
line wrap: on
line diff
--- a/cpp/json_util/json_util.hpp Wed Aug 01 14:08:40 2018 +0200 +++ b/cpp/json_util/json_util.hpp Thu Oct 04 21:17:55 2018 +0200 @@ -47,35 +47,35 @@ template <typename Int> class parser_type_traits_uint : public std::true_type { public: - static boost::optional<Int> get(const nlohmann::json& value) noexcept - { - if (!value.is_number_unsigned()) - return boost::none; + static boost::optional<Int> get(const nlohmann::json& value) noexcept + { + if (!value.is_number_unsigned()) + return boost::none; - const auto ret = value.get<std::uint64_t>(); + const auto ret = value.get<std::uint64_t>(); - if (ret > std::numeric_limits<Int>::max()) - return boost::none; + if (ret > std::numeric_limits<Int>::max()) + return boost::none; - return static_cast<Int>(ret); - } + return static_cast<Int>(ret); + } }; template <typename Int> class parser_type_traits_int : public std::true_type { public: - static boost::optional<Int> get(const nlohmann::json& value) noexcept - { - if (!value.is_number_integer()) - return boost::none; + static boost::optional<Int> get(const nlohmann::json& value) noexcept + { + if (!value.is_number_integer()) + return boost::none; - const auto ret = value.get<std::int64_t>(); + const auto ret = value.get<std::int64_t>(); - if (ret < std::numeric_limits<Int>::min() || ret > std::numeric_limits<Int>::max()) - return boost::none; + if (ret < std::numeric_limits<Int>::min() || ret > std::numeric_limits<Int>::max()) + return boost::none; - return static_cast<Int>(ret); - } + return static_cast<Int>(ret); + } }; } // !detail @@ -116,19 +116,19 @@ template <> class parser_type_traits<bool> : public std::true_type { public: - /** - * Convert the JSON value to bool. - * - * \param value the value - * \return the bool or none if not a boolean type - */ - static boost::optional<bool> get(const nlohmann::json& value) noexcept - { - if (!value.is_boolean()) - return boost::none; + /** + * Convert the JSON value to bool. + * + * \param value the value + * \return the bool or none if not a boolean type + */ + static boost::optional<bool> get(const nlohmann::json& value) noexcept + { + if (!value.is_boolean()) + return boost::none; - return value.get<bool>(); - } + return value.get<bool>(); + } }; /** @@ -137,19 +137,19 @@ template <> class parser_type_traits<double> : public std::true_type { public: - /** - * Convert the JSON value to bool. - * - * \param value the value - * \return the double or none if not a double type - */ - static boost::optional<double> get(const nlohmann::json& value) noexcept - { - if (!value.is_number_float()) - return boost::none; + /** + * Convert the JSON value to bool. + * + * \param value the value + * \return the double or none if not a double type + */ + static boost::optional<double> get(const nlohmann::json& value) noexcept + { + if (!value.is_number_float()) + return boost::none; - return value.get<double>(); - } + return value.get<double>(); + } }; /** @@ -158,19 +158,19 @@ template <> class parser_type_traits<std::string> : public std::true_type { public: - /** - * Convert the JSON value to bool. - * - * \param value the value - * \return the string or none if not a string type - */ - static boost::optional<std::string> get(const nlohmann::json& value) - { - if (!value.is_string()) - return boost::none; + /** + * Convert the JSON value to bool. + * + * \param value the value + * \return the string or none if not a string type + */ + static boost::optional<std::string> get(const nlohmann::json& value) + { + if (!value.is_string()) + return boost::none; - return value.get<std::string>(); - } + return value.get<std::string>(); + } }; /** @@ -200,19 +200,19 @@ template <> class parser_type_traits<std::int64_t> : public std::true_type { public: - /** - * Convert the JSON value to std::int64_t. - * - * \param value the value - * \return the int or none if not a int type - */ - static boost::optional<std::int64_t> get(const nlohmann::json& value) noexcept - { - if (!value.is_number_integer()) - return boost::none; + /** + * Convert the JSON value to std::int64_t. + * + * \param value the value + * \return the int or none if not a int type + */ + static boost::optional<std::int64_t> get(const nlohmann::json& value) noexcept + { + if (!value.is_number_integer()) + return boost::none; - return value.get<std::int64_t>(); - } + return value.get<std::int64_t>(); + } }; /** @@ -242,19 +242,19 @@ template <> class parser_type_traits<std::uint64_t> : public std::true_type { public: - /** - * Convert the JSON value to std::uint64_t. - * - * \param value the value - * \return the int or none if not a int type - */ - static boost::optional<std::uint64_t> get(const nlohmann::json& value) noexcept - { - if (!value.is_number_unsigned()) - return boost::none; + /** + * Convert the JSON value to std::uint64_t. + * + * \param value the value + * \return the int or none if not a int type + */ + static boost::optional<std::uint64_t> get(const nlohmann::json& value) noexcept + { + if (!value.is_number_unsigned()) + return boost::none; - return value.get<std::uint64_t>(); - } + return value.get<std::uint64_t>(); + } }; /** @@ -265,52 +265,52 @@ */ class document : public nlohmann::json { public: - /** - * Inherited constructor. - */ - using nlohmann::json::json; + /** + * Inherited constructor. + */ + using nlohmann::json::json; - /** - * Get a value from the document object. - * - * \param key the property key - * \return the value or boost::none if not found or not convertible - */ - template <typename Type> - inline boost::optional<Type> get(const std::string& key) const noexcept - { - static_assert(parser_type_traits<Type>::value, "type not supported"); + /** + * Get a value from the document object. + * + * \param key the property key + * \return the value or boost::none if not found or not convertible + */ + template <typename Type> + inline boost::optional<Type> get(const std::string& key) const noexcept + { + static_assert(parser_type_traits<Type>::value, "type not supported"); - const auto it = find(key); + const auto it = find(key); - if (it == end()) - return boost::none; + if (it == end()) + return boost::none; - return parser_type_traits<Type>::get(*it); - } + return parser_type_traits<Type>::get(*it); + } - /** - * Get an optional value from the document object. - * - * If the value is undefined, the default value is returned. Otherwise, if - * the value is not in the given type, boost::none is returned. - * - * \param key the property key - * \param def the default value if property is undefined - * \return the value, boost::none or def - */ - template <typename Type, typename DefaultValue> - inline boost::optional<Type> optional(const std::string& key, DefaultValue&& def) const noexcept - { - static_assert(parser_type_traits<Type>::value, "type not supported"); + /** + * Get an optional value from the document object. + * + * If the value is undefined, the default value is returned. Otherwise, if + * the value is not in the given type, boost::none is returned. + * + * \param key the property key + * \param def the default value if property is undefined + * \return the value, boost::none or def + */ + template <typename Type, typename DefaultValue> + inline boost::optional<Type> optional(const std::string& key, DefaultValue&& def) const noexcept + { + static_assert(parser_type_traits<Type>::value, "type not supported"); - const auto it = find(key); + const auto it = find(key); - if (it == end()) - return boost::optional<Type>(std::forward<DefaultValue>(def)); + if (it == end()) + return boost::optional<Type>(std::forward<DefaultValue>(def)); - return parser_type_traits<Type>::get(*it); - } + return parser_type_traits<Type>::get(*it); + } }; /** @@ -323,22 +323,22 @@ */ inline std::string pretty(const nlohmann::json& value, int indent = 4) { - switch (value.type()) { - case nlohmann::json::value_t::null: - return "null"; - case nlohmann::json::value_t::string: - return value.get<std::string>(); - case nlohmann::json::value_t::boolean: - return value.get<bool>() ? "true" : "false"; - case nlohmann::json::value_t::number_integer: - return std::to_string(value.get<std::int64_t>()); - case nlohmann::json::value_t::number_unsigned: - return std::to_string(value.get<std::uint64_t>()); - case nlohmann::json::value_t::number_float: - return std::to_string(value.get<double>()); - default: - return value.dump(indent); - } + switch (value.type()) { + case nlohmann::json::value_t::null: + return "null"; + case nlohmann::json::value_t::string: + return value.get<std::string>(); + case nlohmann::json::value_t::boolean: + return value.get<bool>() ? "true" : "false"; + case nlohmann::json::value_t::number_integer: + return std::to_string(value.get<std::int64_t>()); + case nlohmann::json::value_t::number_unsigned: + return std::to_string(value.get<std::uint64_t>()); + case nlohmann::json::value_t::number_float: + return std::to_string(value.get<double>()); + default: + return value.dump(indent); + } } } // !json_util