Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-11-14 23:35:22 +0100
committerMartin Hořeňovský2023-11-14 23:35:22 +0100
commit733b901dd26110039466c04a74633a5709a1229e (patch)
tree4110b016860a8dc1ecb356e6ff46ed03eb26d80b /src
parent7bf136b501fef3e189b45091e8a4757a5eaef31f (diff)
downloadcatch3-733b901dd26110039466c04a74633a5709a1229e.tar.gz
Fix special character escaping in JsonWriter
Diffstat (limited to 'src')
-rw-r--r--src/catch2/internal/catch_jsonwriter.hpp20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/catch2/internal/catch_jsonwriter.hpp b/src/catch2/internal/catch_jsonwriter.hpp
index 9cac3de6..1b6ac967 100644
--- a/src/catch2/internal/catch_jsonwriter.hpp
+++ b/src/catch2/internal/catch_jsonwriter.hpp
@@ -47,10 +47,26 @@ namespace Catch {
m_sstream << value;
while ( true ) {
char c = m_sstream.get();
-
if ( m_sstream.eof() ) { break; }
+
+ // see https://www.json.org/json-en.html, string definition for the escape list
if ( c == '"' ) {
- m_os << '\\' << '"';
+ m_os << "\\\"";
+ } else if ( c == '\\' ) {
+ m_os << "\\\\";
+ // Note that while forward slash _can_ be escaped, it
+ // does not have to be, if JSON is not further embedded
+ // somewhere where forward slash is meaningful.
+ } else if ( c == '\b' ) {
+ m_os << "\\b";
+ } else if ( c == '\f' ) {
+ m_os << "\\f";
+ } else if ( c == '\n' ) {
+ m_os << "\\n";
+ } else if ( c == '\r' ) {
+ m_os << "\\r";
+ } else if ( c == '\t') {
+ m_os << "\\t";
} else {
m_os << c;
}