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

Unified Diff: base/json/json_writer.cc

Issue 9590002: JSONWriter cleanup: integrate pretty print into write options. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix merge conflict 7. Created 8 years, 9 months 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
« no previous file with comments | « base/json/json_writer.h ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_writer.cc
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
index 27300c0b6356f42392537f88d69a45d929b4c887..e81400339ec38e98c373d06ef5d121cc2b754269 100644
--- a/base/json/json_writer.cc
+++ b/base/json/json_writer.cc
@@ -25,42 +25,43 @@ static const char kPrettyPrintLineEnding[] = "\n";
const char* JSONWriter::kEmptyArray = "[]";
/* static */
-void JSONWriter::Write(const Value* const node,
- bool pretty_print,
- std::string* json) {
- WriteWithOptions(node, pretty_print, 0, json);
+void JSONWriter::Write(const Value* const node, std::string* json) {
+ WriteWithOptions(node, 0, json);
}
/* static */
-void JSONWriter::WriteWithOptions(const Value* const node,
- bool pretty_print,
- int options,
+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);
- JSONWriter writer(pretty_print, json);
+
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);
- writer.BuildJSONString(node, 0, escape, omit_binary_values,
- omit_double_type_preservation);
+ bool pretty_print = !!(options & OPTIONS_PRETTY_PRINT);
+
+ JSONWriter writer(escape, omit_binary_values, omit_double_type_preservation,
+ pretty_print, json);
+ writer.BuildJSONString(node, 0);
+
if (pretty_print)
json->append(kPrettyPrintLineEnding);
}
-JSONWriter::JSONWriter(bool pretty_print, std::string* json)
- : json_string_(json),
- pretty_print_(pretty_print) {
+JSONWriter::JSONWriter(bool escape, bool omit_binary_values,
+ bool omit_double_type_preservation, bool pretty_print,
+ std::string* json)
+ : escape_(escape),
+ omit_binary_values_(omit_binary_values),
+ omit_double_type_preservation_(omit_double_type_preservation),
+ pretty_print_(pretty_print),
+ json_string_(json) {
DCHECK(json);
}
-void JSONWriter::BuildJSONString(const Value* const node,
- int depth,
- bool escape,
- bool omit_binary_values,
- bool omit_double_type_preservation) {
+void JSONWriter::BuildJSONString(const Value* const node, int depth) {
switch (node->GetType()) {
case Value::TYPE_NULL:
json_string_->append("null");
@@ -89,7 +90,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
double value;
bool result = node->GetAsDouble(&value);
DCHECK(result);
- if (omit_double_type_preservation &&
+ if (omit_double_type_preservation_ &&
value <= kint64max &&
value >= kint64min &&
std::floor(value) == value) {
@@ -122,7 +123,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
std::string value;
bool result = node->GetAsString(&value);
DCHECK(result);
- if (escape) {
+ if (escape_) {
JsonDoubleQuote(UTF8ToUTF16(value), true, json_string_);
} else {
JsonDoubleQuote(value, true, json_string_);
@@ -142,7 +143,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
bool result = list->Get(i, &value);
DCHECK(result);
- if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) {
+ if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) {
continue;
}
@@ -152,8 +153,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
json_string_->append(" ");
}
- BuildJSONString(value, depth, escape, omit_binary_values,
- omit_double_type_preservation);
+ BuildJSONString(value, depth);
}
if (pretty_print_)
@@ -177,7 +177,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
bool result = dict->GetWithoutPathExpansion(*key_itr, &value);
DCHECK(result);
- if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) {
+ if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) {
continue;
}
@@ -195,8 +195,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
} else {
json_string_->append(":");
}
- BuildJSONString(value, depth + 1, escape, omit_binary_values,
- omit_double_type_preservation);
+ BuildJSONString(value, depth + 1);
}
if (pretty_print_) {
@@ -211,7 +210,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
case Value::TYPE_BINARY:
{
- if (!omit_binary_values) {
+ if (!omit_binary_values_) {
NOTREACHED() << "Cannot serialize binary value.";
}
break;
« no previous file with comments | « base/json/json_writer.h ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698