AK+Kernel: Escape JSON keys & values

Grab the escaping logic from JSON string value serialization and use
it for serializing all keys and values.

Fixes #3917.
This commit is contained in:
Andreas Kling 2020-11-02 12:56:36 +01:00
parent ee21a724c7
commit 5e164052f6
Notes: sideshowbarker 2024-07-19 01:35:30 +09:00
7 changed files with 68 additions and 33 deletions

View file

@ -61,7 +61,7 @@ public:
{
begin_item();
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}
@ -69,7 +69,7 @@ public:
{
begin_item();
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}
@ -77,7 +77,7 @@ public:
{
begin_item();
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}

View file

@ -158,33 +158,8 @@ inline void JsonValue::serialize(Builder& builder) const
{
switch (m_type) {
case Type::String: {
auto size = m_value.as_string->length();
builder.append("\"");
for (size_t i = 0; i < size; i++) {
char ch = m_value.as_string->characters()[i];
switch (ch) {
case '\e':
builder.append("\\u001B");
break;
case '\b':
builder.append("\\b");
break;
case '\n':
builder.append("\\n");
break;
case '\t':
builder.append("\\t");
break;
case '\"':
builder.append("\\\"");
break;
case '\\':
builder.append("\\\\");
break;
default:
builder.append(ch);
}
}
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
builder.append("\"");
} break;
case Type::Array:

View file

@ -59,7 +59,7 @@ public:
{
begin_item(key);
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}
@ -67,7 +67,7 @@ public:
{
begin_item(key);
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}
@ -75,7 +75,7 @@ public:
{
begin_item(key);
m_builder.append('"');
m_builder.append(value);
m_builder.append_escaped_for_json(value);
m_builder.append('"');
}
@ -154,7 +154,7 @@ private:
m_empty = false;
m_builder.append('"');
m_builder.append(key);
m_builder.append_escaped_for_json(key);
m_builder.append("\":");
}

View file

@ -144,4 +144,32 @@ void StringBuilder::append(const Utf32View& utf32_view)
}
}
void StringBuilder::append_escaped_for_json(const StringView& string)
{
for (auto ch : string) {
switch (ch) {
case '\e':
append("\\u001B");
break;
case '\b':
append("\\b");
break;
case '\n':
append("\\n");
break;
case '\t':
append("\\t");
break;
case '\"':
append("\\\"");
break;
case '\\':
append("\\\\");
break;
default:
append(ch);
}
}
}
}

View file

@ -49,6 +49,8 @@ public:
void appendf(const char*, ...);
void appendvf(const char*, va_list);
void append_escaped_for_json(const StringView&);
template<typename... Parameters>
void appendff(StringView fmtstr, const Parameters&... parameters)
{

View file

@ -93,4 +93,32 @@ void KBufferBuilder::appendf(const char* fmt, ...)
va_end(ap);
}
void KBufferBuilder::append_escaped_for_json(const StringView& string)
{
for (auto ch : string) {
switch (ch) {
case '\e':
append("\\u001B");
break;
case '\b':
append("\\b");
break;
case '\n':
append("\\n");
break;
case '\t':
append("\\t");
break;
case '\"':
append("\\\"");
break;
case '\\':
append("\\\\");
break;
default:
append(ch);
}
}
}
}

View file

@ -45,6 +45,8 @@ public:
void appendf(const char*, ...);
void appendvf(const char*, va_list);
void append_escaped_for_json(const StringView&);
template<typename... Parameters>
void appendff(StringView fmtstr, const Parameters&... parameters)
{