mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Move JsonObject implementation out of line
This commit is contained in:
parent
1dd6b7f5b7
commit
d1ad63c466
Notes:
sideshowbarker
2024-07-17 01:34:35 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/d1ad63c466 Pull-request: https://github.com/SerenityOS/serenity/pull/17044
3 changed files with 185 additions and 112 deletions
|
@ -10,6 +10,7 @@ set(AK_SOURCES
|
|||
FuzzyMatch.cpp
|
||||
GenericLexer.cpp
|
||||
Hex.cpp
|
||||
JsonObject.cpp
|
||||
JsonParser.cpp
|
||||
JsonPath.cpp
|
||||
JsonValue.cpp
|
||||
|
|
158
AK/JsonObject.cpp
Normal file
158
AK/JsonObject.cpp
Normal file
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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)
|
||||
: m_members(other.m_members)
|
||||
{
|
||||
}
|
||||
|
||||
JsonObject::JsonObject(JsonObject&& other)
|
||||
: m_members(move(other.m_members))
|
||||
{
|
||||
}
|
||||
|
||||
JsonObject& JsonObject::operator=(JsonObject const& other)
|
||||
{
|
||||
if (this != &other)
|
||||
m_members = other.m_members;
|
||||
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();
|
||||
}
|
||||
|
||||
JsonValue const& JsonObject::get_deprecated(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
static JsonValue* s_null_value { nullptr };
|
||||
if (!value) {
|
||||
if (!s_null_value)
|
||||
s_null_value = new JsonValue;
|
||||
return *s_null_value;
|
||||
}
|
||||
return *value;
|
||||
}
|
||||
|
||||
JsonValue const* JsonObject::get_ptr(StringView key) const
|
||||
{
|
||||
auto it = m_members.find(key);
|
||||
if (it == m_members.end())
|
||||
return nullptr;
|
||||
return &(*it).value;
|
||||
}
|
||||
|
||||
bool JsonObject::has(StringView key) const
|
||||
{
|
||||
return m_members.contains(key);
|
||||
}
|
||||
|
||||
bool JsonObject::has_null(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_null();
|
||||
}
|
||||
|
||||
bool JsonObject::has_bool(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_bool();
|
||||
}
|
||||
|
||||
bool JsonObject::has_string(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_string();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i32();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u32();
|
||||
}
|
||||
|
||||
bool JsonObject::has_i64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i64();
|
||||
}
|
||||
|
||||
bool JsonObject::has_u64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u64();
|
||||
}
|
||||
|
||||
bool JsonObject::has_number(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_number();
|
||||
}
|
||||
|
||||
bool JsonObject::has_array(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_array();
|
||||
}
|
||||
|
||||
bool JsonObject::has_object(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_object();
|
||||
}
|
||||
|
||||
#ifndef KERNEL
|
||||
bool JsonObject::has_double(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_double();
|
||||
}
|
||||
#endif
|
||||
|
||||
void JsonObject::set(DeprecatedString const& key, JsonValue value)
|
||||
{
|
||||
m_members.set(key, move(value));
|
||||
}
|
||||
|
||||
bool JsonObject::remove(StringView key)
|
||||
{
|
||||
return m_members.remove(key);
|
||||
}
|
||||
|
||||
DeprecatedString JsonObject::to_deprecated_string() const
|
||||
{
|
||||
return serialized<StringBuilder>();
|
||||
}
|
||||
|
||||
}
|
138
AK/JsonObject.h
138
AK/JsonObject.h
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -22,123 +23,39 @@ class JsonObject {
|
|||
using CallbackErrorType = decltype(declval<Callback>()(declval<DeprecatedString const&>(), declval<JsonValue const&>()).release_error());
|
||||
|
||||
public:
|
||||
JsonObject() = default;
|
||||
~JsonObject() = default;
|
||||
JsonObject();
|
||||
~JsonObject();
|
||||
|
||||
JsonObject(JsonObject const& other)
|
||||
: m_members(other.m_members)
|
||||
{
|
||||
}
|
||||
JsonObject(JsonObject const& other);
|
||||
JsonObject(JsonObject&& other);
|
||||
|
||||
JsonObject(JsonObject&& other)
|
||||
: m_members(move(other.m_members))
|
||||
{
|
||||
}
|
||||
JsonObject& operator=(JsonObject const& other);
|
||||
JsonObject& operator=(JsonObject&& other);
|
||||
|
||||
JsonObject& operator=(JsonObject const& other)
|
||||
{
|
||||
if (this != &other)
|
||||
m_members = other.m_members;
|
||||
return *this;
|
||||
}
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] bool is_empty() const;
|
||||
|
||||
JsonObject& operator=(JsonObject&& other)
|
||||
{
|
||||
if (this != &other)
|
||||
m_members = move(other.m_members);
|
||||
return *this;
|
||||
}
|
||||
[[nodiscard]] JsonValue const& get_deprecated(StringView key) const;
|
||||
|
||||
[[nodiscard]] size_t size() const { return m_members.size(); }
|
||||
[[nodiscard]] bool is_empty() const { return m_members.is_empty(); }
|
||||
[[nodiscard]] JsonValue const* get_ptr(StringView key) const;
|
||||
|
||||
[[nodiscard]] JsonValue const& get_deprecated(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
static JsonValue* s_null_value { nullptr };
|
||||
if (!value) {
|
||||
if (!s_null_value)
|
||||
s_null_value = new JsonValue;
|
||||
return *s_null_value;
|
||||
}
|
||||
return *value;
|
||||
}
|
||||
[[nodiscard]] bool has(StringView key) const;
|
||||
|
||||
[[nodiscard]] JsonValue const* get_ptr(StringView key) const
|
||||
{
|
||||
auto it = m_members.find(key);
|
||||
if (it == m_members.end())
|
||||
return nullptr;
|
||||
return &(*it).value;
|
||||
}
|
||||
|
||||
[[nodiscard]] [[nodiscard]] bool has(StringView key) const
|
||||
{
|
||||
return m_members.contains(key);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_null(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_null();
|
||||
}
|
||||
[[nodiscard]] bool has_bool(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_bool();
|
||||
}
|
||||
[[nodiscard]] bool has_string(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_string();
|
||||
}
|
||||
[[nodiscard]] bool has_i32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i32();
|
||||
}
|
||||
[[nodiscard]] bool has_u32(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u32();
|
||||
}
|
||||
[[nodiscard]] bool has_i64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_i64();
|
||||
}
|
||||
[[nodiscard]] bool has_u64(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_u64();
|
||||
}
|
||||
[[nodiscard]] bool has_number(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_number();
|
||||
}
|
||||
[[nodiscard]] bool has_array(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_array();
|
||||
}
|
||||
[[nodiscard]] bool has_object(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_object();
|
||||
}
|
||||
[[nodiscard]] bool has_null(StringView key) const;
|
||||
[[nodiscard]] bool has_bool(StringView key) const;
|
||||
[[nodiscard]] bool has_string(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;
|
||||
#ifndef KERNEL
|
||||
[[nodiscard]] [[nodiscard]] bool has_double(StringView key) const
|
||||
{
|
||||
auto const* value = get_ptr(key);
|
||||
return value && value->is_double();
|
||||
}
|
||||
[[nodiscard]] bool has_double(StringView key) const;
|
||||
#endif
|
||||
|
||||
void set(DeprecatedString const& key, JsonValue value)
|
||||
{
|
||||
m_members.set(key, move(value));
|
||||
}
|
||||
void set(DeprecatedString const& key, JsonValue value);
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_member(Callback callback) const
|
||||
|
@ -155,10 +72,7 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
bool remove(StringView key)
|
||||
{
|
||||
return m_members.remove(key);
|
||||
}
|
||||
bool remove(StringView key);
|
||||
|
||||
template<typename Builder>
|
||||
typename Builder::OutputType serialized() const;
|
||||
|
@ -166,7 +80,7 @@ public:
|
|||
template<typename Builder>
|
||||
void serialize(Builder&) const;
|
||||
|
||||
[[nodiscard]] DeprecatedString to_deprecated_string() const { return serialized<StringBuilder>(); }
|
||||
[[nodiscard]] DeprecatedString to_deprecated_string() const;
|
||||
|
||||
private:
|
||||
OrderedHashMap<DeprecatedString, JsonValue> m_members;
|
||||
|
|
Loading…
Reference in a new issue