forked from LadybirdBrowser/ladybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonObject.h
163 lines (133 loc) · 4.97 KB
/
JsonObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, Max Wipfli <[email protected]>
* Copyright (c) 2023, Sam Atkins <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteString.h>
#include <AK/Concepts.h>
#include <AK/Error.h>
#include <AK/HashMap.h>
#include <AK/JsonArray.h>
#include <AK/JsonObjectSerializer.h>
#include <AK/JsonValue.h>
namespace AK {
class JsonObject {
template<typename Callback>
using CallbackErrorType = decltype(declval<Callback>()(declval<ByteString const&>(), declval<JsonValue const&>()).release_error());
public:
JsonObject();
~JsonObject();
JsonObject(JsonObject const& other);
JsonObject(JsonObject&& other);
JsonObject& operator=(JsonObject const& other);
JsonObject& operator=(JsonObject&& other);
[[nodiscard]] size_t size() const;
[[nodiscard]] bool is_empty() const;
[[nodiscard]] bool has(StringView key) const;
[[nodiscard]] bool has_null(StringView key) const;
[[nodiscard]] bool has_bool(StringView key) const;
[[nodiscard]] bool has_string(StringView key) const;
[[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;
[[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;
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;
Optional<ByteString> get_byte_string(StringView key) const;
Optional<JsonObject const&> get_object(StringView key) const;
Optional<JsonArray const&> get_array(StringView key) const;
Optional<double> get_double_with_precision_loss(StringView key) const;
Optional<float> get_float_with_precision_loss(StringView key) const;
void set(ByteString const& key, JsonValue value);
template<typename Callback>
void for_each_member(Callback callback) const
{
for (auto const& member : m_members)
callback(member.key, member.value);
}
template<FallibleFunction<ByteString const&, JsonValue const&> Callback>
ErrorOr<void, CallbackErrorType<Callback>> try_for_each_member(Callback&& callback) const
{
for (auto const& member : m_members)
TRY(callback(member.key, member.value));
return {};
}
bool remove(StringView key);
template<typename Builder>
typename Builder::OutputType serialized() const;
template<typename Builder>
void serialize(Builder&) const;
[[nodiscard]] ByteString to_byte_string() const;
private:
OrderedHashMap<ByteString, JsonValue> m_members;
};
template<typename Builder>
inline void JsonObject::serialize(Builder& builder) const
{
auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
for_each_member([&](auto& key, auto& value) {
MUST(serializer.add(key, value));
});
MUST(serializer.finish());
}
template<typename Builder>
inline typename Builder::OutputType JsonObject::serialized() const
{
Builder builder;
serialize(builder);
return builder.to_byte_string();
}
template<typename Builder>
inline void JsonValue::serialize(Builder& builder) const
{
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);
});
}
template<typename Builder>
inline typename Builder::OutputType JsonValue::serialized() const
{
Builder builder;
serialize(builder);
return builder.to_byte_string();
}
}
#if USING_AK_GLOBALLY
using AK::JsonObject;
#endif