| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  |  * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch> | 
					
						
							|  |  |  |  * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "JsonObject.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace AK { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JsonObject::JsonObject() = default; | 
					
						
							|  |  |  | JsonObject::~JsonObject() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JsonObject::JsonObject(JsonObject const& other) | 
					
						
							| 
									
										
										
										
											2023-05-13 20:26:20 +02:00
										 |  |  |     : m_members(other.m_members.clone().release_value_but_fixme_should_propagate_errors()) | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JsonObject::JsonObject(JsonObject&& other) | 
					
						
							|  |  |  |     : m_members(move(other.m_members)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JsonObject& JsonObject::operator=(JsonObject const& other) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (this != &other) | 
					
						
							| 
									
										
										
										
											2023-05-13 20:26:20 +02:00
										 |  |  |         m_members = other.m_members.clone().release_value_but_fixme_should_propagate_errors(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JsonObject& JsonObject::operator=(JsonObject&& other) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (this != &other) | 
					
						
							|  |  |  |         m_members = move(other.m_members); | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t JsonObject::size() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_members.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::is_empty() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_members.is_empty(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 18:13:40 -05:00
										 |  |  | Optional<JsonValue&> JsonObject::get(StringView key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto it = m_members.find(key); | 
					
						
							|  |  |  |     if (it == m_members.end()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return it->value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  | Optional<JsonValue const&> JsonObject::get(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto it = m_members.find(key); | 
					
						
							|  |  |  |     if (it == m_members.end()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return it->value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<i8> JsonObject::get_i8(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<i8>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<u8> JsonObject::get_u8(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<u8>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<i16> JsonObject::get_i16(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<i16>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<u16> JsonObject::get_u16(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<u16>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<i32> JsonObject::get_i32(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<i32>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<u32> JsonObject::get_u32(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<u32>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<i64> JsonObject::get_i64(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<i64>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<u64> JsonObject::get_u64(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<u64>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<FlatPtr> JsonObject::get_addr(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_integer<FlatPtr>(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<bool> JsonObject::get_bool(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							|  |  |  |     if (maybe_value.has_value() && maybe_value->is_bool()) | 
					
						
							|  |  |  |         return maybe_value->as_bool(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 13:21:07 -05:00
										 |  |  | Optional<String const&> JsonObject::get_string(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto value = get(key); value.has_value() && value->is_string()) | 
					
						
							|  |  |  |         return value->as_string(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 18:13:40 -05:00
										 |  |  | Optional<JsonObject&> JsonObject::get_object(StringView key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							|  |  |  |     if (maybe_value.has_value() && maybe_value->is_object()) | 
					
						
							|  |  |  |         return maybe_value->as_object(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  | Optional<JsonObject const&> JsonObject::get_object(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							|  |  |  |     if (maybe_value.has_value() && maybe_value->is_object()) | 
					
						
							|  |  |  |         return maybe_value->as_object(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 18:13:40 -05:00
										 |  |  | Optional<JsonArray&> JsonObject::get_array(StringView key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							|  |  |  |     if (maybe_value.has_value() && maybe_value->is_array()) | 
					
						
							|  |  |  |         return maybe_value->as_array(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  | Optional<JsonArray const&> JsonObject::get_array(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							|  |  |  |     if (maybe_value.has_value() && maybe_value->is_array()) | 
					
						
							|  |  |  |         return maybe_value->as_array(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-14 00:36:05 -05:00
										 |  |  | Optional<double> JsonObject::get_double_with_precision_loss(StringView key) const | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							| 
									
										
										
										
											2024-01-12 20:52:38 -05:00
										 |  |  |     if (maybe_value.has_value()) | 
					
						
							|  |  |  |         return maybe_value->get_double_with_precision_loss(); | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-14 00:36:05 -05:00
										 |  |  | Optional<float> JsonObject::get_float_with_precision_loss(StringView key) const | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     auto maybe_value = get(key); | 
					
						
							| 
									
										
										
										
											2024-01-12 20:52:38 -05:00
										 |  |  |     if (maybe_value.has_value()) | 
					
						
							|  |  |  |         return maybe_value->get_float_with_precision_loss(); | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | bool JsonObject::has(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_members.contains(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_null(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_null(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_bool(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_bool(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_string(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_string(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_i8(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<i8>(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_u8(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<u8>(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_i16(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<i16>(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_u16(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<u16>(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_i32(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<i32>(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_u32(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<u32>(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_i64(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<i64>(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_u64(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_integer<u64>(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_number(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_number(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_array(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_array(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::has_object(StringView key) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:19:23 +00:00
										 |  |  |     auto value = get(key); | 
					
						
							|  |  |  |     return value.has_value() && value->is_object(); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 12:18:27 -05:00
										 |  |  | void JsonObject::set(String key, JsonValue value) | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-17 12:18:27 -05:00
										 |  |  |     m_members.set(move(key), move(value)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JsonObject::set(StringView key, JsonValue value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set(MUST(String::from_utf8(key)), move(value)); | 
					
						
							| 
									
										
										
										
											2023-01-04 17:38:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool JsonObject::remove(StringView key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_members.remove(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |