mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 13:00:29 +00:00
AK: Add JsonObject::get...() methods that return Optional
`get()` is intended as a replacement for `get_deprecated()` and `get_ptr ()`. The former returns the same value for "key not found" and "key found and contains `null`" which is ambiguous. The latter returns a raw pointer which is spooky. Returning `Optional<JsonValue const&>` covers all the previous uses for these. The `get_foo()` methods are helpers to make user code less verbose. Most of the time, we only want a specific type of value: if we want a number and get a string, we respond the same as if the value was not there at all. These make that easier to express. This also adjusts the `has_i32()` method and friends to examine the value instead of just looking at the underlying type.
This commit is contained in:
parent
d1ad63c466
commit
8d3b268cca
Notes:
sideshowbarker
2024-07-17 05:00:08 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/8d3b268cca Pull-request: https://github.com/SerenityOS/serenity/pull/17044
2 changed files with 189 additions and 22 deletions
|
@ -67,6 +67,111 @@ JsonValue const* JsonObject::get_ptr(StringView key) const
|
|||
return &(*it).value;
|
||||
}
|
||||
|
||||
Optional<JsonValue const&> JsonObject::get(StringView key) const
|
||||
{
|
||||
auto it = m_members.find(key);
|
||||
if (it == m_members.end())
|
||||
return {};
|
||||
return it->value;
|
||||
}
|
||||
|
||||
Optional<i8> JsonObject::get_i8(StringView key) const
|
||||
{
|
||||
return get_integer<i8>(key);
|
||||
}
|
||||
|
||||
Optional<u8> JsonObject::get_u8(StringView key) const
|
||||
{
|
||||
return get_integer<u8>(key);
|
||||
}
|
||||
|
||||
Optional<i16> JsonObject::get_i16(StringView key) const
|
||||
{
|
||||
return get_integer<i16>(key);
|
||||
}
|
||||
|
||||
Optional<u16> JsonObject::get_u16(StringView key) const
|
||||
{
|
||||
return get_integer<u16>(key);
|
||||
}
|
||||
|
||||
Optional<i32> JsonObject::get_i32(StringView key) const
|
||||
{
|
||||
return get_integer<i32>(key);
|
||||
}
|
||||
|
||||
Optional<u32> JsonObject::get_u32(StringView key) const
|
||||
{
|
||||
return get_integer<u32>(key);
|
||||
}
|
||||
|
||||
Optional<i64> JsonObject::get_i64(StringView key) const
|
||||
{
|
||||
return get_integer<i64>(key);
|
||||
}
|
||||
|
||||
Optional<u64> JsonObject::get_u64(StringView key) const
|
||||
{
|
||||
return get_integer<u64>(key);
|
||||
}
|
||||
|
||||
Optional<FlatPtr> JsonObject::get_addr(StringView key) const
|
||||
{
|
||||
return get_integer<FlatPtr>(key);
|
||||
}
|
||||
|
||||
Optional<bool> JsonObject::get_bool(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_bool())
|
||||
return maybe_value->as_bool();
|
||||
return {};
|
||||
}
|
||||
|
||||
#if !defined(KERNEL)
|
||||
Optional<DeprecatedString> JsonObject::get_deprecated_string(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_string())
|
||||
return maybe_value->as_string();
|
||||
return {};
|
||||
}
|
||||
#endif
|
||||
|
||||
Optional<JsonObject const&> JsonObject::get_object(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_object())
|
||||
return maybe_value->as_object();
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonArray const&> JsonObject::get_array(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_array())
|
||||
return maybe_value->as_array();
|
||||
return {};
|
||||
}
|
||||
|
||||
#if !defined(KERNEL)
|
||||
Optional<double> JsonObject::get_double(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_double())
|
||||
return maybe_value->as_double();
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<float> JsonObject::get_float(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_double())
|
||||
return static_cast<float>(maybe_value->as_double());
|
||||
return {};
|
||||
}
|
||||
#endif
|
||||
|
||||
bool JsonObject::has(StringView key) const
|
||||
{
|
||||
return m_members.contains(key);
|
||||
|
@ -74,69 +179,93 @@ bool JsonObject::has(StringView key) const
|
|||
|
||||
bool JsonObject::has_null(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_null();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_null();
|
||||
}
|
||||
|
||||
bool JsonObject::has_bool(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_bool();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_bool();
|
||||
}
|
||||
|
||||
bool JsonObject::has_string(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_string();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_string();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i8(StringView key) const
|
||||
{
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<i8>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u8(StringView key) const
|
||||
{
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<u8>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i16(StringView key) const
|
||||
{
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<i16>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u16(StringView key) const
|
||||
{
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<u16>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i32();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<i32>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u32();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<u32>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i64();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<i64>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u64();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_integer<u64>();
|
||||
}
|
||||
|
||||
bool JsonObject::has_number(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_number();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_number();
|
||||
}
|
||||
|
||||
bool JsonObject::has_array(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_array();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_array();
|
||||
}
|
||||
|
||||
bool JsonObject::has_object(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_object();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_object();
|
||||
}
|
||||
|
||||
#ifndef KERNEL
|
||||
bool JsonObject::has_double(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_double();
|
||||
auto value = get(key);
|
||||
return value.has_value() && value->is_double();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -44,6 +44,10 @@ public:
|
|||
[[nodiscard]] bool has_null(StringView key) const;
|
||||
[[nodiscard]] bool has_bool(StringView key) const;
|
||||
[[nodiscard]] bool has_string(StringView key) const;
|
||||
[[nodiscard]] bool has_i8(StringView key) const;
|
||||
[[nodiscard]] bool has_u8(StringView key) const;
|
||||
[[nodiscard]] bool has_i16(StringView key) const;
|
||||
[[nodiscard]] bool has_u16(StringView key) const;
|
||||
[[nodiscard]] bool has_i32(StringView key) const;
|
||||
[[nodiscard]] bool has_u32(StringView key) const;
|
||||
[[nodiscard]] bool has_i64(StringView key) const;
|
||||
|
@ -55,6 +59,40 @@ public:
|
|||
[[nodiscard]] bool has_double(StringView key) const;
|
||||
#endif
|
||||
|
||||
Optional<JsonValue const&> get(StringView key) const;
|
||||
|
||||
template<Integral T>
|
||||
Optional<T> get_integer(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_integer<T>())
|
||||
return maybe_value->as_integer<T>();
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<i8> get_i8(StringView key) const;
|
||||
Optional<u8> get_u8(StringView key) const;
|
||||
Optional<i16> get_i16(StringView key) const;
|
||||
Optional<u16> get_u16(StringView key) const;
|
||||
Optional<i32> get_i32(StringView key) const;
|
||||
Optional<u32> get_u32(StringView key) const;
|
||||
Optional<i64> get_i64(StringView key) const;
|
||||
Optional<u64> get_u64(StringView key) const;
|
||||
Optional<FlatPtr> get_addr(StringView key) const;
|
||||
Optional<bool> get_bool(StringView key) const;
|
||||
|
||||
#if !defined(KERNEL)
|
||||
Optional<DeprecatedString> get_deprecated_string(StringView key) const;
|
||||
#endif
|
||||
|
||||
Optional<JsonObject const&> get_object(StringView key) const;
|
||||
Optional<JsonArray const&> get_array(StringView key) const;
|
||||
|
||||
#if !defined(KERNEL)
|
||||
Optional<double> get_double(StringView key) const;
|
||||
Optional<float> get_float(StringView key) const;
|
||||
#endif
|
||||
|
||||
void set(DeprecatedString const& key, JsonValue value);
|
||||
|
||||
template<typename Callback>
|
||||
|
|
Loading…
Reference in a new issue