2020-01-18 08:38:21 +00: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 17:47:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2019-08-07 19:28:07 +00:00
|
|
|
#include <AK/JsonArray.h>
|
2019-08-27 11:15:30 +00:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-17 17:47:35 +00:00
|
|
|
#include <AK/JsonValue.h>
|
2020-02-09 14:50:13 +00:00
|
|
|
#include <AK/String.h>
|
2019-06-17 17:47:35 +00:00
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
class JsonObject {
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
JsonObject() = default;
|
|
|
|
~JsonObject() = default;
|
2019-08-04 09:45:16 +00:00
|
|
|
|
|
|
|
JsonObject(const JsonObject& other)
|
2020-06-11 04:40:27 +00:00
|
|
|
: m_order(other.m_order)
|
|
|
|
, m_members(other.m_members)
|
2019-08-04 09:45:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject(JsonObject&& other)
|
2020-06-11 04:40:27 +00:00
|
|
|
: m_order(move(other.m_order))
|
|
|
|
, m_members(move(other.m_members))
|
2019-08-04 09:45:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject& operator=(const JsonObject& other)
|
|
|
|
{
|
2020-06-11 04:40:27 +00:00
|
|
|
if (this != &other) {
|
2019-08-04 09:45:16 +00:00
|
|
|
m_members = other.m_members;
|
2020-06-11 04:40:27 +00:00
|
|
|
m_order = other.m_order;
|
|
|
|
}
|
2019-08-04 09:45:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject& operator=(JsonObject&& other)
|
|
|
|
{
|
2020-06-11 04:40:27 +00:00
|
|
|
if (this != &other) {
|
2019-08-04 09:45:16 +00:00
|
|
|
m_members = move(other.m_members);
|
2020-06-11 04:40:27 +00:00
|
|
|
m_order = move(other.m_order);
|
|
|
|
}
|
2019-08-04 09:45:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
int size() const { return m_members.size(); }
|
|
|
|
bool is_empty() const { return m_members.is_empty(); }
|
|
|
|
|
|
|
|
JsonValue get(const String& key) const
|
2019-12-30 13:49:45 +00:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
2020-06-11 04:40:27 +00:00
|
|
|
return value ? *value : JsonValue(JsonValue::Type::Null);
|
2019-12-30 13:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:58:56 +00:00
|
|
|
JsonValue get_or(const String& key, const JsonValue& alternative) const
|
2020-03-14 14:55:06 +00:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value ? *value : alternative;
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:49:45 +00:00
|
|
|
const JsonValue* get_ptr(const String& key) const
|
2019-06-17 17:47:35 +00:00
|
|
|
{
|
|
|
|
auto it = m_members.find(key);
|
|
|
|
if (it == m_members.end())
|
2019-12-30 13:49:45 +00:00
|
|
|
return nullptr;
|
|
|
|
return &(*it).value;
|
2019-06-17 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:47:18 +00:00
|
|
|
bool has(const String& key) const
|
|
|
|
{
|
|
|
|
return m_members.contains(key);
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:49:48 +00:00
|
|
|
void set(const String& key, JsonValue value)
|
2019-07-08 11:08:21 +00:00
|
|
|
{
|
2020-08-10 11:49:34 +00:00
|
|
|
if (m_members.set(key, move(value)) == HashSetResult::ReplacedExistingEntry)
|
|
|
|
m_order.remove(m_order.find_first_index(key).value());
|
2020-06-11 04:40:27 +00:00
|
|
|
m_order.append(key);
|
2019-07-08 11:08:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_member(Callback callback) const
|
|
|
|
{
|
2020-06-11 04:40:27 +00:00
|
|
|
for (size_t i = 0; i < m_order.size(); ++i) {
|
|
|
|
auto property = m_order[i];
|
|
|
|
callback(property, m_members.get(property).value());
|
|
|
|
}
|
2019-06-17 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:08:37 +00:00
|
|
|
bool remove(const String& key)
|
|
|
|
{
|
|
|
|
if (m_members.remove(key)) {
|
|
|
|
m_order.remove(m_order.find_first_index(key).value());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:28:07 +00: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 17:47:35 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-11 04:40:27 +00:00
|
|
|
Vector<String> m_order;
|
2019-06-17 17:47:35 +00:00
|
|
|
HashMap<String, JsonValue> m_members;
|
|
|
|
};
|
2019-06-17 19:34:12 +00:00
|
|
|
|
2019-08-07 19:28:07 +00:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonObject::serialize(Builder& builder) const
|
|
|
|
{
|
2019-08-27 11:15:30 +00:00
|
|
|
JsonObjectSerializer serializer { builder };
|
2019-08-07 19:28:07 +00:00
|
|
|
for_each_member([&](auto& key, auto& value) {
|
2020-02-09 14:50:13 +00:00
|
|
|
serializer.add(key, value);
|
2019-08-07 19:28:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonObject::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 18:32:17 +00:00
|
|
|
inline void JsonValue::serialize(Builder& builder) const
|
2019-08-07 19:28:07 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-05-31 13:02:40 +00:00
|
|
|
case Type::String: {
|
|
|
|
builder.append("\"");
|
2020-11-02 11:56:36 +00:00
|
|
|
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
|
2020-05-31 13:02:40 +00:00
|
|
|
builder.append("\"");
|
|
|
|
} break;
|
2019-08-07 19:28:07 +00:00
|
|
|
case Type::Array:
|
|
|
|
m_value.as_array->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Object:
|
|
|
|
m_value.as_object->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Bool:
|
|
|
|
builder.append(m_value.as_bool ? "true" : "false");
|
|
|
|
break;
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(KERNEL)
|
2019-08-07 19:28:07 +00:00
|
|
|
case Type::Double:
|
2021-02-09 15:08:11 +00:00
|
|
|
builder.appendff("{}", m_value.as_double);
|
2019-08-07 19:28:07 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2019-10-29 15:36:50 +00:00
|
|
|
case Type::Int32:
|
2020-10-07 12:02:42 +00:00
|
|
|
builder.appendff("{}", as_i32());
|
2019-08-07 19:28:07 +00:00
|
|
|
break;
|
2019-10-29 15:36:50 +00:00
|
|
|
case Type::Int64:
|
2020-10-07 12:02:42 +00:00
|
|
|
builder.appendff("{}", as_i64());
|
2019-10-29 15:36:50 +00:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt32:
|
2020-10-07 12:02:42 +00:00
|
|
|
builder.appendff("{}", as_u32());
|
2019-10-29 15:36:50 +00:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt64:
|
2020-10-07 12:02:42 +00:00
|
|
|
builder.appendff("{}", as_u64());
|
2019-08-07 19:28:07 +00:00
|
|
|
break;
|
|
|
|
case Type::Null:
|
|
|
|
builder.append("null");
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-07 19:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 18:32:17 +00:00
|
|
|
inline typename Builder::OutputType JsonValue::serialized() const
|
2019-08-07 19:28:07 +00:00
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonObject;
|