/* * Copyright (c) 2018-2020, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include #include #include namespace AK { class JsonArray { public: JsonArray() = default; ~JsonArray() = default; JsonArray(const JsonArray& other) : m_values(other.m_values) { } JsonArray(JsonArray&& other) : m_values(move(other.m_values)) { } template JsonArray(const Vector& vector) { for (auto& value : vector) m_values.append(move(value)); } JsonArray& operator=(const JsonArray& 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; } int size() const { return m_values.size(); } bool is_empty() const { return m_values.is_empty(); } const JsonValue& at(int index) const { return m_values.at(index); } const JsonValue& operator[](int index) const { return at(index); } void clear() { m_values.clear(); } void append(JsonValue value) { m_values.append(move(value)); } template typename Builder::OutputType serialized() const; template void serialize(Builder&) const; String to_string() const { return serialized(); } template void for_each(Callback callback) const { for (auto& value : m_values) callback(value); } const Vector& values() const { return m_values; } void ensure_capacity(int capacity) { m_values.ensure_capacity(capacity); } private: Vector m_values; }; template inline void JsonArray::serialize(Builder& builder) const { JsonArraySerializer serializer { builder }; for_each([&](auto& value) { serializer.add(value); }); } template inline typename Builder::OutputType JsonArray::serialized() const { Builder builder; serialize(builder); return builder.build(); } } using AK::JsonArray;