From 4828be7fc42e9ba01935edf878ff4abedf5eb0b0 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sat, 30 Jul 2022 23:56:41 +0200 Subject: [PATCH] Fix double escaping in the metadata of FORMAT JSON --- src/Formats/JSONUtils.cpp | 7 ++++++- .../Formats/Impl/JSONRowOutputFormat.h | 2 +- .../02375_double_escaping_json.reference | 18 ++++++++++++++++++ .../0_stateless/02375_double_escaping_json.sql | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02375_double_escaping_json.reference create mode 100644 tests/queries/0_stateless/02375_double_escaping_json.sql diff --git a/src/Formats/JSONUtils.cpp b/src/Formats/JSONUtils.cpp index 1ac58760516..7c94b122096 100644 --- a/src/Formats/JSONUtils.cpp +++ b/src/Formats/JSONUtils.cpp @@ -529,7 +529,12 @@ namespace JSONUtils writeObjectStart(out, 2); writeTitle("name", out, 3); - writeDoubleQuoted(fields[i].name, out); + + /// The field names are pre-escaped to be put into JSON string literal. + writeChar('"', out); + writeString(fields[i].name, out); + writeChar('"', out); + writeFieldDelimiter(out); writeTitle("type", out, 3); writeJSONString(fields[i].type->getName(), out, settings); diff --git a/src/Processors/Formats/Impl/JSONRowOutputFormat.h b/src/Processors/Formats/Impl/JSONRowOutputFormat.h index 3459cc1b7a6..66b8d0f682e 100644 --- a/src/Processors/Formats/Impl/JSONRowOutputFormat.h +++ b/src/Processors/Formats/Impl/JSONRowOutputFormat.h @@ -72,7 +72,7 @@ protected: size_t field_number = 0; size_t row_count = 0; - NamesAndTypes fields; + NamesAndTypes fields; /// The field names are pre-escaped to be put into JSON string literal. Statistics statistics; FormatSettings settings; diff --git a/tests/queries/0_stateless/02375_double_escaping_json.reference b/tests/queries/0_stateless/02375_double_escaping_json.reference new file mode 100644 index 00000000000..23dbe1a59df --- /dev/null +++ b/tests/queries/0_stateless/02375_double_escaping_json.reference @@ -0,0 +1,18 @@ +{ + "meta": + [ + { + "name": "\"", + "type": "String" + } + ], + + "data": + [ + { + "\"": "\\" + } + ], + + "rows": 1 +} diff --git a/tests/queries/0_stateless/02375_double_escaping_json.sql b/tests/queries/0_stateless/02375_double_escaping_json.sql new file mode 100644 index 00000000000..ecfb24fca2e --- /dev/null +++ b/tests/queries/0_stateless/02375_double_escaping_json.sql @@ -0,0 +1 @@ +SELECT '\\' AS `"` FORMAT JSON SETTINGS output_format_write_statistics = 0;