mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 15:07:45 +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
2 changed files with 189 additions and 22 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue