AK: Return const& from JsonObject::get()

This adds a static JsonValue* s_null_value, which allows
JsonObject::get to return a reference instaed of copying the return
value. Since JsonValue is only 16 bytes, this seems like a reasonable
compromise.
This commit is contained in:
Max Wipfli 2021-06-28 11:35:58 +02:00 committed by Andreas Kling
parent e0ed160372
commit 66526cbbaf
Notes: sideshowbarker 2024-07-18 11:21:28 +09:00

View file

@ -47,10 +47,16 @@ public:
int size() const { return m_members.size(); }
bool is_empty() const { return m_members.is_empty(); }
JsonValue get(String const& key) const
JsonValue const& get(String const& key) const
{
auto* value = get_ptr(key);
return value ? *value : JsonValue(JsonValue::Type::Null);
static JsonValue* s_null_value { nullptr };
if (!value) {
if (!s_null_value)
s_null_value = new JsonValue;
return *s_null_value;
}
return *value;
}
JsonValue get_or(String const& key, JsonValue const& alternative) const