summaryrefslogtreecommitdiff
path: root/src/utils/cpp/json.hpp
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
commit619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch)
tree01868de723cb82c86842f33743fa7b14e24c1fa3 /src/utils/cpp/json.hpp
downloadjustbuild-619def44c1cca9f3cdf63544d5f24f2c7a7d9b77.tar.gz
Initial self-hosting commit
This is the initial version of our tool that is able to build itself. In can be bootstrapped by ./bin/bootstrap.py Co-authored-by: Oliver Reiche <oliver.reiche@huawei.com> Co-authored-by: Victor Moreno <victor.moreno1@huawei.com>
Diffstat (limited to 'src/utils/cpp/json.hpp')
-rw-r--r--src/utils/cpp/json.hpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/utils/cpp/json.hpp b/src/utils/cpp/json.hpp
new file mode 100644
index 00000000..8945e975
--- /dev/null
+++ b/src/utils/cpp/json.hpp
@@ -0,0 +1,83 @@
+#ifndef INCLUDED_SRC_UTILS_CPP_JSON_HPP
+#define INCLUDED_SRC_UTILS_CPP_JSON_HPP
+
+#include <algorithm>
+#include <optional>
+#include <sstream>
+#include <string>
+
+#include "nlohmann/json.hpp"
+#include "gsl-lite/gsl-lite.hpp"
+
+template <typename ValueT>
+auto ExtractValueAs(
+ nlohmann::json const& j,
+ std::string const& key,
+ std::function<void(std::string const& error)>&& logger =
+ [](std::string const & /*unused*/) -> void {}) noexcept
+ -> std::optional<ValueT> {
+ try {
+ auto it = j.find(key);
+ if (it == j.end()) {
+ logger("key " + key + " cannot be found in JSON object");
+ return std::nullopt;
+ }
+ return it.value().template get<ValueT>();
+ } catch (std::exception& e) {
+ logger(e.what());
+ return std::nullopt;
+ }
+}
+
+namespace detail {
+
+[[nodiscard]] static inline auto IndentListsOnlyUntilDepth(
+ nlohmann::json const& json,
+ std::string const& indent,
+ std::size_t until,
+ std::size_t depth) -> std::string {
+ using iterator = std::ostream_iterator<std::string>;
+ if (json.is_object()) {
+ std::size_t i{};
+ std::ostringstream oss{};
+ oss << '{' << std::endl;
+ for (auto const& [key, value] : json.items()) {
+ std::fill_n(iterator{oss}, depth + 1, indent);
+ oss << nlohmann::json(key).dump() << ": "
+ << IndentListsOnlyUntilDepth(value, indent, until, depth + 1)
+ << (++i == json.size() ? "" : ",") << std::endl;
+ }
+ std::fill_n(iterator{oss}, depth, indent);
+ oss << '}';
+ gsl_EnsuresAudit(nlohmann::json::parse(oss.str()) == json);
+ return oss.str();
+ }
+ if (json.is_array() and depth < until) {
+ std::size_t i{};
+ std::ostringstream oss{};
+ oss << '[' << std::endl;
+ for (auto const& value : json) {
+ std::fill_n(iterator{oss}, depth + 1, indent);
+ oss << IndentListsOnlyUntilDepth(value, indent, until, depth + 1)
+ << (++i == json.size() ? "" : ",") << std::endl;
+ }
+ std::fill_n(iterator{oss}, depth, indent);
+ oss << ']';
+ gsl_EnsuresAudit(nlohmann::json::parse(oss.str()) == json);
+ return oss.str();
+ }
+ return json.dump();
+}
+
+} // namespace detail
+
+/// \brief Dump json with indent. Indent lists only until specified depth.
+[[nodiscard]] static inline auto IndentListsOnlyUntilDepth(
+ nlohmann::json const& json,
+ std::size_t indent,
+ std::size_t until_depth = 0) -> std::string {
+ return detail::IndentListsOnlyUntilDepth(
+ json, std::string(indent, ' '), until_depth, 0);
+}
+
+#endif // INCLUDED_SRC_UTILS_CPP_JSON_HPP