From bb939d37727f2a046c96dc6ca4a8b2ea8b85531f Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Sun, 14 Apr 2024 22:35:51 +0200 Subject: [PATCH] change implementation of `optionalValueAt` --- src/libutil/json-utils.cc | 10 ++++------ src/libutil/json-utils.hh | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/libutil/json-utils.cc b/src/libutil/json-utils.cc index 7a7264a9a..1b911bf75 100644 --- a/src/libutil/json-utils.cc +++ b/src/libutil/json-utils.cc @@ -30,14 +30,12 @@ const nlohmann::json & valueAt( return map.at(key); } -std::optional optionalValueAt(const nlohmann::json & value, const std::string & key) +std::optional optionalValueAt(const nlohmann::json::object_t & map, const std::string & key) { - try { - auto & v = valueAt(value, key); - return v.get(); - } catch (...) { + if (!map.contains(key)) return std::nullopt; - } + + return std::optional { map.at(key) }; } diff --git a/src/libutil/json-utils.hh b/src/libutil/json-utils.hh index 2024624f4..08c98cc8c 100644 --- a/src/libutil/json-utils.hh +++ b/src/libutil/json-utils.hh @@ -23,7 +23,7 @@ const nlohmann::json & valueAt( const nlohmann::json::object_t & map, const std::string & key); -std::optional optionalValueAt(const nlohmann::json & value, const std::string & key); +std::optional optionalValueAt(const nlohmann::json::object_t & value, const std::string & key); /** * Downcast the json object, failing with a nice error if the conversion fails.