1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +00:00

AK: Store JsonValue's value in AK::Variant

This commit is contained in:
Dan Klishch 2023-11-15 03:03:07 -05:00 committed by Andrew Kaster
parent 05e53b5fd3
commit 88af15d513
3 changed files with 137 additions and 249 deletions

View file

@ -138,44 +138,18 @@ inline typename Builder::OutputType JsonObject::serialized() const
template<typename Builder>
inline void JsonValue::serialize(Builder& builder) const
{
switch (m_type) {
case Type::String: {
builder.append('\"');
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
builder.append('\"');
} break;
case Type::Array:
m_value.as_array->serialize(builder);
break;
case Type::Object:
m_value.as_object->serialize(builder);
break;
case Type::Bool:
builder.append(m_value.as_bool ? "true"sv : "false"sv);
break;
#if !defined(KERNEL)
case Type::Double:
builder.appendff("{}", m_value.as_double);
break;
#endif
case Type::Int32:
builder.appendff("{}", m_value.as_i32);
break;
case Type::Int64:
builder.appendff("{}", m_value.as_i64);
break;
case Type::UnsignedInt32:
builder.appendff("{}", m_value.as_u32);
break;
case Type::UnsignedInt64:
builder.appendff("{}", m_value.as_u64);
break;
case Type::Null:
builder.append("null"sv);
break;
default:
VERIFY_NOT_REACHED();
}
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<typename Builder>