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