2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-11-17 13:15:01 +00:00
|
|
|
#include <AK/Concepts.h>
|
|
|
|
#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>
|
2023-12-16 14:19:34 +00:00
|
|
|
using CallbackErrorType = decltype(declval<Callback>()(declval<ByteString 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
|
|
|
|
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;
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
Optional<ByteString> get_byte_string(StringView key) const;
|
2022-12-21 12:19:23 +00:00
|
|
|
|
|
|
|
Optional<JsonObject const&> get_object(StringView key) const;
|
|
|
|
Optional<JsonArray const&> get_array(StringView key) const;
|
|
|
|
|
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
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void set(ByteString 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
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
template<FallibleFunction<ByteString 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-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString to_byte_string() const;
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
OrderedHashMap<ByteString, 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-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_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
|
|
|
{
|
2023-11-15 08:03:07 +00:00
|
|
|
m_value.visit(
|
|
|
|
[&](Empty const&) { builder.append("null"sv); },
|
|
|
|
[&](bool const& value) { builder.append(value ? "true"sv : "false"sv); },
|
|
|
|
[&](Arithmetic auto const& value) { builder.appendff("{}", value); },
|
|
|
|
[&](ByteString const& value) {
|
|
|
|
builder.append('\"');
|
|
|
|
builder.append_escaped_for_json(value.bytes());
|
|
|
|
builder.append('\"');
|
|
|
|
},
|
|
|
|
[&](auto const& array_or_object) {
|
|
|
|
array_or_object->serialize(builder);
|
|
|
|
});
|
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-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_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
|