summaryrefslogtreecommitdiff
path: root/src/utils/cpp/json.hpp
blob: c52b8bd2b08e9d7adc65363ae47dd7227f4ad8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_UTILS_CPP_JSON_HPP
#define INCLUDED_SRC_UTILS_CPP_JSON_HPP

#include <algorithm>
#include <cstddef>
#include <optional>
#include <sstream>
#include <string>
#include <unordered_map>

#include "fmt/core.h"
#include "fmt/ostream.h"
#include "gsl/gsl"
#include "nlohmann/json.hpp"
#include "src/utils/cpp/gsl.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 {

// NOLINTNEXTLINE(misc-no-recursion)
[[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 << '}';
        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 << ']';
        EnsuresAudit(nlohmann::json::parse(oss.str()) == json);
        return oss.str();
    }
    return json.dump();
}

// NOLINTNEXTLINE(misc-no-recursion)
[[nodiscard]] static inline auto IndentOnlyUntilDepth(
    nlohmann::json const& json,
    std::string const& indent,
    std::size_t until,
    std::size_t depth,
    std::optional<std::string> path,
    std::unordered_map<std::string, std::size_t> const& depths) -> std::string {
    using iterator = std::ostream_iterator<std::string>;
    if (path and depths.find(*path) != depths.end()) {
        until = depths.find(*path)->second;
    }
    if (json.is_object() and depth < until) {
        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() << ": "
                << IndentOnlyUntilDepth(
                       value,
                       indent,
                       until,
                       depth + 1,
                       path ? std::optional<std::string>(*path + "/" + key)
                            : std::nullopt,
                       depths)
                << (++i == json.size() ? "" : ",") << std::endl;
        }
        std::fill_n(iterator{oss}, depth, indent);
        oss << '}';
        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 << IndentOnlyUntilDepth(
                       value, indent, until, depth + 1, std::nullopt, depths)
                << (++i == json.size() ? "" : ",") << std::endl;
        }
        std::fill_n(iterator{oss}, depth, indent);
        oss << ']';
        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);
}

// \brief Dump json with indent. Indent until the given list; for initial
// pure object-paths, alternative depths can be specified
[[nodiscard]] static inline auto IndentOnlyUntilDepth(
    nlohmann::json const& json,
    std::size_t indent,
    std::size_t until_depth,
    std::unordered_map<std::string, std::size_t> const& depths) -> std::string {
    return detail::IndentOnlyUntilDepth(
        json, std::string(indent, ' '), until_depth, 0, "", depths);
}

// \brief Dump json, replacing subexpressions at the given depths by "*".
// NOLINTNEXTLINE(misc-no-recursion)
[[nodiscard]] static inline auto TruncateJson(nlohmann::json const& json,
                                              std::size_t depth)
    -> std::string {
    if (depth == 0) {
        return "*";
    }
    if (json.is_object()) {
        std::size_t i{};
        std::ostringstream oss{};
        oss << '{';
        for (auto const& [key, value] : json.items()) {
            oss << nlohmann::json(key).dump() << ":"
                << TruncateJson(value, depth - 1)
                << (++i == json.size() ? "" : ",");
        }
        oss << '}';
        return oss.str();
    }
    if (json.is_array()) {
        std::size_t i{};
        std::ostringstream oss{};
        oss << '[';
        for (auto const& value : json) {
            oss << TruncateJson(value, depth - 1)
                << (++i == json.size() ? "" : ",");
        }
        oss << ']';
        return oss.str();
    }
    return json.dump();
}

[[nodiscard]] static inline auto AbbreviateJson(nlohmann::json const& json,
                                                std::size_t len)
    -> std::string {
    auto json_string = json.dump();
    if (json_string.size() <= len) {
        return json_string;
    }
    std::size_t i = 1;
    auto old_abbrev = TruncateJson(json, i);
    auto new_abbrev = old_abbrev;
    while (new_abbrev.size() <= len) {
        old_abbrev = new_abbrev;
        ++i;
        new_abbrev = TruncateJson(json, i);
    }
    return old_abbrev;
}

#if defined(FMT_VERSION) and FMT_VERSION >= 100000
// Use nlohmann::basic_json::operator<<() for formatting via libfmt.
// This explicit template specialization seems to be required starting with
// libfmt 10.x.
template <>
struct fmt::formatter<nlohmann::basic_json<>> : ostream_formatter {};
#endif

#endif  // INCLUDED_SRC_UTILS_CPP_JSON_HPP