diff cpp/json_util/json_util.hpp @ 630:04dffcdb8e1a

json_util: initial import, closes #776 @1h
author David Demelier <markand@malikania.fr>
date Tue, 20 Mar 2018 13:18:44 +0100
parents
children 337a805b9ce2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cpp/json_util/json_util.hpp	Tue Mar 20 13:18:44 2018 +0100
@@ -0,0 +1,214 @@
+/*
+ * json_util.hpp -- utilities for JSON
+ *
+ * Copyright (c) 2018 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef JSON_UTIL_HPP
+#define JSON_UTIL_HPP
+
+/**
+ * \file json_util.hpp
+ * \brief Utilities for JSON.
+ */
+
+/**
+ * \page Json Json
+ * \brief Utilities for JSON.
+ *
+ * ## Export macros
+ *
+ * You must define `JSON_UTIL_DLL` globally and `JSON_UTIL_BUILDING_DLL` when
+ * compiling the library if you want a DLL, alternatively you can provide your
+ * own `JSON_UTIL_EXPORT` macro instead.
+ */
+
+#include <cstdint>
+#include <string>
+
+#include <boost/optional.hpp>
+
+#include <json.hpp>
+
+/**
+ * \cond JSON_UTIL_HIDDEN_SYMBOLS
+ */
+
+#if !defined(JSON_UTIL_EXPORT)
+#   if defined(JSON_UTIL_DLL)
+#       if defined(_WIN32)
+#           if defined(JSON_UTIL_BUILDING_DLL)
+#               define JSON_UTIL_EXPORT __declspec(dllexport)
+#           else
+#               define JSON_UTIL_EXPORT __declspec(dllimport)
+#           endif
+#       else
+#           define JSON_UTIL_EXPORT
+#       endif
+#   else
+#       define JSON_UTIL_EXPORT
+#   endif
+#endif
+
+/**
+ * \endcond
+ */
+
+/**
+ * \brief Utilities for JSON.
+ */
+namespace json_util {
+
+/**
+ * Get a JSON value from the given object or array.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \return the value or boost::none if not found
+ */
+JSON_UTIL_EXPORT
+boost::optional<nlohmann::json> get(const nlohmann::json& json,
+                                    const nlohmann::json::json_pointer& key) noexcept;
+
+/**
+ * Get a bool or null if not found or invalid.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \return the value or boost::none if not found or invalid
+ */
+JSON_UTIL_EXPORT
+boost::optional<bool> get_bool(const nlohmann::json& json,
+                               const nlohmann::json::json_pointer& key) noexcept;
+
+/**
+ * Get a 64 bit signed integer or null if not found or invalid.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \return the value or boost::none if not found or invalid
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::uint64_t> get_int(const nlohmann::json& json,
+                                       const nlohmann::json::json_pointer& key) noexcept;
+
+/**
+ * Get a 64 bit unsigned integer or null if not found or invalid.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \return the value or boost::none if not found or invalid
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::uint64_t> get_uint(const nlohmann::json& json,
+                                        const nlohmann::json::json_pointer& key) noexcept;
+
+/**
+ * Get a string or null if not found or invalid.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \return the value or boost::none if not found or invalid
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::string> get_string(const nlohmann::json& json,
+                                        const nlohmann::json::json_pointer& key) noexcept;
+
+/**
+ * Get an optional bool.
+ *
+ * If the property is not found, return default value. If the property is not
+ * a bool, return boost::none, otherwise return the value.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \param def the default value
+ * \return the value, boost::none or def
+ */
+JSON_UTIL_EXPORT
+boost::optional<bool> optional_bool(const nlohmann::json& json,
+                                    const nlohmann::json::json_pointer& key,
+                                    bool def = false) noexcept;
+
+/**
+ * Get an optional integer.
+ *
+ * If the property is not found, return default value. If the property is not
+ * an integer, return boost::none, otherwise return the value.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \param def the default value
+ * \return the value, boost::none or def
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::int64_t> optional_int(const nlohmann::json& json,
+                                           const nlohmann::json::json_pointer& key,
+                                           std::int64_t def = 0) noexcept;
+
+/**
+ * Get an optional unsigned integer.
+ *
+ * If the property is not found, return default value. If the property is not
+ * an unsigned integer, return boost::none, otherwise return the value.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \param def the default value
+ * \return the value, boost::none or def
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::uint64_t> optional_uint(const nlohmann::json& json,
+                                             const nlohmann::json::json_pointer& key,
+                                             std::uint64_t def = 0) noexcept;
+
+/**
+ * Get an optional string.
+ *
+ * If the property is not found, return default value. If the property is not
+ * a string, return boost::none, otherwise return the value.
+ *
+ * \param json the JSON object/array
+ * \param key the pointer to the object
+ * \param def the default value
+ * \return the value, boost::none or def
+ */
+JSON_UTIL_EXPORT
+boost::optional<std::string> optional_string(const nlohmann::json& json,
+                                             const nlohmann::json::json_pointer& key,
+                                             const std::string& def = "") noexcept;
+
+/**
+ * Print the value as human readable.
+ *
+ * \param value the value
+ * \return the string
+ */
+JSON_UTIL_EXPORT
+std::string pretty(const nlohmann::json& value);
+
+/**
+ * Check if the array contains the given value.
+ *
+ * \param array the array
+ * \param value the JSON value to check
+ * \return true if present
+ */
+JSON_UTIL_EXPORT
+bool contains(const nlohmann::json& array, const nlohmann::json& value) noexcept;
+
+} // !irccd
+
+#endif // !JSON_UTIL_HPP