mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-06 08:36:15 +00:00
AK+Everywhere: Convert JSON value serialization to String
This removes the use of StringBuilder::OutputType (which was ByteString, and only used by the JSON classes). And it removes the StringBuilder template parameter from the serialization methods; this was only ever used with StringBuilder, so a template is pretty overkill here.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonParser.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace AK {
|
||||
@@ -192,4 +193,28 @@ ErrorOr<JsonValue> JsonValue::from_string(StringView input)
|
||||
return JsonParser(input).parse();
|
||||
}
|
||||
|
||||
String JsonValue::serialized() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
serialize(builder);
|
||||
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
|
||||
void JsonValue::serialize(StringBuilder& 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); },
|
||||
[&](String const& value) {
|
||||
builder.append('\"');
|
||||
builder.append_escaped_for_json(value.bytes());
|
||||
builder.append('\"');
|
||||
},
|
||||
[&](auto const& array_or_object) {
|
||||
array_or_object->serialize(builder);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user