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