diff options
author | Andreas Kling <kling@serenityos.org> | 2020-11-02 12:56:36 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-02 12:56:36 +0100 |
commit | 5e164052f6432d7da8793994cc4eafce6538cb14 (patch) | |
tree | d3ef7f56eb2ac30477ec1be7d335c855af1ea8a5 /Kernel | |
parent | ee21a724c75e5d398c9c227e2aec64452f550749 (diff) | |
download | serenity-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.cpp | 28 | ||||
-rw-r--r-- | Kernel/KBufferBuilder.h | 2 |
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) { |