mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 22:57:34 +00:00
AK: Add JSON object/array for-each methods for fallible callbacks
This allows the provided callback to return an ErrorOr-like type to propagate errors back to the caller.
This commit is contained in:
parent
56ab529752
commit
13b18a182a
3 changed files with 129 additions and 0 deletions
|
@ -7,6 +7,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObjectSerializer.h>
|
||||
|
@ -16,6 +18,9 @@
|
|||
namespace AK {
|
||||
|
||||
class JsonObject {
|
||||
template<typename Callback>
|
||||
using CallbackErrorType = decltype(declval<Callback>()(declval<String const&>(), declval<JsonValue const&>()).release_error());
|
||||
|
||||
public:
|
||||
JsonObject() = default;
|
||||
~JsonObject() = default;
|
||||
|
@ -142,6 +147,14 @@ public:
|
|||
callback(member.key, member.value);
|
||||
}
|
||||
|
||||
template<FallibleFunction<String const&, JsonValue const&> Callback>
|
||||
ErrorOr<void, CallbackErrorType<Callback>> try_for_each_member(Callback&& callback) const
|
||||
{
|
||||
for (auto const& member : m_members)
|
||||
TRY(callback(member.key, member.value));
|
||||
return {};
|
||||
}
|
||||
|
||||
bool remove(StringView key)
|
||||
{
|
||||
return m_members.remove(key);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue