aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Giessen <charles@lunarg.com>2022-10-27 14:03:15 -0600
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>2022-10-28 17:43:29 -0600
commit25fc297edb10221e321f51addc5fff40023b0dcc (patch)
treea7eab1a6385ccded73266c5d8cdfbe92d4e4d560
parent1ac532e3f48eec4dfa9a0235edc05559b0857ad4 (diff)
downloadusermoji-25fc297edb10221e321f51addc5fff40023b0dcc.tar.xz
vulkaninfo: Escape strings in JSON output
JSON output breaks if escape sequences are in the various strings output by drivers but aren't properly handles. This commit scans over each string and inserts backslashes when necessary.
-rw-r--r--vulkaninfo/outputprinter.h44
1 files changed, 42 insertions, 2 deletions
diff --git a/vulkaninfo/outputprinter.h b/vulkaninfo/outputprinter.h
index 26dd2a29..4dc36ef2 100644
--- a/vulkaninfo/outputprinter.h
+++ b/vulkaninfo/outputprinter.h
@@ -582,7 +582,7 @@ class Printer {
break;
case (OutputType::json):
case (OutputType::vkconfig_output):
- PrintElement("\"" + string + "\"", value_description);
+ PrintElement("\"" + EscapeJSONCString(string) + "\"");
default:
break;
}
@@ -709,6 +709,46 @@ class Printer {
get_top().set_next_subheader = false;
}
}
+
+ // Replace special characters in strings with their escaped versions.
+ // <https://www.json.org/json-en.html>
+ std::string EscapeJSONCString(std::string string) {
+ if (output_type != OutputType::json) return string;
+ std::string out{};
+ for (size_t i = 0; i < string.size(); i++) {
+ char c = string[i];
+ char out_c = c;
+ switch (c) {
+ case '\"':
+ case '\\':
+ out.push_back('\\');
+ break;
+ case '\b':
+ out.push_back('\\');
+ out_c = 'b';
+ break;
+ case '\f':
+ out.push_back('\\');
+ out_c = 'f';
+ break;
+ case '\n':
+ out.push_back('\\');
+ out_c = 'n';
+ break;
+ case '\r':
+ out.push_back('\\');
+ out_c = 'r';
+ break;
+ case '\t':
+ out.push_back('\\');
+ out_c = 't';
+ break;
+ }
+ out.push_back(out_c);
+ }
+
+ return out;
+ }
};
// Purpose: When a Printer starts an object or array it will automatically indent the output. This isn't
// always desired, requiring a manual decrease of indention. This wrapper facilitates that while also
@@ -745,4 +785,4 @@ class ArrayWrapper {
private:
Printer &p;
-}; \ No newline at end of file
+};