Index: src/ostreams.cc |
diff --git a/src/ostreams.cc b/src/ostreams.cc |
index 45f41bb012089fd52a8beb14d4e6e4ad37cabb9f..a0a548b6070dafbc6f45f8a4c970bef1a4ba5220 100644 |
--- a/src/ostreams.cc |
+++ b/src/ostreams.cc |
@@ -61,6 +61,14 @@ std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) { |
return os << buf; |
} |
+std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c, |
+ bool (*pred)(uint16_t)) { |
+ // JSON does not allow \x99; must use \u0099. |
+ char buf[10]; |
+ const char* format = pred(c) ? "%c" : "\\u%04x"; |
+ snprintf(buf, sizeof(buf), format, c); |
+ return os << buf; |
+} |
std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) { |
if (c <= String::kMaxUtf16CodeUnit) { |
@@ -84,7 +92,7 @@ std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) { |
if (c.value == '\r') return os << "\\r"; |
if (c.value == '\t') return os << "\\t"; |
if (c.value == '\"') return os << "\\\""; |
- return PrintUC16(os, c.value, IsOK); |
+ return PrintUC16ForJSON(os, c.value, IsOK); |
} |