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
|
|
|
|
|
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 {
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:12:02 +03:00
|
|
|
template<typename T>
|
2021-06-28 11:21:20 +03:00
|
|
|
JsonArray(Vector<T> const& vector)
|
2021-01-15 22:12:02 +03:00
|
|
|
{
|
|
|
|
for (auto& value : vector)
|
|
|
|
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
|
|
|
|
|
|
|
void clear() { m_values.clear(); }
|
2020-03-06 10:49:48 +03:00
|
|
|
void append(JsonValue value) { m_values.append(move(value)); }
|
2021-06-28 12:57:37 +03:00
|
|
|
void set(size_t index, JsonValue value) { m_values[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;
|
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] String to_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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2019-08-27 14:15:30 +03:00
|
|
|
JsonArraySerializer serializer { builder };
|
|
|
|
for_each([&](auto& value) { serializer.add(value); });
|
2019-08-07 22:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonArray::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonArray;
|