/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Max Wipfli * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include "JsonObject.h" namespace AK { JsonObject::JsonObject() = default; JsonObject::~JsonObject() = default; JsonObject::JsonObject(JsonObject const& other) : m_members(other.m_members.clone().release_value_but_fixme_should_propagate_errors()) { } JsonObject::JsonObject(JsonObject&& other) : m_members(move(other.m_members)) { } JsonObject& JsonObject::operator=(JsonObject const& other) { if (this != &other) m_members = other.m_members.clone().release_value_but_fixme_should_propagate_errors(); 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(); } Optional JsonObject::get(StringView key) const { auto it = m_members.find(key); if (it == m_members.end()) return {}; return it->value; } Optional JsonObject::get_i8(StringView key) const { return get_integer(key); } Optional JsonObject::get_u8(StringView key) const { return get_integer(key); } Optional JsonObject::get_i16(StringView key) const { return get_integer(key); } Optional JsonObject::get_u16(StringView key) const { return get_integer(key); } Optional JsonObject::get_i32(StringView key) const { return get_integer(key); } Optional JsonObject::get_u32(StringView key) const { return get_integer(key); } Optional JsonObject::get_i64(StringView key) const { return get_integer(key); } Optional JsonObject::get_u64(StringView key) const { return get_integer(key); } Optional JsonObject::get_addr(StringView key) const { return get_integer(key); } Optional 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 {}; } #if !defined(KERNEL) Optional JsonObject::get_byte_string(StringView key) const { auto maybe_value = get(key); if (maybe_value.has_value() && maybe_value->is_string()) return maybe_value->as_string(); return {}; } #endif Optional 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 {}; } Optional 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 {}; } #if !defined(KERNEL) Optional JsonObject::get_double_with_precision_loss(StringView key) const { auto maybe_value = get(key); if (maybe_value.has_value()) return maybe_value->get_double_with_precision_loss(); return {}; } Optional JsonObject::get_float_with_precision_loss(StringView key) const { auto maybe_value = get(key); if (maybe_value.has_value()) return maybe_value->get_float_with_precision_loss(); return {}; } #endif bool JsonObject::has(StringView key) const { return m_members.contains(key); } bool JsonObject::has_null(StringView key) const { auto value = get(key); return value.has_value() && value->is_null(); } bool JsonObject::has_bool(StringView key) const { auto value = get(key); return value.has_value() && value->is_bool(); } bool JsonObject::has_string(StringView key) const { 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(); } bool JsonObject::has_u8(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_i16(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_u16(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_i32(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_u32(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_i64(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_u64(StringView key) const { auto value = get(key); return value.has_value() && value->is_integer(); } bool JsonObject::has_number(StringView key) const { auto value = get(key); return value.has_value() && value->is_number(); } bool JsonObject::has_array(StringView key) const { auto value = get(key); return value.has_value() && value->is_array(); } bool JsonObject::has_object(StringView key) const { auto value = get(key); return value.has_value() && value->is_object(); } void JsonObject::set(ByteString const& key, JsonValue value) { m_members.set(key, move(value)); } bool JsonObject::remove(StringView key) { return m_members.remove(key); } ByteString JsonObject::to_byte_string() const { return serialized(); } }