2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-27 14:15:30 +03:00
|
|
|
#include <AK/JsonArraySerializer.h>
|
2019-06-17 20:47:35 +03:00
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
class JsonArray {
|
|
|
|
public:
|
|
|
|
JsonArray() {}
|
|
|
|
~JsonArray() {}
|
|
|
|
|
2019-08-04 12:45:16 +03:00
|
|
|
JsonArray(const JsonArray& other)
|
|
|
|
: m_values(other.m_values)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray(JsonArray&& other)
|
|
|
|
: m_values(move(other.m_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
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(const JsonValue& value) { m_values.append(value); }
|
2019-08-04 12:45:16 +03:00
|
|
|
void append(JsonValue&& value) { m_values.append(move(value)); }
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
|
|
|
|
|
|
|
String to_string() const { return serialized<StringBuilder>(); }
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2019-06-24 13:02:31 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto& value : m_values)
|
|
|
|
callback(value);
|
|
|
|
}
|
|
|
|
|
2019-08-04 11:05:02 +03:00
|
|
|
const Vector<JsonValue>& values() const { return m_values; }
|
|
|
|
|
2019-10-23 15:55:21 +03:00
|
|
|
void ensure_capacity(int capacity) { m_values.ensure_capacity(capacity); }
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
private:
|
|
|
|
Vector<JsonValue> m_values;
|
|
|
|
};
|
2019-06-17 22:34:12 +03:00
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonArray::serialize(Builder& builder) const
|
|
|
|
{
|
2019-08-27 14:15:30 +03:00
|
|
|
JsonArraySerializer serializer { builder };
|
|
|
|
for_each([&](auto& value) { serializer.add(value); });
|
2019-08-07 22:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonArray::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonArray;
|