2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-28 08:21:20 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2021-06-28 08:21:20 +00:00
|
|
|
JsonObject(JsonObject const& other)
|
2021-06-28 09:02:18 +00:00
|
|
|
: m_members(other.m_members)
|
2019-08-04 09:45:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject(JsonObject&& other)
|
2021-06-28 09:02:18 +00:00
|
|
|
: m_members(move(other.m_members))
|
2019-08-04 09:45:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-28 08:21:20 +00:00
|
|
|
JsonObject& operator=(JsonObject const& other)
|
2019-08-04 09:45:16 +00:00
|
|
|
{
|
2021-06-28 09:02:18 +00:00
|
|
|
if (this != &other)
|
2019-08-04 09:45:16 +00:00
|
|
|
m_members = other.m_members;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject& operator=(JsonObject&& other)
|
|
|
|
{
|
2021-06-28 09:02:18 +00:00
|
|
|
if (this != &other)
|
2019-08-04 09:45:16 +00:00
|
|
|
m_members = move(other.m_members);
|
|
|
|
return *this;
|
|
|
|
}
|
2019-06-17 17:47:35 +00:00
|
|
|
|
2021-06-28 12:17:14 +00:00
|
|
|
[[nodiscard]] size_t size() const { return m_members.size(); }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_members.is_empty(); }
|
2019-06-17 17:47:35 +00:00
|
|
|
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] JsonValue const& get(StringView key) const
|
2019-12-30 13:49:45 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 09:35:58 +00: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 13:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] JsonValue const* get_ptr(StringView 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
|
|
|
}
|
|
|
|
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] [[nodiscard]] bool has(StringView key) const
|
2019-10-21 12:47:18 +00:00
|
|
|
{
|
|
|
|
return m_members.contains(key);
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_null(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_null();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_bool(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_bool();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_string(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_string();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_i32(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_i32();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_u32(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_u32();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_i64(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_i64();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_u64(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_u64();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_number(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_number();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_array(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_array();
|
|
|
|
}
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] bool has_object(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_object();
|
|
|
|
}
|
|
|
|
#ifndef KERNEL
|
2021-11-07 13:53:06 +00:00
|
|
|
[[nodiscard]] [[nodiscard]] bool has_double(StringView key) const
|
2021-06-28 10:00:34 +00:00
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
auto const* value = get_ptr(key);
|
2021-06-28 10:00:34 +00:00
|
|
|
return value && value->is_double();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-28 08:21:20 +00:00
|
|
|
void set(String const& key, JsonValue value)
|
2019-07-08 11:08:21 +00:00
|
|
|
{
|
2021-06-28 09:02:18 +00:00
|
|
|
m_members.set(key, move(value));
|
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
|
|
|
|
{
|
2021-12-15 13:49:35 +00:00
|
|
|
for (auto const& member : m_members)
|
2021-06-28 09:02:18 +00:00
|
|
|
callback(member.key, member.value);
|
2019-06-17 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 13:48:25 +00:00
|
|
|
bool remove(StringView key)
|
2020-09-06 14:08:37 +00:00
|
|
|
{
|
2021-06-28 09:02:18 +00:00
|
|
|
return m_members.remove(key);
|
2020-09-06 14:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:28:07 +00:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
|
|
|
|
2021-06-28 12:17:14 +00:00
|
|
|
[[nodiscard]] String to_string() const { return serialized<StringBuilder>(); }
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
private:
|
2021-06-28 09:02:18 +00:00
|
|
|
OrderedHashMap<String, JsonValue> m_members;
|
2019-06-17 17:47:35 +00:00
|
|
|
};
|
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
|
|
|
|
{
|
2022-02-24 18:08:48 +00:00
|
|
|
auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
|
2019-08-07 19:28:07 +00:00
|
|
|
for_each_member([&](auto& key, auto& value) {
|
2022-02-24 18:08:48 +00:00
|
|
|
MUST(serializer.add(key, value));
|
2019-08-07 19:28:07 +00:00
|
|
|
});
|
2022-02-24 18:08:48 +00:00
|
|
|
MUST(serializer.finish());
|
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;
|