summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-20 19:14:43 +0000
committermad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-20 19:14:43 +0000
commitbcf60bb09a7aaf6a311e0928f9d870ab92c32c80 (patch)
tree2a4fa8e62c50f85a6e040bcdb0c50c9c35d30e46
parent905d993a879a87175837fcb01451f6450ecf325f (diff)
downloadchromium_src-bcf60bb09a7aaf6a311e0928f9d870ab92c32c80.zip
chromium_src-bcf60bb09a7aaf6a311e0928f9d870ab92c32c80.tar.gz
chromium_src-bcf60bb09a7aaf6a311e0928f9d870ab92c32c80.tar.bz2
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
-rw-r--r--base/json_writer.cc8
-rw-r--r--base/json_writer_unittest.cc12
2 files changed, 20 insertions, 0 deletions
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;