2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-28 11:21:20 +03:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2019-08-07 22:28:07 +03:00
|
|
|
#include <AK/JsonArray.h>
|
2019-08-27 14:15:30 +03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-17 20:47:35 +03:00
|
|
|
#include <AK/JsonValue.h>
|
2020-02-09 17:50:13 +03:00
|
|
|
#include <AK/String.h>
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
class JsonObject {
|
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
JsonObject() = default;
|
|
|
|
~JsonObject() = default;
|
2019-08-04 12:45:16 +03:00
|
|
|
|
2021-06-28 11:21:20 +03:00
|
|
|
JsonObject(JsonObject const& other)
|
2021-06-28 12:02:18 +03:00
|
|
|
: m_members(other.m_members)
|
2019-08-04 12:45:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject(JsonObject&& other)
|
2021-06-28 12:02:18 +03:00
|
|
|
: m_members(move(other.m_members))
|
2019-08-04 12:45:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:21:20 +03:00
|
|
|
JsonObject& operator=(JsonObject const& other)
|
2019-08-04 12:45:16 +03:00
|
|
|
{
|
2021-06-28 12:02:18 +03:00
|
|
|
if (this != &other)
|
2019-08-04 12:45:16 +03:00
|
|
|
m_members = other.m_members;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject& operator=(JsonObject&& other)
|
|
|
|
{
|
2021-06-28 12:02:18 +03:00
|
|
|
if (this != &other)
|
2019-08-04 12:45:16 +03:00
|
|
|
m_members = move(other.m_members);
|
|
|
|
return *this;
|
|
|
|
}
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] size_t size() const { return m_members.size(); }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_members.is_empty(); }
|
2019-06-17 20:47:35 +03:00
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] JsonValue const& get(String const& key) const
|
2019-12-30 16:49:45 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
2021-06-28 12:35:58 +03:00
|
|
|
static JsonValue* s_null_value { nullptr };
|
|
|
|
if (!value) {
|
|
|
|
if (!s_null_value)
|
|
|
|
s_null_value = new JsonValue;
|
|
|
|
return *s_null_value;
|
|
|
|
}
|
|
|
|
return *value;
|
2019-12-30 16:49:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] JsonValue const* get_ptr(String const& key) const
|
2019-06-17 20:47:35 +03:00
|
|
|
{
|
|
|
|
auto it = m_members.find(key);
|
|
|
|
if (it == m_members.end())
|
2019-12-30 16:49:45 +03:00
|
|
|
return nullptr;
|
|
|
|
return &(*it).value;
|
2019-06-17 20:47:35 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] [[nodiscard]] bool has(String const& key) const
|
2019-10-21 15:47:18 +03:00
|
|
|
{
|
|
|
|
return m_members.contains(key);
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_null(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_null();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_bool(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_bool();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_string(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_string();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_i32(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_i32();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_u32(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_u32();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_i64(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_i64();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_u64(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_u64();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_number(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_number();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_array(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_array();
|
|
|
|
}
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] bool has_object(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_object();
|
|
|
|
}
|
|
|
|
#ifndef KERNEL
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] [[nodiscard]] bool has_double(String const& key) const
|
2021-06-28 13:00:34 +03:00
|
|
|
{
|
|
|
|
auto* value = get_ptr(key);
|
|
|
|
return value && value->is_double();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-28 11:21:20 +03:00
|
|
|
void set(String const& key, JsonValue value)
|
2019-07-08 14:08:21 +03:00
|
|
|
{
|
2021-06-28 12:02:18 +03:00
|
|
|
m_members.set(key, move(value));
|
2019-07-08 14:08:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-17 20:47:35 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_member(Callback callback) const
|
|
|
|
{
|
2021-06-28 12:02:18 +03:00
|
|
|
for (auto& member : m_members)
|
|
|
|
callback(member.key, member.value);
|
2019-06-17 20:47:35 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 11:21:20 +03:00
|
|
|
bool remove(String const& key)
|
2020-09-06 17:08:37 +03:00
|
|
|
{
|
2021-06-28 12:02:18 +03:00
|
|
|
return m_members.remove(key);
|
2020-09-06 17:08:37 +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;
|
|
|
|
|
2021-06-28 15:17:14 +03:00
|
|
|
[[nodiscard]] String to_string() const { return serialized<StringBuilder>(); }
|
2019-06-17 20:47:35 +03:00
|
|
|
|
|
|
|
private:
|
2021-06-28 12:02:18 +03:00
|
|
|
OrderedHashMap<String, JsonValue> m_members;
|
2019-06-17 20:47:35 +03:00
|
|
|
};
|
2019-06-17 22:34:12 +03:00
|
|
|
|
2019-08-07 22:28:07 +03:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonObject::serialize(Builder& builder) const
|
|
|
|
{
|
2019-08-27 14:15:30 +03:00
|
|
|
JsonObjectSerializer serializer { builder };
|
2019-08-07 22:28:07 +03:00
|
|
|
for_each_member([&](auto& key, auto& value) {
|
2020-02-09 17:50:13 +03:00
|
|
|
serializer.add(key, value);
|
2019-08-07 22:28:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonObject::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 21:32:17 +03:00
|
|
|
inline void JsonValue::serialize(Builder& builder) const
|
2019-08-07 22:28:07 +03:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-05-31 16:02:40 +03:00
|
|
|
case Type::String: {
|
|
|
|
builder.append("\"");
|
2020-11-02 14:56:36 +03:00
|
|
|
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
|
2020-05-31 16:02:40 +03:00
|
|
|
builder.append("\"");
|
|
|
|
} break;
|
2019-08-07 22:28:07 +03: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 13:00:04 +03:00
|
|
|
#if !defined(KERNEL)
|
2019-08-07 22:28:07 +03:00
|
|
|
case Type::Double:
|
2021-02-09 18:08:11 +03:00
|
|
|
builder.appendff("{}", m_value.as_double);
|
2019-08-07 22:28:07 +03:00
|
|
|
break;
|
|
|
|
#endif
|
2019-10-29 18:36:50 +03:00
|
|
|
case Type::Int32:
|
2020-10-07 15:02:42 +03:00
|
|
|
builder.appendff("{}", as_i32());
|
2019-08-07 22:28:07 +03:00
|
|
|
break;
|
2019-10-29 18:36:50 +03:00
|
|
|
case Type::Int64:
|
2020-10-07 15:02:42 +03:00
|
|
|
builder.appendff("{}", as_i64());
|
2019-10-29 18:36:50 +03:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt32:
|
2020-10-07 15:02:42 +03:00
|
|
|
builder.appendff("{}", as_u32());
|
2019-10-29 18:36:50 +03:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt64:
|
2020-10-07 15:02:42 +03:00
|
|
|
builder.appendff("{}", as_u64());
|
2019-08-07 22:28:07 +03:00
|
|
|
break;
|
|
|
|
case Type::Null:
|
|
|
|
builder.append("null");
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-07 22:28:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 21:32:17 +03:00
|
|
|
inline typename Builder::OutputType JsonValue::serialized() const
|
2019-08-07 22:28:07 +03:00
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 22:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonObject;
|