From bcf60bb09a7aaf6a311e0928f9d870ab92c32c80 Mon Sep 17 00:00:00 2001 From: "mad@chromium.org" Date: Mon, 20 Apr 2009 19:14:43 +0000 Subject: Landing this patch for Jamesr. CR was done at: http://codereview.chromium.org/67257 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14048 0039d316-1c4b-4281-b951-d872f2087c98 --- base/json_writer.cc | 8 ++++++++ base/json_writer_unittest.cc | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/base/json_writer.cc b/base/json_writer.cc index 20c533b..c68bfe6 100644 --- a/base/json_writer.cc +++ b/base/json_writer.cc @@ -67,6 +67,14 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) { 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); break; } diff --git a/base/json_writer_unittest.cc b/base/json_writer_unittest.cc index 4b6f312..9f24fe1 100644 --- a/base/json_writer_unittest.cc +++ b/base/json_writer_unittest.cc @@ -32,6 +32,18 @@ TEST(JSONWriterTest, Writing) { ASSERT_EQ("1.0", output_js); delete root; + // Test Real values in the the range (-1, 1) must have leading zeros + root = Value::CreateRealValue(0.2); + JSONWriter::Write(root, false, &output_js); + ASSERT_EQ("0.2", output_js); + delete root; + + // Test Real values in the the range (-1, 1) must have leading zeros + root = Value::CreateRealValue(-0.8); + JSONWriter::Write(root, false, &output_js); + ASSERT_EQ("-0.8", output_js); + delete root; + // Writer unittests like empty list/dict nesting, // list list nesting, etc. DictionaryValue root_dict; -- cgit v1.1