Index: base/json/json_writer.cc |
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc |
index 65317724dba7102670cb3f75a72bc0bea982578a..6dd7aed07cba2559155bfcb848e9bc23b6c5d01e 100644 |
--- a/base/json/json_writer.cc |
+++ b/base/json/json_writer.cc |
@@ -26,19 +26,21 @@ const char* JSONWriter::kEmptyArray = "[]"; |
void JSONWriter::Write(const Value* const node, |
bool pretty_print, |
std::string* json) { |
- WriteWithOptionalEscape(node, pretty_print, true, json); |
+ WriteWithOptions(node, pretty_print, 0, json); |
} |
/* static */ |
-void JSONWriter::WriteWithOptionalEscape(const Value* const node, |
- bool pretty_print, |
- bool escape, |
- std::string* json) { |
+void JSONWriter::WriteWithOptions(const Value* const node, |
+ bool pretty_print, |
+ int options, |
+ std::string* json) { |
json->clear(); |
// Is there a better way to estimate the size of the output? |
json->reserve(1024); |
JSONWriter writer(pretty_print, json); |
- writer.BuildJSONString(node, 0, escape); |
+ bool escape = !(options & OPTIONS_DO_NOT_ESCAPE); |
+ bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES); |
+ writer.BuildJSONString(node, 0, escape, omit_binary_values); |
if (pretty_print) |
json->append(kPrettyPrintLineEnding); |
} |
@@ -51,7 +53,8 @@ JSONWriter::JSONWriter(bool pretty_print, std::string* json) |
void JSONWriter::BuildJSONString(const Value* const node, |
int depth, |
- bool escape) { |
+ bool escape, |
+ bool omit_binary_values) { |
switch (node->GetType()) { |
case Value::TYPE_NULL: |
json_string_->append("null"); |
@@ -122,16 +125,21 @@ void JSONWriter::BuildJSONString(const Value* const node, |
const ListValue* list = static_cast<const ListValue*>(node); |
for (size_t i = 0; i < list->GetSize(); ++i) { |
+ Value* value = NULL; |
+ bool result = list->Get(i, &value); |
+ DCHECK(result); |
+ |
+ if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) { |
+ continue; |
+ } |
+ |
if (i != 0) { |
json_string_->append(","); |
if (pretty_print_) |
json_string_->append(" "); |
} |
- Value* value = NULL; |
- bool result = list->Get(i, &value); |
- DCHECK(result); |
- BuildJSONString(value, depth, escape); |
+ BuildJSONString(value, depth, escape, omit_binary_values); |
} |
if (pretty_print_) |
@@ -151,16 +159,20 @@ void JSONWriter::BuildJSONString(const Value* const node, |
for (DictionaryValue::key_iterator key_itr = dict->begin_keys(); |
key_itr != dict->end_keys(); |
++key_itr) { |
+ Value* value = NULL; |
+ bool result = dict->GetWithoutPathExpansion(*key_itr, &value); |
+ DCHECK(result); |
+ |
+ if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) { |
+ continue; |
+ } |
+ |
if (key_itr != dict->begin_keys()) { |
json_string_->append(","); |
if (pretty_print_) |
json_string_->append(kPrettyPrintLineEnding); |
} |
- Value* value = NULL; |
- bool result = dict->GetWithoutPathExpansion(*key_itr, &value); |
- DCHECK(result); |
- |
if (pretty_print_) |
IndentLine(depth + 1); |
AppendQuotedString(*key_itr); |
@@ -169,7 +181,7 @@ void JSONWriter::BuildJSONString(const Value* const node, |
} else { |
json_string_->append(":"); |
} |
- BuildJSONString(value, depth + 1, escape); |
+ BuildJSONString(value, depth + 1, escape, omit_binary_values); |
} |
if (pretty_print_) { |
@@ -182,8 +194,15 @@ void JSONWriter::BuildJSONString(const Value* const node, |
break; |
} |
+ case Value::TYPE_BINARY: |
+ { |
+ if (!omit_binary_values) { |
+ NOTREACHED() << "Cannot serialize binary value."; |
+ } |
+ break; |
+ } |
+ |
default: |
- // TODO(jhughes): handle TYPE_BINARY |
NOTREACHED() << "unknown json type"; |
} |
} |