2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-28 10:21:20 +02:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2023-01-04 17:38:01 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-17 19:47:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-17 08:15:01 -05:00
|
|
|
#include <AK/Concepts.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-11-17 08:15:01 -05:00
|
|
|
#include <AK/Error.h>
|
2019-06-17 19:47:35 +02:00
|
|
|
#include <AK/HashMap.h>
|
2019-08-07 21:28:07 +02:00
|
|
|
#include <AK/JsonArray.h>
|
2019-08-27 14:15:30 +03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-17 19:47:35 +02:00
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
|
2019-06-17 21:34:12 +02:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 19:47:35 +02:00
|
|
|
class JsonObject {
|
2022-11-17 08:15:01 -05:00
|
|
|
template<typename Callback>
|
2022-12-04 18:02:33 +00:00
|
|
|
using CallbackErrorType = decltype(declval<Callback>()(declval<DeprecatedString const&>(), declval<JsonValue const&>()).release_error());
|
2022-11-17 08:15:01 -05:00
|
|
|
|
2019-06-17 19:47:35 +02:00
|
|
|
public:
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject();
|
|
|
|
~JsonObject();
|
2019-08-04 11:45:16 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject(JsonObject const& other);
|
|
|
|
JsonObject(JsonObject&& other);
|
2019-08-04 11:45:16 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject& operator=(JsonObject const& other);
|
|
|
|
JsonObject& operator=(JsonObject&& other);
|
2019-08-04 11:45:16 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] size_t size() const;
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
2019-08-04 11:45:16 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] JsonValue const& get_deprecated(StringView key) const;
|
2019-06-17 19:47:35 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] JsonValue const* get_ptr(StringView key) const;
|
2019-06-17 19:47:35 +02:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] bool has(StringView key) const;
|
2019-12-30 14:49:45 +01:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] bool has_null(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_bool(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_string(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_i32(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_u32(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_i64(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_u64(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_number(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_array(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_object(StringView key) const;
|
2021-06-28 12:00:34 +02:00
|
|
|
#ifndef KERNEL
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] bool has_double(StringView key) const;
|
2021-06-28 12:00:34 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
void set(DeprecatedString const& key, JsonValue value);
|
2019-07-08 13:08:21 +02:00
|
|
|
|
2019-06-17 19:47:35 +02:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_member(Callback callback) const
|
|
|
|
{
|
2021-12-15 14:49:35 +01:00
|
|
|
for (auto const& member : m_members)
|
2021-06-28 11:02:18 +02:00
|
|
|
callback(member.key, member.value);
|
2019-06-17 19:47:35 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
template<FallibleFunction<DeprecatedString const&, JsonValue const&> Callback>
|
2022-11-17 08:15:01 -05:00
|
|
|
ErrorOr<void, CallbackErrorType<Callback>> try_for_each_member(Callback&& callback) const
|
|
|
|
{
|
|
|
|
for (auto const& member : m_members)
|
|
|
|
TRY(callback(member.key, member.value));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
bool remove(StringView key);
|
2020-09-06 16:08:37 +02:00
|
|
|
|
2019-08-07 21:28:07 +02:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] DeprecatedString to_deprecated_string() const;
|
2019-06-17 19:47:35 +02:00
|
|
|
|
|
|
|
private:
|
2022-12-04 18:02:33 +00:00
|
|
|
OrderedHashMap<DeprecatedString, JsonValue> m_members;
|
2019-06-17 19:47:35 +02:00
|
|
|
};
|
2019-06-17 21:34:12 +02:00
|
|
|
|
2019-08-07 21:28:07 +02:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonObject::serialize(Builder& builder) const
|
|
|
|
{
|
2022-02-24 20:08:48 +02:00
|
|
|
auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
|
2019-08-07 21:28:07 +02:00
|
|
|
for_each_member([&](auto& key, auto& value) {
|
2022-02-24 20:08:48 +02:00
|
|
|
MUST(serializer.add(key, value));
|
2019-08-07 21:28:07 +02:00
|
|
|
});
|
2022-02-24 20:08:48 +02:00
|
|
|
MUST(serializer.finish());
|
2019-08-07 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonObject::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 19:32:17 +01:00
|
|
|
inline void JsonValue::serialize(Builder& builder) const
|
2019-08-07 21:28:07 +02:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-05-31 16:02:40 +03:00
|
|
|
case Type::String: {
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('\"');
|
2020-11-02 12:56:36 +01:00
|
|
|
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('\"');
|
2020-05-31 16:02:40 +03:00
|
|
|
} break;
|
2019-08-07 21:28:07 +02:00
|
|
|
case Type::Array:
|
|
|
|
m_value.as_array->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Object:
|
|
|
|
m_value.as_object->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Bool:
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append(m_value.as_bool ? "true"sv : "false"sv);
|
2019-08-07 21:28:07 +02:00
|
|
|
break;
|
2020-05-16 12:00:04 +02:00
|
|
|
#if !defined(KERNEL)
|
2019-08-07 21:28:07 +02:00
|
|
|
case Type::Double:
|
2021-02-09 16:08:11 +01:00
|
|
|
builder.appendff("{}", m_value.as_double);
|
2019-08-07 21:28:07 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2019-10-29 16:36:50 +01:00
|
|
|
case Type::Int32:
|
2020-10-07 14:02:42 +02:00
|
|
|
builder.appendff("{}", as_i32());
|
2019-08-07 21:28:07 +02:00
|
|
|
break;
|
2019-10-29 16:36:50 +01:00
|
|
|
case Type::Int64:
|
2020-10-07 14:02:42 +02:00
|
|
|
builder.appendff("{}", as_i64());
|
2019-10-29 16:36:50 +01:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt32:
|
2020-10-07 14:02:42 +02:00
|
|
|
builder.appendff("{}", as_u32());
|
2019-10-29 16:36:50 +01:00
|
|
|
break;
|
|
|
|
case Type::UnsignedInt64:
|
2020-10-07 14:02:42 +02:00
|
|
|
builder.appendff("{}", as_u64());
|
2019-08-07 21:28:07 +02:00
|
|
|
break;
|
|
|
|
case Type::Null:
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("null"sv);
|
2019-08-07 21:28:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-07 21:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
2020-02-06 19:32:17 +01:00
|
|
|
inline typename Builder::OutputType JsonValue::serialized() const
|
2019-08-07 21:28:07 +02:00
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-26 12:18:30 +01:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-17 21:34:12 +02:00
|
|
|
using AK::JsonObject;
|
2022-11-26 12:18:30 +01:00
|
|
|
#endif
|