Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/JsonArraySerializer.h>
30#include <AK/JsonValue.h>
31#include <AK/Vector.h>
32
33namespace AK {
34
35class JsonArray {
36public:
37 JsonArray() {}
38 ~JsonArray() {}
39
40 JsonArray(const JsonArray& other)
41 : m_values(other.m_values)
42 {
43 }
44
45 JsonArray(JsonArray&& other)
46 : m_values(move(other.m_values))
47 {
48 }
49
50 JsonArray& operator=(const JsonArray& other)
51 {
52 if (this != &other)
53 m_values = other.m_values;
54 return *this;
55 }
56
57 JsonArray& operator=(JsonArray&& other)
58 {
59 if (this != &other)
60 m_values = move(other.m_values);
61 return *this;
62 }
63
64 int size() const { return m_values.size(); }
65 bool is_empty() const { return m_values.is_empty(); }
66
67 const JsonValue& at(int index) const { return m_values.at(index); }
68 const JsonValue& operator[](int index) const { return at(index); }
69
70 void clear() { m_values.clear(); }
71 void append(const JsonValue& value) { m_values.append(value); }
72 void append(JsonValue&& value) { m_values.append(move(value)); }
73
74 template<typename Builder>
75 typename Builder::OutputType serialized() const;
76
77 template<typename Builder>
78 void serialize(Builder&) const;
79
80 String to_string() const { return serialized<StringBuilder>(); }
81
82 template<typename Callback>
83 void for_each(Callback callback) const
84 {
85 for (auto& value : m_values)
86 callback(value);
87 }
88
89 const Vector<JsonValue>& values() const { return m_values; }
90
91 void ensure_capacity(int capacity) { m_values.ensure_capacity(capacity); }
92
93private:
94 Vector<JsonValue> m_values;
95};
96
97template<typename Builder>
98inline void JsonArray::serialize(Builder& builder) const
99{
100 JsonArraySerializer serializer { builder };
101 for_each([&](auto& value) { serializer.add(value); });
102}
103
104template<typename Builder>
105inline typename Builder::OutputType JsonArray::serialized() const
106{
107 Builder builder;
108 serialize(builder);
109 return builder.build();
110}
111
112}
113
114using AK::JsonArray;