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>
|
2023-01-04 17:38:01 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
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
|
|
|
|
|
2022-11-17 13:15:01 +00:00
|
|
|
#include <AK/Concepts.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-11-17 13:15:01 +00:00
|
|
|
#include <AK/Error.h>
|
2019-06-17 17:47:35 +00:00
|
|
|
#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>
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
class JsonObject {
|
2022-11-17 13:15:01 +00: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 13:15:01 +00:00
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
public:
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject();
|
|
|
|
~JsonObject();
|
2019-08-04 09:45:16 +00:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject(JsonObject const& other);
|
|
|
|
JsonObject(JsonObject&& other);
|
2019-08-04 09:45:16 +00:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
JsonObject& operator=(JsonObject const& other);
|
|
|
|
JsonObject& operator=(JsonObject&& other);
|
2019-08-04 09:45:16 +00:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] size_t size() const;
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
2019-08-04 09:45:16 +00:00
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] bool has(StringView key) const;
|
2019-12-30 13:49:45 +00: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;
|
2022-12-21 12:19:23 +00:00
|
|
|
[[nodiscard]] bool has_i8(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_u8(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_i16(StringView key) const;
|
|
|
|
[[nodiscard]] bool has_u16(StringView key) const;
|
2023-01-04 17:38:01 +00:00
|
|
|
[[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 10:00:34 +00:00
|
|
|
#ifndef KERNEL
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] bool has_double(StringView key) const;
|
2021-06-28 10:00:34 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-21 12:19:23 +00:00
|
|
|
Optional<JsonValue const&> get(StringView key) const;
|
|
|
|
|
|
|
|
template<Integral T>
|
|
|
|
Optional<T> get_integer(StringView key) const
|
|
|
|
{
|
|
|
|
auto maybe_value = get(key);
|
|
|
|
if (maybe_value.has_value() && maybe_value->is_integer<T>())
|
|
|
|
return maybe_value->as_integer<T>();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<i8> get_i8(StringView key) const;
|
|
|
|
Optional<u8> get_u8(StringView key) const;
|
|
|
|
Optional<i16> get_i16(StringView key) const;
|
|
|
|
Optional<u16> get_u16(StringView key) const;
|
|
|
|
Optional<i32> get_i32(StringView key) const;
|
|
|
|
Optional<u32> get_u32(StringView key) const;
|
|
|
|
Optional<i64> get_i64(StringView key) const;
|
|
|
|
Optional<u64> get_u64(StringView key) const;
|
|
|
|
Optional<FlatPtr> get_addr(StringView key) const;
|
|
|
|
Optional<bool> get_bool(StringView key) const;
|
|
|
|
|
|
|
|
#if !defined(KERNEL)
|
|
|
|
Optional<DeprecatedString> get_deprecated_string(StringView key) const;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Optional<JsonObject const&> get_object(StringView key) const;
|
|
|
|
Optional<JsonArray const&> get_array(StringView key) const;
|
|
|
|
|
|
|
|
#if !defined(KERNEL)
|
2023-11-14 05:36:05 +00:00
|
|
|
Optional<double> get_double_with_precision_loss(StringView key) const;
|
|
|
|
Optional<float> get_float_with_precision_loss(StringView key) const;
|
2022-12-21 12:19:23 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
void set(DeprecatedString const& key, JsonValue 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
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
template<FallibleFunction<DeprecatedString const&, JsonValue const&> Callback>
|
2022-11-17 13:15:01 +00: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 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;
|
|
|
|
|
2023-01-04 17:38:01 +00:00
|
|
|
[[nodiscard]] DeprecatedString to_deprecated_string() const;
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
private:
|
2022-12-04 18:02:33 +00:00
|
|
|
OrderedHashMap<DeprecatedString, 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);
|
2023-01-26 18:58:09 +00:00
|
|
|
return builder.to_deprecated_string();
|
2019-08-07 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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: {
|
2022-07-11 20:10:18 +00:00
|
|
|
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() });
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('\"');
|
2020-05-31 13:02:40 +00:00
|
|
|
} 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:
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append(m_value.as_bool ? "true"sv : "false"sv);
|
2019-08-07 19:28:07 +00:00
|
|
|
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:
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("null"sv);
|
2019-08-07 19:28:07 +00:00
|
|
|
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);
|
2023-01-26 18:58:09 +00:00
|
|
|
return builder.to_deprecated_string();
|
2019-08-07 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-17 19:34:12 +00:00
|
|
|
using AK::JsonObject;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|