/src/serenity/AK/JsonObject.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch> |
4 | | * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | */ |
8 | | |
9 | | #include "JsonObject.h" |
10 | | |
11 | | namespace AK { |
12 | | |
13 | 73.1k | JsonObject::JsonObject() = default; |
14 | 166k | JsonObject::~JsonObject() = default; |
15 | | |
16 | | JsonObject::JsonObject(JsonObject const& other) |
17 | 93.2k | : m_members(other.m_members.clone().release_value_but_fixme_should_propagate_errors()) |
18 | 93.2k | { |
19 | 93.2k | } |
20 | | |
21 | | JsonObject::JsonObject(JsonObject&& other) |
22 | 0 | : m_members(move(other.m_members)) |
23 | 0 | { |
24 | 0 | } |
25 | | |
26 | | JsonObject& JsonObject::operator=(JsonObject const& other) |
27 | 0 | { |
28 | 0 | if (this != &other) |
29 | 0 | m_members = other.m_members.clone().release_value_but_fixme_should_propagate_errors(); |
30 | 0 | return *this; |
31 | 0 | } |
32 | | |
33 | | JsonObject& JsonObject::operator=(JsonObject&& other) |
34 | 0 | { |
35 | 0 | if (this != &other) |
36 | 0 | m_members = move(other.m_members); |
37 | 0 | return *this; |
38 | 0 | } |
39 | | |
40 | | size_t JsonObject::size() const |
41 | 0 | { |
42 | 0 | return m_members.size(); |
43 | 0 | } |
44 | | |
45 | | bool JsonObject::is_empty() const |
46 | 0 | { |
47 | 0 | return m_members.is_empty(); |
48 | 0 | } |
49 | | |
50 | | Optional<JsonValue const&> JsonObject::get(StringView key) const |
51 | 0 | { |
52 | 0 | auto it = m_members.find(key); |
53 | 0 | if (it == m_members.end()) |
54 | 0 | return {}; |
55 | 0 | return it->value; |
56 | 0 | } |
57 | | |
58 | | Optional<i8> JsonObject::get_i8(StringView key) const |
59 | 0 | { |
60 | 0 | return get_integer<i8>(key); |
61 | 0 | } |
62 | | |
63 | | Optional<u8> JsonObject::get_u8(StringView key) const |
64 | 0 | { |
65 | 0 | return get_integer<u8>(key); |
66 | 0 | } |
67 | | |
68 | | Optional<i16> JsonObject::get_i16(StringView key) const |
69 | 0 | { |
70 | 0 | return get_integer<i16>(key); |
71 | 0 | } |
72 | | |
73 | | Optional<u16> JsonObject::get_u16(StringView key) const |
74 | 0 | { |
75 | 0 | return get_integer<u16>(key); |
76 | 0 | } |
77 | | |
78 | | Optional<i32> JsonObject::get_i32(StringView key) const |
79 | 0 | { |
80 | 0 | return get_integer<i32>(key); |
81 | 0 | } |
82 | | |
83 | | Optional<u32> JsonObject::get_u32(StringView key) const |
84 | 0 | { |
85 | 0 | return get_integer<u32>(key); |
86 | 0 | } |
87 | | |
88 | | Optional<i64> JsonObject::get_i64(StringView key) const |
89 | 0 | { |
90 | 0 | return get_integer<i64>(key); |
91 | 0 | } |
92 | | |
93 | | Optional<u64> JsonObject::get_u64(StringView key) const |
94 | 0 | { |
95 | 0 | return get_integer<u64>(key); |
96 | 0 | } |
97 | | |
98 | | Optional<FlatPtr> JsonObject::get_addr(StringView key) const |
99 | 0 | { |
100 | 0 | return get_integer<FlatPtr>(key); |
101 | 0 | } |
102 | | |
103 | | Optional<bool> JsonObject::get_bool(StringView key) const |
104 | 0 | { |
105 | 0 | auto maybe_value = get(key); |
106 | 0 | if (maybe_value.has_value() && maybe_value->is_bool()) |
107 | 0 | return maybe_value->as_bool(); |
108 | 0 | return {}; |
109 | 0 | } |
110 | | |
111 | | #if !defined(KERNEL) |
112 | | Optional<ByteString> JsonObject::get_byte_string(StringView key) const |
113 | 0 | { |
114 | 0 | auto maybe_value = get(key); |
115 | 0 | if (maybe_value.has_value() && maybe_value->is_string()) |
116 | 0 | return maybe_value->as_string(); |
117 | 0 | return {}; |
118 | 0 | } |
119 | | #endif |
120 | | |
121 | | Optional<JsonObject const&> JsonObject::get_object(StringView key) const |
122 | 0 | { |
123 | 0 | auto maybe_value = get(key); |
124 | 0 | if (maybe_value.has_value() && maybe_value->is_object()) |
125 | 0 | return maybe_value->as_object(); |
126 | 0 | return {}; |
127 | 0 | } |
128 | | |
129 | | Optional<JsonArray const&> JsonObject::get_array(StringView key) const |
130 | 0 | { |
131 | 0 | auto maybe_value = get(key); |
132 | 0 | if (maybe_value.has_value() && maybe_value->is_array()) |
133 | 0 | return maybe_value->as_array(); |
134 | 0 | return {}; |
135 | 0 | } |
136 | | |
137 | | #if !defined(KERNEL) |
138 | | Optional<double> JsonObject::get_double_with_precision_loss(StringView key) const |
139 | 0 | { |
140 | 0 | auto maybe_value = get(key); |
141 | 0 | if (maybe_value.has_value()) |
142 | 0 | return maybe_value->get_double_with_precision_loss(); |
143 | 0 | return {}; |
144 | 0 | } |
145 | | |
146 | | Optional<float> JsonObject::get_float_with_precision_loss(StringView key) const |
147 | 0 | { |
148 | 0 | auto maybe_value = get(key); |
149 | 0 | if (maybe_value.has_value()) |
150 | 0 | return maybe_value->get_float_with_precision_loss(); |
151 | 0 | return {}; |
152 | 0 | } |
153 | | #endif |
154 | | |
155 | | bool JsonObject::has(StringView key) const |
156 | 0 | { |
157 | 0 | return m_members.contains(key); |
158 | 0 | } |
159 | | |
160 | | bool JsonObject::has_null(StringView key) const |
161 | 0 | { |
162 | 0 | auto value = get(key); |
163 | 0 | return value.has_value() && value->is_null(); |
164 | 0 | } |
165 | | |
166 | | bool JsonObject::has_bool(StringView key) const |
167 | 0 | { |
168 | 0 | auto value = get(key); |
169 | 0 | return value.has_value() && value->is_bool(); |
170 | 0 | } |
171 | | |
172 | | bool JsonObject::has_string(StringView key) const |
173 | 0 | { |
174 | 0 | auto value = get(key); |
175 | 0 | return value.has_value() && value->is_string(); |
176 | 0 | } |
177 | | |
178 | | bool JsonObject::has_i8(StringView key) const |
179 | 0 | { |
180 | 0 | auto value = get(key); |
181 | 0 | return value.has_value() && value->is_integer<i8>(); |
182 | 0 | } |
183 | | |
184 | | bool JsonObject::has_u8(StringView key) const |
185 | 0 | { |
186 | 0 | auto value = get(key); |
187 | 0 | return value.has_value() && value->is_integer<u8>(); |
188 | 0 | } |
189 | | |
190 | | bool JsonObject::has_i16(StringView key) const |
191 | 0 | { |
192 | 0 | auto value = get(key); |
193 | 0 | return value.has_value() && value->is_integer<i16>(); |
194 | 0 | } |
195 | | |
196 | | bool JsonObject::has_u16(StringView key) const |
197 | 0 | { |
198 | 0 | auto value = get(key); |
199 | 0 | return value.has_value() && value->is_integer<u16>(); |
200 | 0 | } |
201 | | |
202 | | bool JsonObject::has_i32(StringView key) const |
203 | 0 | { |
204 | 0 | auto value = get(key); |
205 | 0 | return value.has_value() && value->is_integer<i32>(); |
206 | 0 | } |
207 | | |
208 | | bool JsonObject::has_u32(StringView key) const |
209 | 0 | { |
210 | 0 | auto value = get(key); |
211 | 0 | return value.has_value() && value->is_integer<u32>(); |
212 | 0 | } |
213 | | |
214 | | bool JsonObject::has_i64(StringView key) const |
215 | 0 | { |
216 | 0 | auto value = get(key); |
217 | 0 | return value.has_value() && value->is_integer<i64>(); |
218 | 0 | } |
219 | | |
220 | | bool JsonObject::has_u64(StringView key) const |
221 | 0 | { |
222 | 0 | auto value = get(key); |
223 | 0 | return value.has_value() && value->is_integer<u64>(); |
224 | 0 | } |
225 | | |
226 | | bool JsonObject::has_number(StringView key) const |
227 | 0 | { |
228 | 0 | auto value = get(key); |
229 | 0 | return value.has_value() && value->is_number(); |
230 | 0 | } |
231 | | |
232 | | bool JsonObject::has_array(StringView key) const |
233 | 0 | { |
234 | 0 | auto value = get(key); |
235 | 0 | return value.has_value() && value->is_array(); |
236 | 0 | } |
237 | | |
238 | | bool JsonObject::has_object(StringView key) const |
239 | 0 | { |
240 | 0 | auto value = get(key); |
241 | 0 | return value.has_value() && value->is_object(); |
242 | 0 | } |
243 | | |
244 | | void JsonObject::set(ByteString const& key, JsonValue value) |
245 | 1.19M | { |
246 | 1.19M | m_members.set(key, move(value)); |
247 | 1.19M | } |
248 | | |
249 | | bool JsonObject::remove(StringView key) |
250 | 0 | { |
251 | 0 | return m_members.remove(key); |
252 | 0 | } |
253 | | |
254 | | ByteString JsonObject::to_byte_string() const |
255 | 0 | { |
256 | 0 | return serialized<StringBuilder>(); |
257 | 0 | } |
258 | | |
259 | | } |