blob: 932f8b77ace90779d1ba55c7420949c73a85c3fe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Concepts.h>
#include <AK/Error.h>
#include <AK/JsonArraySerializer.h>
#include <AK/JsonValue.h>
#include <AK/Vector.h>
namespace AK {
class JsonArray {
template<typename Callback>
using CallbackErrorType = decltype(declval<Callback>()(declval<JsonValue const&>()).release_error());
public:
JsonArray() = default;
~JsonArray() = default;
JsonArray(JsonArray const& other)
: m_values(other.m_values)
{
}
JsonArray(JsonArray&& other)
: m_values(move(other.m_values))
{
}
template<IterableContainer ContainerT>
JsonArray(ContainerT const& source)
{
for (auto& value : source)
m_values.append(move(value));
}
JsonArray& operator=(JsonArray const& other)
{
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;
}
[[nodiscard]] size_t size() const { return m_values.size(); }
[[nodiscard]] bool is_empty() const { return m_values.is_empty(); }
[[nodiscard]] JsonValue const& at(size_t index) const { return m_values.at(index); }
[[nodiscard]] JsonValue const& operator[](size_t index) const { return at(index); }
[[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); }
void clear() { m_values.clear(); }
void append(JsonValue value) { m_values.append(move(value)); }
void set(size_t index, JsonValue value) { m_values[index] = move(value); }
template<typename Builder>
typename Builder::OutputType serialized() const;
template<typename Builder>
void serialize(Builder&) const;
[[nodiscard]] String to_string() const { return serialized<StringBuilder>(); }
template<typename Callback>
void for_each(Callback callback) const
{
for (auto const& value : m_values)
callback(value);
}
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 {};
}
[[nodiscard]] Vector<JsonValue> const& values() const { return m_values; }
void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); }
private:
Vector<JsonValue> m_values;
};
template<typename Builder>
inline void JsonArray::serialize(Builder& builder) const
{
auto serializer = MUST(JsonArraySerializer<>::try_create(builder));
for_each([&](auto& value) { MUST(serializer.add(value)); });
MUST(serializer.finish());
}
template<typename Builder>
inline typename Builder::OutputType JsonArray::serialized() const
{
Builder builder;
serialize(builder);
return builder.build();
}
}
using AK::JsonArray;
|