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:
Timothy Flynn 2025-02-17 15:08:17 -05:00 committed by Tim Flynn
commit fe2dff4944
Notes: github-actions[bot] 2025-02-21 00:28:53 +00:00
15 changed files with 98 additions and 95 deletions

View file

@ -8,12 +8,10 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/Concepts.h>
#include <AK/Error.h>
#include <AK/HashMap.h>
#include <AK/JsonArray.h>
#include <AK/JsonObjectSerializer.h>
#include <AK/JsonValue.h>
#include <AK/String.h>
@ -107,59 +105,13 @@ public:
bool remove(StringView key);
template<typename Builder>
typename Builder::OutputType serialized() const;
template<typename Builder>
void serialize(Builder&) const;
String serialized() const;
void serialize(StringBuilder&) const;
private:
OrderedHashMap<String, JsonValue> m_members;
};
template<typename Builder>
inline void JsonObject::serialize(Builder& builder) const
{
auto serializer = MUST(JsonObjectSerializer<>::try_create(builder));
for_each_member([&](auto& key, auto& value) {
MUST(serializer.add(key, value));
});
MUST(serializer.finish());
}
template<typename Builder>
inline typename Builder::OutputType JsonObject::serialized() const
{
Builder builder;
serialize(builder);
return builder.to_byte_string();
}
template<typename Builder>
inline void JsonValue::serialize(Builder& 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);
});
}
template<typename Builder>
inline typename Builder::OutputType JsonValue::serialized() const
{
Builder builder;
serialize(builder);
return builder.to_byte_string();
}
}
#if USING_AK_GLOBALLY