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