From cb20a75b328d9d9566953a6aa1410595a611a99e Mon Sep 17 00:00:00 2001 From: Sergey Linev <S.Linev@gsi.de> Date: Fri, 20 Feb 2015 14:38:11 +0100 Subject: [PATCH] json: correctly process escape characters in the JSON string Signed-off-by: Bertrand Bellenot <bertrand.bellenot@cern.ch> --- net/http/src/TRootSnifferStore.cxx | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/net/http/src/TRootSnifferStore.cxx b/net/http/src/TRootSnifferStore.cxx index 2dd5f13369b..c3b9c974249 100644 --- a/net/http/src/TRootSnifferStore.cxx +++ b/net/http/src/TRootSnifferStore.cxx @@ -144,10 +144,25 @@ void TRootSnifferStoreJson::SetField(Int_t lvl, const char *field, fBuf->Append(","); if (!fCompact) fBuf->Append("\n"); fBuf->Append(TString::Format("%*s\"%s\"%s", fCompact ? 0 : lvl * 4 + 2, "", field, (fCompact ? ":" : " : "))); - if (with_quotes) { - fBuf->Append(TString::Format("\"%s\"", value)); - } else { + if (!with_quotes) { fBuf->Append(value); + } else { + fBuf->Append("\""); + for (const char *v = value; *v != 0; v++) { + switch (*v) { + case '\n': + fBuf->Append("\\\\n"); + break; + case '\t': + fBuf->Append("\\\\t"); + break; + default: + fBuf->Append(*v); + // double escape character, only keep \' and \" sequences untouched + if ((*v == '\\') && (*(v+1) != '\'') && (*(v+1) != '\"')) fBuf->Append("\\"); + } + } + fBuf->Append("\""); } } -- GitLab