diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-11 15:20:04 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-11 17:40:33 +0200 |
commit | 5cab526989221c0228d9ca08deb241b640dbfa95 (patch) | |
tree | a0340412f77d320f5320e18b247f658b4cb4a5b6 /src/utils/cpp/json.hpp | |
parent | aafd97ee263f11da108b873ed8f350f3824f9f67 (diff) | |
download | justbuild-5cab526989221c0228d9ca08deb241b640dbfa95.tar.gz |
json: support pruning
... by removing from an object the outer keys where the value is null.
Diffstat (limited to 'src/utils/cpp/json.hpp')
-rw-r--r-- | src/utils/cpp/json.hpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/cpp/json.hpp b/src/utils/cpp/json.hpp index c52b8bd2..64c208a7 100644 --- a/src/utils/cpp/json.hpp +++ b/src/utils/cpp/json.hpp @@ -215,6 +215,22 @@ namespace detail { return old_abbrev; } +// \brief For a json object, return an object that is obtained from the original +// one by dropping all top-level keys where the value is null. +[[nodiscard]] static inline auto PruneJson(nlohmann::json const& json) + -> nlohmann::json { + if (not json.is_object()) { + return json; + } + auto result = nlohmann::json::object(); + for (auto const& el : json.items()) { + if (not el.value().is_null()) { + result[el.key()] = el.value(); + } + } + return result; +} + #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 |