Permalink
Browse files

Properly escaping JSON (except for unicode) (https://reviews.apache.o…

…rg/r/5104).

git-svn-id: https://svn.apache.org/repos/asf/incubator/mesos/trunk@1338430 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 22bfadc commit a6d2ec9bb274198ee92704925f18ba25eb0e7124 @benh benh committed May 14, 2012
Showing with 18 additions and 1 deletion.
  1. +18 −1 src/common/json.hpp
View
@@ -108,7 +108,24 @@ struct Renderer : boost::static_visitor<>
void operator () (const String& string) const
{
- out << "\"" << string.value << "\"";
+ // TODO(benh): This escaping DOES NOT handle unicode.
+ out << "\"";
+ std::string::const_iterator iterator = string.value.begin();
+ while (iterator != string.value.end()) {
+ switch (*iterator) {
+ case '"': out << "\\\""; break;
+ case '\\': out << "\\\\"; break;
+ case '/': out << "\\/"; break;
+ case '\b': out << "\\b"; break;
+ case '\f': out << "\\f"; break;
+ case '\n': out << "\\n"; break;
+ case '\r': out << "\\r"; break;
+ case '\t': out << "\\t"; break;
+ default: out << *iterator; break;
+ }
+ ++iterator;
+ }
+ out << "\"";
}
void operator () (const Number& number) const

0 comments on commit a6d2ec9

Please sign in to comment.