From 7011dba98e9616727a4d529fe4fda6a8ceefe393 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 30 Dec 2019 14:49:45 +0100 Subject: [PATCH] AK: Add JsonObject::get_ptr() for copy-free lookup This variant of get() returns a const JsonValue* instead of a JsonValue and can be used when you want to peek into a JsonObject's member fields without making copies. --- AK/JsonObject.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AK/JsonObject.h b/AK/JsonObject.h index a69d2255b9..54a50627cf 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -41,11 +41,17 @@ public: bool is_empty() const { return m_members.is_empty(); } JsonValue get(const String& key) const + { + auto* value = get_ptr(key); + return value ? *value : JsonValue(JsonValue::Type::Undefined); + } + + const JsonValue* get_ptr(const String& key) const { auto it = m_members.find(key); if (it == m_members.end()) - return JsonValue(JsonValue::Type::Undefined); - return (*it).value; + return nullptr; + return &(*it).value; } bool has(const String& key) const