/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Max Wipfli * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace AK { class JsonObject { template using CallbackErrorType = decltype(declval()(declval(), declval()).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 get(StringView key) const; template Optional get_integer(StringView key) const { auto maybe_value = get(key); if (maybe_value.has_value() && maybe_value->is_integer()) return maybe_value->as_integer(); return {}; } Optional get_i8(StringView key) const; Optional get_u8(StringView key) const; Optional get_i16(StringView key) const; Optional get_u16(StringView key) const; Optional get_i32(StringView key) const; Optional get_u32(StringView key) const; Optional get_i64(StringView key) const; Optional get_u64(StringView key) const; Optional get_addr(StringView key) const; Optional get_bool(StringView key) const; #if !defined(KERNEL) Optional get_byte_string(StringView key) const; #endif Optional get_object(StringView key) const; Optional get_array(StringView key) const; #if !defined(KERNEL) Optional get_double_with_precision_loss(StringView key) const; Optional get_float_with_precision_loss(StringView key) const; #endif void set(ByteString const& key, JsonValue value); template void for_each_member(Callback callback) const { for (auto const& member : m_members) callback(member.key, member.value); } template Callback> ErrorOr> 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::OutputType serialized() const; template void serialize(Builder&) const; [[nodiscard]] ByteString to_byte_string() const; private: OrderedHashMap m_members; }; template 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 inline typename Builder::OutputType JsonObject::serialized() const { Builder builder; serialize(builder); return builder.to_byte_string(); } template 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 inline typename Builder::OutputType JsonValue::serialized() const { Builder builder; serialize(builder); return builder.to_byte_string(); } } #if USING_AK_GLOBALLY using AK::JsonObject; #endif