summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-11-02 12:56:36 +0100
committerAndreas Kling <kling@serenityos.org>2020-11-02 12:56:36 +0100
commit5e164052f6432d7da8793994cc4eafce6538cb14 (patch)
treed3ef7f56eb2ac30477ec1be7d335c855af1ea8a5 /Kernel
parentee21a724c75e5d398c9c227e2aec64452f550749 (diff)
downloadserenity-5e164052f6432d7da8793994cc4eafce6538cb14.zip
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.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/KBufferBuilder.cpp28
-rw-r--r--Kernel/KBufferBuilder.h2
2 files changed, 30 insertions, 0 deletions
diff --git a/Kernel/KBufferBuilder.cpp b/Kernel/KBufferBuilder.cpp
index 1c3129be4e..f3480b10a2 100644
--- a/Kernel/KBufferBuilder.cpp
+++ b/Kernel/KBufferBuilder.cpp
@@ -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);
+ }
+ }
+}
+
}
diff --git a/Kernel/KBufferBuilder.h b/Kernel/KBufferBuilder.h
index affef2bbe8..9335620a02 100644
--- a/Kernel/KBufferBuilder.h
+++ b/Kernel/KBufferBuilder.h
@@ -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)
{