From ee347effac246618f282d64a221306b7452009f0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 17 Jun 2019 21:34:12 +0200 Subject: [PATCH] AK: Use a single StringBuilder throughout JSON serialization. --- AK/JsonArray.cpp | 15 ++++++++++++--- AK/JsonArray.h | 7 +++++++ AK/JsonObject.cpp | 15 ++++++++++++--- AK/JsonObject.h | 7 +++++++ AK/JsonValue.cpp | 41 +++++++++++++++++++++++++++++++---------- AK/JsonValue.h | 8 ++++++++ 6 files changed, 77 insertions(+), 16 deletions(-) diff --git a/AK/JsonArray.cpp b/AK/JsonArray.cpp index 49188889e93..2cba65cbf39 100644 --- a/AK/JsonArray.cpp +++ b/AK/JsonArray.cpp @@ -1,15 +1,24 @@ #include #include -String JsonArray::to_string() const +namespace AK { + +void JsonArray::to_string(StringBuilder& builder) const { - StringBuilder builder; builder.append('['); for (int i = 0; i < m_values.size(); ++i) { - builder.append(m_values[i].to_string()); + m_values[i].to_string(builder); if (i != size() - 1) builder.append(','); } builder.append(']'); +} + +String JsonArray::to_string() const +{ + StringBuilder builder; + to_string(builder); return builder.to_string(); } + +} diff --git a/AK/JsonArray.h b/AK/JsonArray.h index 38eaad09c6f..e67a731fe8b 100644 --- a/AK/JsonArray.h +++ b/AK/JsonArray.h @@ -3,6 +3,8 @@ #include #include +namespace AK { + class JsonArray { public: JsonArray() {} @@ -18,7 +20,12 @@ public: void append(const JsonValue& value) { m_values.append(value); } String to_string() const; + void to_string(StringBuilder&) const; private: Vector m_values; }; + +} + +using AK::JsonArray; diff --git a/AK/JsonObject.cpp b/AK/JsonObject.cpp index 11f01bd235f..7817dc937e8 100644 --- a/AK/JsonObject.cpp +++ b/AK/JsonObject.cpp @@ -1,9 +1,10 @@ #include #include -String JsonObject::to_string() const +namespace AK { + +void JsonObject::to_string(StringBuilder& builder) const { - StringBuilder builder; int index = 0; builder.append('{'); for_each_member([&] (auto& key, auto& value) { @@ -11,11 +12,19 @@ String JsonObject::to_string() const builder.append(key); builder.append('"'); builder.append(':'); - builder.append(value.to_string()); + value.to_string(builder); if (index != size() - 1) builder.append(','); ++index; }); builder.append('}'); +} + +String JsonObject::to_string() const +{ + StringBuilder builder; + to_string(builder); return builder.to_string(); } + +} diff --git a/AK/JsonObject.h b/AK/JsonObject.h index ab01ae6675f..42810e8497a 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -4,6 +4,8 @@ #include #include +namespace AK { + class JsonObject { public: JsonObject() { } @@ -39,7 +41,12 @@ public: } String to_string() const; + void to_string(StringBuilder&) const; private: HashMap m_members; }; + +} + +using AK::JsonObject; diff --git a/AK/JsonValue.cpp b/AK/JsonValue.cpp index 4ed88bb16d2..6b6e10d9a80 100644 --- a/AK/JsonValue.cpp +++ b/AK/JsonValue.cpp @@ -1,6 +1,9 @@ #include #include #include +#include + +namespace AK { JsonValue::JsonValue(Type type) : m_type(type) @@ -116,25 +119,43 @@ void JsonValue::clear() m_value.as_string = nullptr; } -String JsonValue::to_string() const +void JsonValue::to_string(StringBuilder& builder) const { switch (m_type) { case Type::String: - return String::format("\"%s\"", m_value.as_string->characters()); + builder.appendf("\"%s\"", m_value.as_string->characters()); + break; case Type::Array: - return m_value.as_array->to_string(); + m_value.as_array->to_string(builder); + break; case Type::Object: - return m_value.as_object->to_string(); + m_value.as_object->to_string(builder); + break; case Type::Bool: - return m_value.as_bool ? "true" : "false"; + builder.append(m_value.as_bool ? "true" : "false"); + break; case Type::Double: - return String::format("%g", m_value.as_double); + builder.appendf("%g", m_value.as_double); + break; case Type::Int: - return String::format("%d", m_value.as_int); + builder.appendf("%d", m_value.as_int); + break; case Type::Undefined: - return "undefined"; + builder.append("undefined"); + break; case Type::Null: - return "null"; + builder.append("null"); + break; + default: + ASSERT_NOT_REACHED(); } - ASSERT_NOT_REACHED(); +} + +String JsonValue::to_string() const +{ + StringBuilder builder; + to_string(builder); + return builder.to_string(); +} + } diff --git a/AK/JsonValue.h b/AK/JsonValue.h index 5bdfb9a5d79..23d96326fe4 100644 --- a/AK/JsonValue.h +++ b/AK/JsonValue.h @@ -2,8 +2,11 @@ #include +namespace AK { + class JsonArray; class JsonObject; +class StringBuilder; class JsonValue { public: @@ -35,6 +38,7 @@ public: JsonValue(const JsonObject&); String to_string() const; + void to_string(StringBuilder&) const; private: void clear(); @@ -51,3 +55,7 @@ private: bool as_bool; } m_value; }; + +} + +using AK::JsonValue;