| Index: base/json/json_writer.cc
|
| diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
|
| index 6a9cc6aa470b592e6a76919236fe71706fd93036..3521698917dc2b56924f872484a1bee6f7a07244 100644
|
| --- a/base/json/json_writer.cc
|
| +++ b/base/json/json_writer.cc
|
| @@ -50,6 +50,34 @@ void JSONWriter::WriteWithOptions(const Value* const node, int options,
|
| json->append(kPrettyPrintLineEnding);
|
| }
|
|
|
| +std::string JSONWriter::DoubleToFormattedString(double value,
|
| + bool omit_double_type_preservation) {
|
| + if (omit_double_type_preservation &&
|
| + value <= kint64max &&
|
| + value >= kint64min &&
|
| + std::floor(value) == value) {
|
| + return Int64ToString(static_cast<int64>(value));
|
| + }
|
| + 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
|
| + // real rather than an int.
|
| + if (real.find('.') == std::string::npos &&
|
| + real.find('e') == std::string::npos &&
|
| + real.find('E') == std::string::npos) {
|
| + real.append(".0");
|
| + }
|
| + // The JSON spec requires that non-integer values in the range (-1,1)
|
| + // have a zero before the decimal point - ".52" is not valid, "0.52" is.
|
| + if (real[0] == '.') {
|
| + real.insert(0, "0");
|
| + } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
|
| + // "-.1" bad "-0.1" good
|
| + real.insert(1, "0");
|
| + }
|
| + return real;
|
| +}
|
| +
|
| JSONWriter::JSONWriter(bool escape, bool omit_binary_values,
|
| bool omit_double_type_preservation, bool pretty_print,
|
| std::string* json)
|
| @@ -90,31 +118,8 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) {
|
| double value;
|
| bool result = node->GetAsDouble(&value);
|
| DCHECK(result);
|
| - if (omit_double_type_preservation_ &&
|
| - value <= kint64max &&
|
| - value >= kint64min &&
|
| - std::floor(value) == value) {
|
| - json_string_->append(Int64ToString(static_cast<int64>(value)));
|
| - break;
|
| - }
|
| - 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
|
| - // real rather than an int.
|
| - if (real.find('.') == std::string::npos &&
|
| - real.find('e') == std::string::npos &&
|
| - real.find('E') == std::string::npos) {
|
| - real.append(".0");
|
| - }
|
| - // The JSON spec requires that non-integer values in the range (-1,1)
|
| - // have a zero before the decimal point - ".52" is not valid, "0.52" is.
|
| - if (real[0] == '.') {
|
| - real.insert(0, "0");
|
| - } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
|
| - // "-.1" bad "-0.1" good
|
| - real.insert(1, "0");
|
| - }
|
| - json_string_->append(real);
|
| + json_string_->append(DoubleToFormattedString(value,
|
| + omit_double_type_preservation_));
|
| break;
|
| }
|
|
|
|
|