AK: De-duplicate the definition of the JSON value variant

This commit is contained in:
Timothy Flynn 2025-02-17 11:38:53 -05:00 committed by Tim Flynn
parent 06faa7b160
commit a8bc0aed4a
Notes: github-actions[bot] 2025-02-21 00:29:47 +00:00
2 changed files with 16 additions and 27 deletions

View file

@ -13,28 +13,16 @@
namespace AK {
namespace {
using JsonValueStorage = Variant<
Empty,
bool,
i64,
u64,
double,
ByteString,
NonnullOwnPtr<JsonArray>,
NonnullOwnPtr<JsonObject>>;
static ErrorOr<JsonValueStorage> clone(JsonValueStorage const& other)
static ErrorOr<JsonValue::Storage> clone(JsonValue::Storage const& other)
{
return other.visit(
[](NonnullOwnPtr<JsonArray> const& value) -> ErrorOr<JsonValueStorage> {
[](NonnullOwnPtr<JsonArray> const& value) -> ErrorOr<JsonValue::Storage> {
return TRY(try_make<JsonArray>(*value));
},
[](NonnullOwnPtr<JsonObject> const& value) -> ErrorOr<JsonValueStorage> {
[](NonnullOwnPtr<JsonObject> const& value) -> ErrorOr<JsonValue::Storage> {
return TRY(try_make<JsonObject>(*value));
},
[](auto const& value) -> ErrorOr<JsonValueStorage> { return JsonValueStorage(value); });
}
[](auto const& value) -> ErrorOr<JsonValue::Storage> { return JsonValue::Storage(value); });
}
JsonValue::JsonValue() = default;

View file

@ -9,8 +9,8 @@
#include <AK/ByteString.h>
#include <AK/Forward.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/StringBuilder.h>
namespace AK {
@ -26,6 +26,16 @@ public:
Object,
};
using Storage = Variant<
Empty,
bool,
i64,
u64,
double,
ByteString,
NonnullOwnPtr<JsonArray>,
NonnullOwnPtr<JsonObject>>;
static ErrorOr<JsonValue> from_string(StringView);
JsonValue();
@ -222,16 +232,7 @@ public:
bool equals(JsonValue const& other) const;
private:
Variant<
Empty,
bool,
i64,
u64,
double,
ByteString,
NonnullOwnPtr<JsonArray>,
NonnullOwnPtr<JsonObject>>
m_value;
Storage m_value;
};
template<>