mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-12 20:42:21 +00:00
AK: Provide a few mutable JSON object/array accessors
We have mutable accessors on the JsonValue class already. This will be needed for interaction with Firefox's DevTools, where we will want to mutate the serialized DOM tree we receive from WebContent.
This commit is contained in:
parent
705001483a
commit
0fc1d4cd69
Notes:
github-actions[bot]
2025-02-19 13:47:48 +00:00
Author: https://github.com/trflynn89
Commit: 0fc1d4cd69
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3589
Reviewed-by: https://github.com/ADKaster
3 changed files with 44 additions and 0 deletions
|
@ -80,6 +80,13 @@ public:
|
|||
|
||||
[[nodiscard]] ByteString to_byte_string() const { return serialized<StringBuilder>(); }
|
||||
|
||||
template<typename Callback>
|
||||
void for_each(Callback callback)
|
||||
{
|
||||
for (auto& value : m_values)
|
||||
callback(value);
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void for_each(Callback callback) const
|
||||
{
|
||||
|
@ -87,6 +94,14 @@ public:
|
|||
callback(value);
|
||||
}
|
||||
|
||||
template<FallibleFunction<JsonValue&> Callback>
|
||||
ErrorOr<void, CallbackErrorType<Callback>> try_for_each(Callback&& callback)
|
||||
{
|
||||
for (auto& value : m_values)
|
||||
TRY(callback(value));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<FallibleFunction<JsonValue const&> Callback>
|
||||
ErrorOr<void, CallbackErrorType<Callback>> try_for_each(Callback&& callback) const
|
||||
{
|
||||
|
@ -95,6 +110,7 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector<JsonValue>& values() { return m_values; }
|
||||
[[nodiscard]] Vector<JsonValue> const& values() const { return m_values; }
|
||||
|
||||
void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); }
|
||||
|
|
|
@ -47,6 +47,14 @@ bool JsonObject::is_empty() const
|
|||
return m_members.is_empty();
|
||||
}
|
||||
|
||||
Optional<JsonValue&> JsonObject::get(StringView key)
|
||||
{
|
||||
auto it = m_members.find(key);
|
||||
if (it == m_members.end())
|
||||
return {};
|
||||
return it->value;
|
||||
}
|
||||
|
||||
Optional<JsonValue const&> JsonObject::get(StringView key) const
|
||||
{
|
||||
auto it = m_members.find(key);
|
||||
|
@ -116,6 +124,14 @@ Optional<ByteString> JsonObject::get_byte_string(StringView key) const
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonObject&> JsonObject::get_object(StringView key)
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_object())
|
||||
return maybe_value->as_object();
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonObject const&> JsonObject::get_object(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
|
@ -124,6 +140,14 @@ Optional<JsonObject const&> JsonObject::get_object(StringView key) const
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonArray&> JsonObject::get_array(StringView key)
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
if (maybe_value.has_value() && maybe_value->is_array())
|
||||
return maybe_value->as_array();
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonArray const&> JsonObject::get_array(StringView key) const
|
||||
{
|
||||
auto maybe_value = get(key);
|
||||
|
|
|
@ -52,6 +52,7 @@ public:
|
|||
[[nodiscard]] bool has_array(StringView key) const;
|
||||
[[nodiscard]] bool has_object(StringView key) const;
|
||||
|
||||
Optional<JsonValue&> get(StringView key);
|
||||
Optional<JsonValue const&> get(StringView key) const;
|
||||
|
||||
template<Integral T>
|
||||
|
@ -76,7 +77,10 @@ public:
|
|||
|
||||
Optional<ByteString> get_byte_string(StringView key) const;
|
||||
|
||||
Optional<JsonObject&> get_object(StringView key);
|
||||
Optional<JsonObject const&> get_object(StringView key) const;
|
||||
|
||||
Optional<JsonArray&> get_array(StringView key);
|
||||
Optional<JsonArray const&> get_array(StringView key) const;
|
||||
|
||||
Optional<double> get_double_with_precision_loss(StringView key) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue