2020-01-18 03:38:21 -05:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2019-06-17 13:47:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-01-27 07:03:20 -05:00
|
|
|
#include <AK/Concepts.h>
|
2022-11-17 08:15:01 -05:00
|
|
|
#include <AK/Error.h>
|
2019-08-27 07:15:30 -04:00
|
|
|
#include <AK/JsonArraySerializer.h>
|
2019-06-17 13:47:35 -04:00
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2019-06-17 15:34:12 -04:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 13:47:35 -04:00
|
|
|
class JsonArray {
|
2022-11-17 08:15:01 -05:00
|
|
|
template<typename Callback>
|
|
|
|
using CallbackErrorType = decltype(declval<Callback>()(declval<JsonValue const&>()).release_error());
|
|
|
|
|
2019-06-17 13:47:35 -04:00
|
|
|
public:
|
2021-01-10 18:29:28 -05:00
|
|
|
JsonArray() = default;
|
|
|
|
~JsonArray() = default;
|
2019-06-17 13:47:35 -04:00
|
|
|
|
2024-11-02 16:59:27 -04:00
|
|
|
explicit JsonArray(size_t initial_size)
|
|
|
|
{
|
|
|
|
resize(initial_size);
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:21:20 -04:00
|
|
|
JsonArray(JsonArray const& other)
|
2019-08-04 05:45:16 -04:00
|
|
|
: m_values(other.m_values)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray(JsonArray&& other)
|
|
|
|
: m_values(move(other.m_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:55:15 -05:00
|
|
|
template<IterableContainerOf<JsonValue> ContainerT>
|
2022-01-27 07:03:20 -05:00
|
|
|
JsonArray(ContainerT const& source)
|
2021-01-15 14:12:02 -05:00
|
|
|
{
|
2022-01-27 07:03:20 -05:00
|
|
|
for (auto& value : source)
|
2021-01-15 14:12:02 -05:00
|
|
|
m_values.append(move(value));
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:21:20 -04:00
|
|
|
JsonArray& operator=(JsonArray const& other)
|
2019-08-04 05:45:16 -04:00
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
m_values = other.m_values;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray& operator=(JsonArray&& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
m_values = move(other.m_values);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-28 08:17:14 -04:00
|
|
|
[[nodiscard]] size_t size() const { return m_values.size(); }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_values.is_empty(); }
|
2019-06-17 13:47:35 -04:00
|
|
|
|
2021-06-28 08:17:14 -04:00
|
|
|
[[nodiscard]] JsonValue const& at(size_t index) const { return m_values.at(index); }
|
|
|
|
[[nodiscard]] JsonValue const& operator[](size_t index) const { return at(index); }
|
2019-06-17 13:47:35 -04:00
|
|
|
|
2022-11-13 00:49:54 -05:00
|
|
|
[[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); }
|
|
|
|
|
2023-04-17 01:38:27 -04:00
|
|
|
void must_append(JsonValue value) { m_values.append(move(value)); }
|
|
|
|
|
2019-06-17 13:47:35 -04:00
|
|
|
void clear() { m_values.clear(); }
|
2023-04-17 01:38:27 -04:00
|
|
|
ErrorOr<void> append(JsonValue value) { return m_values.try_append(move(value)); }
|
2023-05-03 12:31:23 -04:00
|
|
|
void set(size_t index, JsonValue value) { m_values.at(index) = move(value); }
|
2019-06-17 13:47:35 -04:00
|
|
|
|
2019-08-07 15:28:07 -04:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
[[nodiscard]] ByteString to_byte_string() const { return serialized<StringBuilder>(); }
|
2019-06-17 13:47:35 -04:00
|
|
|
|
2019-06-24 06:02:31 -04:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each(Callback callback) const
|
|
|
|
{
|
2021-12-15 08:49:35 -05:00
|
|
|
for (auto const& value : m_values)
|
2019-06-24 06:02:31 -04:00
|
|
|
callback(value);
|
|
|
|
}
|
|
|
|
|
2022-11-17 08:15:01 -05:00
|
|
|
template<FallibleFunction<JsonValue const&> Callback>
|
|
|
|
ErrorOr<void, CallbackErrorType<Callback>> try_for_each(Callback&& callback) const
|
|
|
|
{
|
|
|
|
for (auto const& value : m_values)
|
|
|
|
TRY(callback(value));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-28 08:17:14 -04:00
|
|
|
[[nodiscard]] Vector<JsonValue> const& values() const { return m_values; }
|
2019-08-04 04:05:02 -04:00
|
|
|
|
2021-06-28 05:57:37 -04:00
|
|
|
void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); }
|
2024-11-02 16:59:27 -04:00
|
|
|
void resize(size_t size) { m_values.resize(size); }
|
2019-10-23 08:55:21 -04:00
|
|
|
|
2019-06-17 13:47:35 -04:00
|
|
|
private:
|
|
|
|
Vector<JsonValue> m_values;
|
|
|
|
};
|
2019-06-17 15:34:12 -04:00
|
|
|
|
2019-08-07 15:28:07 -04:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonArray::serialize(Builder& builder) const
|
|
|
|
{
|
2022-02-24 13:08:48 -05:00
|
|
|
auto serializer = MUST(JsonArraySerializer<>::try_create(builder));
|
|
|
|
for_each([&](auto& value) { MUST(serializer.add(value)); });
|
|
|
|
MUST(serializer.finish());
|
2019-08-07 15:28:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonArray::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
2023-12-16 09:19:34 -05:00
|
|
|
return builder.to_byte_string();
|
2019-08-07 15:28:07 -04:00
|
|
|
}
|
|
|
|
|
2019-06-17 15:34:12 -04:00
|
|
|
}
|
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-17 15:34:12 -04:00
|
|
|
using AK::JsonArray;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|