From d61853430a3df8914995ae35ac7a0795827d7a87 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 30 Sep 2014 00:41:18 +0200 Subject: [PATCH] Support control characters in JSON output --- src/libexpr/value-to-json.cc | 3 +++ src/libexpr/value-to-json.hh | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/libexpr/value-to-json.cc b/src/libexpr/value-to-json.cc index a2004df5c..d1ec9b566 100644 --- a/src/libexpr/value-to-json.cc +++ b/src/libexpr/value-to-json.cc @@ -3,6 +3,7 @@ #include "util.hh" #include +#include namespace nix { @@ -16,6 +17,8 @@ void escapeJSON(std::ostream & str, const string & s) else if (*i == '\n') str << "\\n"; else if (*i == '\r') str << "\\r"; else if (*i == '\t') str << "\\t"; + else if (*i >= 0 && *i < 32) + str << "\\u" << std::setfill('0') << std::setw(4) << std::hex << (uint16_t) *i << std::dec; else str << *i; str << "\""; } diff --git a/src/libexpr/value-to-json.hh b/src/libexpr/value-to-json.hh index e3a97efe4..f6796f205 100644 --- a/src/libexpr/value-to-json.hh +++ b/src/libexpr/value-to-json.hh @@ -36,6 +36,11 @@ struct JSONObject attr(s); escapeJSON(str, t); } + void attr(const string & s, int n) + { + attr(s); + str << n; + } }; struct JSONList