/src/serenity/AK/JsonArray.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Concepts.h> |
10 | | #include <AK/Error.h> |
11 | | #include <AK/JsonArraySerializer.h> |
12 | | #include <AK/JsonValue.h> |
13 | | #include <AK/Vector.h> |
14 | | |
15 | | namespace AK { |
16 | | |
17 | | class JsonArray { |
18 | | template<typename Callback> |
19 | | using CallbackErrorType = decltype(declval<Callback>()(declval<JsonValue const&>()))::ErrorType; |
20 | | |
21 | | static_assert(SameAs<CallbackErrorType<ErrorOr<void> (*)(JsonValue const&)>, Error>); |
22 | | static_assert(SameAs<ErrorOr<void, CallbackErrorType<ErrorOr<void> (*)(JsonValue const&)>>, ErrorOr<void>>); |
23 | | |
24 | | public: |
25 | 295k | JsonArray() = default; |
26 | 801k | ~JsonArray() = default; |
27 | | |
28 | | JsonArray(JsonArray const& other) |
29 | 506k | : m_values(other.m_values) |
30 | 506k | { |
31 | 506k | } |
32 | | |
33 | | JsonArray(JsonArray&& other) |
34 | | : m_values(move(other.m_values)) |
35 | 0 | { |
36 | 0 | } |
37 | | |
38 | | template<IterableContainer ContainerT> |
39 | | JsonArray(ContainerT const& source) |
40 | | { |
41 | | for (auto& value : source) |
42 | | m_values.append(move(value)); |
43 | | } |
44 | | |
45 | | JsonArray& operator=(JsonArray const& other) |
46 | 0 | { |
47 | 0 | if (this != &other) |
48 | 0 | m_values = other.m_values; |
49 | 0 | return *this; |
50 | 0 | } |
51 | | |
52 | | JsonArray& operator=(JsonArray&& other) |
53 | 0 | { |
54 | 0 | if (this != &other) |
55 | 0 | m_values = move(other.m_values); |
56 | 0 | return *this; |
57 | 0 | } |
58 | | |
59 | 0 | [[nodiscard]] size_t size() const { return m_values.size(); } |
60 | 0 | [[nodiscard]] bool is_empty() const { return m_values.is_empty(); } |
61 | | |
62 | 0 | [[nodiscard]] JsonValue const& at(size_t index) const { return m_values.at(index); } |
63 | 0 | [[nodiscard]] JsonValue const& operator[](size_t index) const { return at(index); } |
64 | | |
65 | 0 | [[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); } |
66 | | |
67 | 0 | void must_append(JsonValue value) { m_values.append(move(value)); } |
68 | | |
69 | 0 | void clear() { m_values.clear(); } |
70 | 9.23M | ErrorOr<void> append(JsonValue value) { return m_values.try_append(move(value)); } |
71 | 0 | void set(size_t index, JsonValue value) { m_values.at(index) = move(value); } |
72 | | |
73 | | template<typename Builder> |
74 | | typename Builder::OutputType serialized() const; |
75 | | |
76 | | template<typename Builder> |
77 | | void serialize(Builder&) const; |
78 | | |
79 | 0 | [[nodiscard]] ByteString to_byte_string() const { return serialized<StringBuilder>(); } |
80 | | |
81 | | template<typename Callback> |
82 | | void for_each(Callback callback) const |
83 | 0 | { |
84 | 0 | for (auto const& value : m_values) |
85 | 0 | callback(value); |
86 | 0 | } Unexecuted instantiation: void AK::JsonArray::for_each<AK::JsonArray::serialize<AK::StringBuilder>(AK::StringBuilder&) const::{lambda(auto:1&)#1}>(AK::JsonArray::serialize<AK::StringBuilder>(AK::StringBuilder&) const::{lambda(auto:1&)#1}) const Unexecuted instantiation: JSONObject.cpp:void AK::JsonArray::for_each<JS::JSONObject::parse_json_array(JS::VM&, AK::JsonArray const&)::$_0>(JS::JSONObject::parse_json_array(JS::VM&, AK::JsonArray const&)::$_0) const |
87 | | |
88 | | template<FallibleFunction<JsonValue const&> Callback> |
89 | | ErrorOr<void, CallbackErrorType<Callback>> try_for_each(Callback&& callback) const |
90 | | { |
91 | | for (auto const& value : m_values) |
92 | | TRY(callback(value)); |
93 | | return {}; |
94 | | } |
95 | | |
96 | 0 | [[nodiscard]] Vector<JsonValue> const& values() const { return m_values; } |
97 | | |
98 | 0 | void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); } |
99 | | |
100 | | private: |
101 | | Vector<JsonValue> m_values; |
102 | | }; |
103 | | |
104 | | template<typename Builder> |
105 | | inline void JsonArray::serialize(Builder& builder) const |
106 | 0 | { |
107 | 0 | auto serializer = MUST(JsonArraySerializer<>::try_create(builder)); |
108 | 0 | for_each([&](auto& value) { MUST(serializer.add(value)); }); |
109 | 0 | MUST(serializer.finish()); |
110 | 0 | } |
111 | | |
112 | | template<typename Builder> |
113 | | inline typename Builder::OutputType JsonArray::serialized() const |
114 | 0 | { |
115 | 0 | Builder builder; |
116 | 0 | serialize(builder); |
117 | 0 | return builder.to_byte_string(); |
118 | 0 | } |
119 | | |
120 | | } |
121 | | |
122 | | #if USING_AK_GLOBALLY |
123 | | using AK::JsonArray; |
124 | | #endif |