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

Unified Diff: base/json/json_writer.cc

Issue 9016024: Add option for not adding '.0' to the end of doubles that have no fractional (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 8 years, 12 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
Index: base/json/json_writer.cc
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
index f457331165004ebcc91226d6fbf2e26965e402df..02b9df6888be3811ebf6e75a28ba9d067cf18dec 100644
--- a/base/json/json_writer.cc
+++ b/base/json/json_writer.cc
@@ -4,6 +4,8 @@
#include "base/json/json_writer.h"
+#include <cmath>
+
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/stringprintf.h"
@@ -40,7 +42,10 @@ void JSONWriter::WriteWithOptions(const Value* const node,
JSONWriter writer(pretty_print, json);
bool escape = !(options & OPTIONS_DO_NOT_ESCAPE);
bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES);
- writer.BuildJSONString(node, 0, escape, omit_binary_values);
+ bool allow_doubles_as_integers =
+ !!(options & OPTIONS_ALLOW_DOUBLES_AS_INTEGERS);
+ writer.BuildJSONString(node, 0, escape, omit_binary_values,
+ allow_doubles_as_integers);
if (pretty_print)
json->append(kPrettyPrintLineEnding);
}
@@ -54,7 +59,8 @@ JSONWriter::JSONWriter(bool pretty_print, std::string* json)
void JSONWriter::BuildJSONString(const Value* const node,
int depth,
bool escape,
- bool omit_binary_values) {
+ bool omit_binary_values,
+ bool allow_doubles_as_integers) {
switch (node->GetType()) {
case Value::TYPE_NULL:
json_string_->append("null");
@@ -83,6 +89,13 @@ void JSONWriter::BuildJSONString(const Value* const node,
double value;
bool result = node->GetAsDouble(&value);
DCHECK(result);
+ if (allow_doubles_as_integers &&
+ value <= kint64max &&
+ value >= kint64min &&
+ std::floor(value) == value) {
+ json_string_->append(Int64ToString(static_cast<int64>(value)));
+ break;
tony 2012/03/06 17:57:25 Does it work to just not append the .0 below?
kkania 2012/03/06 21:26:09 No, because DoubleToString sometimes uses exponent
+ }
std::string real = DoubleToString(value);
// Ensure that the number has a .0 if there's no decimal or 'e'. This
// makes sure that when we read the JSON back, it's interpreted as a
@@ -139,7 +152,8 @@ void JSONWriter::BuildJSONString(const Value* const node,
json_string_->append(" ");
}
- BuildJSONString(value, depth, escape, omit_binary_values);
+ BuildJSONString(value, depth, escape, omit_binary_values,
+ allow_doubles_as_integers);
}
if (pretty_print_)
@@ -181,7 +195,8 @@ void JSONWriter::BuildJSONString(const Value* const node,
} else {
json_string_->append(":");
}
- BuildJSONString(value, depth + 1, escape, omit_binary_values);
+ BuildJSONString(value, depth + 1, escape, omit_binary_values,
+ allow_doubles_as_integers);
}
if (pretty_print_) {

Powered by Google App Engine
This is Rietveld 408576698