AK: Use a single StringBuilder throughout JSON serialization.

This commit is contained in:
Andreas Kling 2019-06-17 21:34:12 +02:00
parent 3b9fcab1af
commit ee347effac
Notes: sideshowbarker 2024-07-19 13:33:30 +09:00
6 changed files with 77 additions and 16 deletions

View file

@ -1,15 +1,24 @@
#include <AK/JsonArray.h>
#include <AK/StringBuilder.h>
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();
}
}

View file

@ -3,6 +3,8 @@
#include <AK/JsonValue.h>
#include <AK/Vector.h>
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<JsonValue> m_values;
};
}
using AK::JsonArray;

View file

@ -1,9 +1,10 @@
#include <AK/JsonObject.h>
#include <AK/StringBuilder.h>
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();
}
}

View file

@ -4,6 +4,8 @@
#include <AK/HashMap.h>
#include <AK/JsonValue.h>
namespace AK {
class JsonObject {
public:
JsonObject() { }
@ -39,7 +41,12 @@ public:
}
String to_string() const;
void to_string(StringBuilder&) const;
private:
HashMap<String, JsonValue> m_members;
};
}
using AK::JsonObject;

View file

@ -1,6 +1,9 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
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();
}
}
String JsonValue::to_string() const
{
StringBuilder builder;
to_string(builder);
return builder.to_string();
}
}

View file

@ -2,8 +2,11 @@
#include <AK/AKString.h>
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;