Index: base/json/json_writer.cc |
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc |
index d600663821913009e9abe870481878a06d4fae48..6a9cc6aa470b592e6a76919236fe71706fd93036 100644 |
--- a/base/json/json_writer.cc |
+++ b/base/json/json_writer.cc |
@@ -21,24 +21,28 @@ |
static const char kPrettyPrintLineEnding[] = "\n"; |
#endif |
-// static |
+/* static */ |
+const char* JSONWriter::kEmptyArray = "[]"; |
+ |
+/* static */ |
void JSONWriter::Write(const Value* const node, std::string* json) { |
WriteWithOptions(node, 0, json); |
} |
-// static |
+/* static */ |
void JSONWriter::WriteWithOptions(const Value* const node, int options, |
std::string* json) { |
json->clear(); |
// Is there a better way to estimate the size of the output? |
json->reserve(1024); |
+ bool escape = !(options & OPTIONS_DO_NOT_ESCAPE); |
bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES); |
bool omit_double_type_preservation = |
!!(options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION); |
bool pretty_print = !!(options & OPTIONS_PRETTY_PRINT); |
- JSONWriter writer(omit_binary_values, omit_double_type_preservation, |
+ JSONWriter writer(escape, omit_binary_values, omit_double_type_preservation, |
pretty_print, json); |
writer.BuildJSONString(node, 0); |
@@ -46,10 +50,11 @@ |
json->append(kPrettyPrintLineEnding); |
} |
-JSONWriter::JSONWriter(bool omit_binary_values, |
+JSONWriter::JSONWriter(bool escape, bool omit_binary_values, |
bool omit_double_type_preservation, bool pretty_print, |
std::string* json) |
- : omit_binary_values_(omit_binary_values), |
+ : escape_(escape), |
+ omit_binary_values_(omit_binary_values), |
omit_double_type_preservation_(omit_double_type_preservation), |
pretty_print_(pretty_print), |
json_string_(json) { |
@@ -118,7 +123,11 @@ |
std::string value; |
bool result = node->GetAsString(&value); |
DCHECK(result); |
- EscapeJSONString(value, true, json_string_); |
+ if (escape_) { |
+ JsonDoubleQuote(UTF8ToUTF16(value), true, json_string_); |
+ } else { |
+ JsonDoubleQuote(value, true, json_string_); |
+ } |
break; |
} |
@@ -160,7 +169,7 @@ |
json_string_->append(kPrettyPrintLineEnding); |
const DictionaryValue* dict = |
- static_cast<const DictionaryValue*>(node); |
+ static_cast<const DictionaryValue*>(node); |
bool first_entry = true; |
for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); |
itr.Advance(), first_entry = false) { |
@@ -177,8 +186,7 @@ |
if (pretty_print_) |
IndentLine(depth + 1); |
- |
- EscapeJSONString(itr.key(), true, json_string_); |
+ AppendQuotedString(itr.key()); |
if (pretty_print_) { |
json_string_->append(": "); |
} else { |
@@ -210,6 +218,12 @@ |
} |
} |
+void JSONWriter::AppendQuotedString(const std::string& str) { |
+ // TODO(viettrungluu): |str| is UTF-8, not ASCII, so to properly escape it we |
+ // have to convert it to UTF-16. This round-trip is suboptimal. |
+ JsonDoubleQuote(UTF8ToUTF16(str), true, json_string_); |
+} |
+ |
void JSONWriter::IndentLine(int depth) { |
// It may be faster to keep an indent string so we don't have to keep |
// reallocating. |