1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

JSON: Templatize the JSON serialization code

This makes it possible to use something other than a StringBuilder for
serialization (and to produce something other than a String.) :^)
This commit is contained in:
Andreas Kling 2019-08-07 21:28:07 +02:00
parent 43ec733b61
commit f6998b1817
11 changed files with 145 additions and 109 deletions

View file

@ -158,50 +158,6 @@ void JsonValue::clear()
m_value.as_string = nullptr;
}
void JsonValue::serialize(StringBuilder& builder) const
{
switch (m_type) {
case Type::String:
builder.appendf("\"%s\"", m_value.as_string->characters());
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" : "false");
break;
#ifndef KERNEL
case Type::Double:
builder.appendf("%g", m_value.as_double);
break;
#endif
case Type::Int:
builder.appendf("%d", m_value.as_int);
break;
case Type::UnsignedInt:
builder.appendf("%u", m_value.as_uint);
break;
case Type::Undefined:
builder.append("undefined");
break;
case Type::Null:
builder.append("null");
break;
default:
ASSERT_NOT_REACHED();
}
}
String JsonValue::serialized() const
{
StringBuilder builder;
serialize(builder);
return builder.to_string();
}
JsonValue JsonValue::from_string(const StringView& input)
{
return JsonParser(input).parse();