Index: base/debug/trace_event_impl.cc |
diff --git a/base/debug/trace_event_impl.cc b/base/debug/trace_event_impl.cc |
index 9ee9fc802be105ea979576ec239c6275c467a7fc..6fff9041515d53e7f172dd22f2e2e8b0d9541445 100644 |
--- a/base/debug/trace_event_impl.cc |
+++ b/base/debug/trace_event_impl.cc |
@@ -17,6 +17,7 @@ |
#include "base/message_loop/message_loop.h" |
#include "base/process/process_metrics.h" |
#include "base/stl_util.h" |
+#include "base/strings/string_number_conversions.h" |
#include "base/strings/string_split.h" |
#include "base/strings/string_tokenizer.h" |
#include "base/strings/string_util.h" |
@@ -509,9 +510,30 @@ void TraceEvent::AppendValueAsJSON(unsigned char type, |
case TRACE_VALUE_TYPE_INT: |
StringAppendF(out, "%" PRId64, static_cast<int64>(value.as_int)); |
break; |
- case TRACE_VALUE_TYPE_DOUBLE: |
- StringAppendF(out, "%f", value.as_double); |
+ case TRACE_VALUE_TYPE_DOUBLE: { |
+ // FIXME: base/json/json_writer.cc is using the same code, |
+ // should be made into a common method. |
+ std::string real = DoubleToString(value.as_double); |
+ // 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"); |
+ } |
+ |
+ StringAppendF(out, "%s", real.c_str()); |
break; |
+ } |
case TRACE_VALUE_TYPE_POINTER: |
// JSON only supports double and int numbers. |
// So as not to lose bits from a 64-bit pointer, output as a hex string. |