Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Unified Diff: base/json/json_writer.cc

Issue 8505033: Allow JSONWriter and JSONValueSerializer to omit binary values when instructed to do so. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address lint warning. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/json/json_writer.cc
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
index 65317724dba7102670cb3f75a72bc0bea982578a..003f9c855fccfd98368bda20ebdfdc6e5c49368f 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);
+ Write(node, pretty_print, 0, json);
}
/* static */
-void JSONWriter::WriteWithOptionalEscape(const Value* const node,
- bool pretty_print,
- bool escape,
- std::string* json) {
+void JSONWriter::Write(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 ignore_binary_values = !!(options & OPTIONS_IGNORE_BINARY_VALUES);
+ writer.BuildJSONString(node, 0, escape, ignore_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 ignore_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 (ignore_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, ignore_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 (ignore_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, ignore_binary_values);
}
if (pretty_print_) {
@@ -182,8 +194,15 @@ void JSONWriter::BuildJSONString(const Value* const node,
break;
}
+ case Value::TYPE_BINARY:
+ {
+ if (!ignore_binary_values) {
+ NOTREACHED() << "Cannot serialize binary value.";
+ }
+ break;
+ }
+
default:
- // TODO(jhughes): handle TYPE_BINARY
NOTREACHED() << "unknown json type";
}
}

Powered by Google App Engine
This is Rietveld 408576698