Serenity Operating System
at hosted 113 lines 3.4 kB view raw
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(JsonValue value) { m_values.append(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 String to_string() const { return serialized<StringBuilder>(); } 80 81 template<typename Callback> 82 void for_each(Callback callback) const 83 { 84 for (auto& value : m_values) 85 callback(value); 86 } 87 88 const Vector<JsonValue>& values() const { return m_values; } 89 90 void ensure_capacity(int capacity) { m_values.ensure_capacity(capacity); } 91 92private: 93 Vector<JsonValue> m_values; 94}; 95 96template<typename Builder> 97inline void JsonArray::serialize(Builder& builder) const 98{ 99 JsonArraySerializer serializer { builder }; 100 for_each([&](auto& value) { serializer.add(value); }); 101} 102 103template<typename Builder> 104inline typename Builder::OutputType JsonArray::serialized() const 105{ 106 Builder builder; 107 serialize(builder); 108 return builder.build(); 109} 110 111} 112 113using AK::JsonArray;