2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-06-28 05:21:20 -03:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2023-01-04 14:38:01 -03:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-17 14:47:35 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-11-17 10:15:01 -03:00
|
|
|
#include <AK/Concepts.h>
|
|
|
|
|
#include <AK/Error.h>
|
2019-06-17 14:47:35 -03:00
|
|
|
#include <AK/HashMap.h>
|
2019-08-07 16:28:07 -03:00
|
|
|
#include <AK/JsonArray.h>
|
2019-06-17 14:47:35 -03:00
|
|
|
#include <AK/JsonValue.h>
|
2025-02-17 14:18:27 -03:00
|
|
|
#include <AK/String.h>
|
2019-06-17 14:47:35 -03:00
|
|
|
|
2019-06-17 16:34:12 -03:00
|
|
|
namespace AK {
|
|
|
|
|
|
2019-06-17 14:47:35 -03:00
|
|
|
class JsonObject {
|
2022-11-17 10:15:01 -03:00
|
|
|
template<typename Callback>
|
2025-02-17 14:18:27 -03:00
|
|
|
using CallbackErrorType = decltype(declval<Callback>()(declval<String const&>(), declval<JsonValue const&>()).release_error());
|
2022-11-17 10:15:01 -03:00
|
|
|
|
2019-06-17 14:47:35 -03:00
|
|
|
public:
|
2023-01-04 14:38:01 -03:00
|
|
|
JsonObject();
|
|
|
|
|
~JsonObject();
|
2019-08-04 06:45:16 -03:00
|
|
|
|
2023-01-04 14:38:01 -03:00
|
|
|
JsonObject(JsonObject const& other);
|
|
|
|
|
JsonObject(JsonObject&& other);
|
2019-08-04 06:45:16 -03:00
|
|
|
|
2023-01-04 14:38:01 -03:00
|
|
|
JsonObject& operator=(JsonObject const& other);
|
|
|
|
|
JsonObject& operator=(JsonObject&& other);
|
2019-08-04 06:45:16 -03:00
|
|
|
|
2023-01-04 14:38:01 -03:00
|
|
|
[[nodiscard]] size_t size() const;
|
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
2019-08-04 06:45:16 -03:00
|
|
|
|
2023-01-04 14:38:01 -03:00
|
|
|
[[nodiscard]] bool has(StringView key) const;
|
2019-12-30 10:49:45 -03:00
|
|
|
|
2023-01-04 14:38:01 -03: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 09:19:23 -03: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 14:38:01 -03: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 07:00:34 -03:00
|
|
|
|
2025-02-14 20:13:40 -03:00
|
|
|
Optional<JsonValue&> get(StringView key);
|
2022-12-21 09:19:23 -03: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;
|
|
|
|
|
|
2025-02-17 15:21:07 -03:00
|
|
|
Optional<String const&> get_string(StringView key) const;
|
2022-12-21 09:19:23 -03:00
|
|
|
|
2025-02-14 20:13:40 -03:00
|
|
|
Optional<JsonObject&> get_object(StringView key);
|
2022-12-21 09:19:23 -03:00
|
|
|
Optional<JsonObject const&> get_object(StringView key) const;
|
2025-02-14 20:13:40 -03:00
|
|
|
|
|
|
|
|
Optional<JsonArray&> get_array(StringView key);
|
2022-12-21 09:19:23 -03:00
|
|
|
Optional<JsonArray const&> get_array(StringView key) const;
|
|
|
|
|
|
2023-11-14 02:36:05 -03:00
|
|
|
Optional<double> get_double_with_precision_loss(StringView key) const;
|
|
|
|
|
Optional<float> get_float_with_precision_loss(StringView key) const;
|
2022-12-21 09:19:23 -03:00
|
|
|
|
2025-02-17 14:18:27 -03:00
|
|
|
void set(String key, JsonValue value);
|
|
|
|
|
void set(StringView key, JsonValue value);
|
2019-07-08 08:08:21 -03:00
|
|
|
|
2019-06-17 14:47:35 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_member(Callback callback) const
|
|
|
|
|
{
|
2021-12-15 10:49:35 -03:00
|
|
|
for (auto const& member : m_members)
|
2021-06-28 06:02:18 -03:00
|
|
|
callback(member.key, member.value);
|
2019-06-17 14:47:35 -03:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 14:18:27 -03:00
|
|
|
template<FallibleFunction<String const&, JsonValue const&> Callback>
|
2022-11-17 10:15:01 -03: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 14:38:01 -03:00
|
|
|
bool remove(StringView key);
|
2020-09-06 11:08:37 -03:00
|
|
|
|
2025-02-17 17:08:17 -03:00
|
|
|
String serialized() const;
|
|
|
|
|
void serialize(StringBuilder&) const;
|
2019-08-07 16:28:07 -03:00
|
|
|
|
2019-06-17 14:47:35 -03:00
|
|
|
private:
|
2025-02-17 14:18:27 -03:00
|
|
|
OrderedHashMap<String, JsonValue> m_members;
|
2019-06-17 14:47:35 -03:00
|
|
|
};
|
2019-06-17 16:34:12 -03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-17 16:34:12 -03:00
|
|
|
using AK::JsonObject;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|