JSON: Port JsonArray and JsonObject serialization to serializers

This way, primitive JsonValue serialization is still handled by
JsonValue::serialize(), but JsonArray and JsonObject serialization
always goes through serializer classes. This is no less efficient
if you have the whole JSON in memory already.
This commit is contained in:
Sergey Bugaev 2019-08-27 14:15:30 +03:00 committed by Andreas Kling
parent 56f5c14d86
commit 216b7b3b80
Notes: sideshowbarker 2024-07-19 12:29:34 +09:00
2 changed files with 6 additions and 18 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <AK/JsonArraySerializer.h>
#include <AK/JsonValue.h>
#include <AK/Vector.h>
@ -68,13 +69,8 @@ private:
template<typename Builder>
inline void JsonArray::serialize(Builder& builder) const
{
builder.append('[');
for (int i = 0; i < m_values.size(); ++i) {
m_values[i].serialize(builder);
if (i != size() - 1)
builder.append(',');
}
builder.append(']');
JsonArraySerializer serializer { builder };
for_each([&](auto& value) { serializer.add(value); });
}
template<typename Builder>

View File

@ -3,6 +3,7 @@
#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/JsonArray.h>
#include <AK/JsonObjectSerializer.h>
#include <AK/JsonValue.h>
namespace AK {
@ -79,19 +80,10 @@ private:
template<typename Builder>
inline void JsonObject::serialize(Builder& builder) const
{
int index = 0;
builder.append('{');
JsonObjectSerializer serializer { builder };
for_each_member([&](auto& key, auto& value) {
builder.append('"');
builder.append(key);
builder.append('"');
builder.append(':');
value.serialize(builder);
if (index != size() - 1)
builder.append(',');
++index;
serializer.add(key, value);
});
builder.append('}');
}
template<typename Builder>