mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
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:
parent
e0ed160372
commit
66526cbbaf
1 changed files with 8 additions and 2 deletions
|
@ -47,10 +47,16 @@ public:
|
||||||
int size() const { return m_members.size(); }
|
int size() const { return m_members.size(); }
|
||||||
bool is_empty() const { return m_members.is_empty(); }
|
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);
|
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
|
JsonValue get_or(String const& key, JsonValue const& alternative) const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue