1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +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

@ -44,8 +44,13 @@ public:
void append(const JsonValue& value) { m_values.append(value); }
void append(JsonValue&& value) { m_values.append(move(value)); }
String serialized() const;
void serialize(StringBuilder&) const;
template<typename Builder>
typename Builder::OutputType serialized() const;
template<typename Builder>
void serialize(Builder&) const;
String to_string() const { return serialized<StringBuilder>(); }
template<typename Callback>
void for_each(Callback callback) const
@ -60,6 +65,26 @@ private:
Vector<JsonValue> m_values;
};
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(']');
}
template<typename Builder>
inline typename Builder::OutputType JsonArray::serialized() const
{
Builder builder;
serialize(builder);
return builder.build();
}
}
using AK::JsonArray;