From 0fc1d4cd698b9209c58e6cc96d236697da092e9e Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 14 Feb 2025 18:13:40 -0500 Subject: [PATCH] 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. --- AK/JsonArray.h | 16 ++++++++++++++++ AK/JsonObject.cpp | 24 ++++++++++++++++++++++++ AK/JsonObject.h | 4 ++++ 3 files changed, 44 insertions(+) diff --git a/AK/JsonArray.h b/AK/JsonArray.h index 4bdf4a1ad50..d0625766658 100644 --- a/AK/JsonArray.h +++ b/AK/JsonArray.h @@ -80,6 +80,13 @@ public: [[nodiscard]] ByteString to_byte_string() const { return serialized(); } + template + void for_each(Callback callback) + { + for (auto& value : m_values) + callback(value); + } + template void for_each(Callback callback) const { @@ -87,6 +94,14 @@ public: callback(value); } + template Callback> + ErrorOr> try_for_each(Callback&& callback) + { + for (auto& value : m_values) + TRY(callback(value)); + return {}; + } + template Callback> ErrorOr> try_for_each(Callback&& callback) const { @@ -95,6 +110,7 @@ public: return {}; } + [[nodiscard]] Vector& values() { return m_values; } [[nodiscard]] Vector const& values() const { return m_values; } void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); } diff --git a/AK/JsonObject.cpp b/AK/JsonObject.cpp index 554031ec928..510fe84cdc1 100644 --- a/AK/JsonObject.cpp +++ b/AK/JsonObject.cpp @@ -47,6 +47,14 @@ bool JsonObject::is_empty() const return m_members.is_empty(); } +Optional JsonObject::get(StringView key) +{ + auto it = m_members.find(key); + if (it == m_members.end()) + return {}; + return it->value; +} + Optional JsonObject::get(StringView key) const { auto it = m_members.find(key); @@ -116,6 +124,14 @@ Optional JsonObject::get_byte_string(StringView key) const return {}; } +Optional 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::get_object(StringView key) const { auto maybe_value = get(key); @@ -124,6 +140,14 @@ Optional JsonObject::get_object(StringView key) const return {}; } +Optional 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 JsonObject::get_array(StringView key) const { auto maybe_value = get(key); diff --git a/AK/JsonObject.h b/AK/JsonObject.h index 0bd3f788ebe..153fdc88237 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -52,6 +52,7 @@ public: [[nodiscard]] bool has_array(StringView key) const; [[nodiscard]] bool has_object(StringView key) const; + Optional get(StringView key); Optional get(StringView key) const; template @@ -76,7 +77,10 @@ public: Optional get_byte_string(StringView key) const; + Optional get_object(StringView key); Optional get_object(StringView key) const; + + Optional get_array(StringView key); Optional get_array(StringView key) const; Optional get_double_with_precision_loss(StringView key) const;