1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

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
6 changed files with 77 additions and 16 deletions

View file

@ -1,15 +1,24 @@
#include <AK/JsonArray.h> #include <AK/JsonArray.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
String JsonArray::to_string() const namespace AK {
void JsonArray::to_string(StringBuilder& builder) const
{ {
StringBuilder builder;
builder.append('['); builder.append('[');
for (int i = 0; i < m_values.size(); ++i) { 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) if (i != size() - 1)
builder.append(','); builder.append(',');
} }
builder.append(']'); builder.append(']');
}
String JsonArray::to_string() const
{
StringBuilder builder;
to_string(builder);
return builder.to_string(); return builder.to_string();
} }
}

View file

@ -3,6 +3,8 @@
#include <AK/JsonValue.h> #include <AK/JsonValue.h>
#include <AK/Vector.h> #include <AK/Vector.h>
namespace AK {
class JsonArray { class JsonArray {
public: public:
JsonArray() {} JsonArray() {}
@ -18,7 +20,12 @@ public:
void append(const JsonValue& value) { m_values.append(value); } void append(const JsonValue& value) { m_values.append(value); }
String to_string() const; String to_string() const;
void to_string(StringBuilder&) const;
private: private:
Vector<JsonValue> m_values; Vector<JsonValue> m_values;
}; };
}
using AK::JsonArray;

View file

@ -1,9 +1,10 @@
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
String JsonObject::to_string() const namespace AK {
void JsonObject::to_string(StringBuilder& builder) const
{ {
StringBuilder builder;
int index = 0; int index = 0;
builder.append('{'); builder.append('{');
for_each_member([&] (auto& key, auto& value) { for_each_member([&] (auto& key, auto& value) {
@ -11,11 +12,19 @@ String JsonObject::to_string() const
builder.append(key); builder.append(key);
builder.append('"'); builder.append('"');
builder.append(':'); builder.append(':');
builder.append(value.to_string()); value.to_string(builder);
if (index != size() - 1) if (index != size() - 1)
builder.append(','); builder.append(',');
++index; ++index;
}); });
builder.append('}'); builder.append('}');
}
String JsonObject::to_string() const
{
StringBuilder builder;
to_string(builder);
return builder.to_string(); return builder.to_string();
} }
}

View file

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

View file

@ -1,6 +1,9 @@
#include <AK/JsonArray.h> #include <AK/JsonArray.h>
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/JsonValue.h> #include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
namespace AK {
JsonValue::JsonValue(Type type) JsonValue::JsonValue(Type type)
: m_type(type) : m_type(type)
@ -116,25 +119,43 @@ void JsonValue::clear()
m_value.as_string = nullptr; m_value.as_string = nullptr;
} }
String JsonValue::to_string() const void JsonValue::to_string(StringBuilder& builder) const
{ {
switch (m_type) { switch (m_type) {
case Type::String: case Type::String:
return String::format("\"%s\"", m_value.as_string->characters()); builder.appendf("\"%s\"", m_value.as_string->characters());
break;
case Type::Array: case Type::Array:
return m_value.as_array->to_string(); m_value.as_array->to_string(builder);
break;
case Type::Object: case Type::Object:
return m_value.as_object->to_string(); m_value.as_object->to_string(builder);
break;
case Type::Bool: case Type::Bool:
return m_value.as_bool ? "true" : "false"; builder.append(m_value.as_bool ? "true" : "false");
break;
case Type::Double: case Type::Double:
return String::format("%g", m_value.as_double); builder.appendf("%g", m_value.as_double);
break;
case Type::Int: case Type::Int:
return String::format("%d", m_value.as_int); builder.appendf("%d", m_value.as_int);
break;
case Type::Undefined: case Type::Undefined:
return "undefined"; builder.append("undefined");
break;
case Type::Null: 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();
}
} }

View file

@ -2,8 +2,11 @@
#include <AK/AKString.h> #include <AK/AKString.h>
namespace AK {
class JsonArray; class JsonArray;
class JsonObject; class JsonObject;
class StringBuilder;
class JsonValue { class JsonValue {
public: public:
@ -35,6 +38,7 @@ public:
JsonValue(const JsonObject&); JsonValue(const JsonObject&);
String to_string() const; String to_string() const;
void to_string(StringBuilder&) const;
private: private:
void clear(); void clear();
@ -51,3 +55,7 @@ private:
bool as_bool; bool as_bool;
} m_value; } m_value;
}; };
}
using AK::JsonValue;