summaryrefslogtreecommitdiffstats
path: root/base/json
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-25 20:47:52 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-25 20:47:52 +0000
commit4dad9ad838f6671fbd67e1c5292525e739e31983 (patch)
tree4d79fc17f12752cc221e0e40d16951677da71f92 /base/json
parent2b3f0f59a6761a41e22007c2c3096e8e18517e08 (diff)
downloadchromium_src-4dad9ad838f6671fbd67e1c5292525e739e31983.zip
chromium_src-4dad9ad838f6671fbd67e1c5292525e739e31983.tar.gz
chromium_src-4dad9ad838f6671fbd67e1c5292525e739e31983.tar.bz2
Many changes to DictionaryValues:
* Add support for keys with "." in them via new XXXWithoutPathExpansion() APIs. * Use these APIs with all key iterator usage. * SetXXX() calls cannot fail, so change them from bool to void. * Change GetSize() to size() since it's cheap, and add empty(). Other: * Use standard for loop format in more places (e.g. instead of while loops when they're really doing a for loop). * Shorten a few bits of code. BUG=567 TEST=none Review URL: http://codereview.chromium.org/441008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33109 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json')
-rw-r--r--base/json/json_reader.cc3
-rw-r--r--base/json/json_reader_unittest.cc59
-rw-r--r--base/json/json_writer.cc2
-rw-r--r--base/json/json_writer_unittest.cc19
4 files changed, 64 insertions, 19 deletions
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc
index 06d790c..bdc682b 100644
--- a/base/json/json_reader.cc
+++ b/base/json/json_reader.cc
@@ -281,7 +281,8 @@ Value* JSONReader::BuildValue(bool is_root) {
Value* dict_value = BuildValue(false);
if (!dict_value)
return NULL;
- static_cast<DictionaryValue*>(node.get())->Set(dict_key, dict_value);
+ static_cast<DictionaryValue*>(node.get())->SetWithoutPathExpansion(
+ dict_key, dict_value);
// After a key/value pair, we expect a comma or the end of the
// object.
diff --git a/base/json/json_reader_unittest.cc b/base/json/json_reader_unittest.cc
index 8ae51c5..17dea56 100644
--- a/base/json/json_reader_unittest.cc
+++ b/base/json/json_reader_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -297,8 +297,7 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
root.reset(JSONReader::Read(
- "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }",
- false));
+ "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false));
ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
@@ -313,32 +312,32 @@ TEST(JSONReaderTest, Reading) {
ASSERT_EQ(L"str", str_val);
root2.reset(JSONReader::Read(
- "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
+ "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
ASSERT_TRUE(root2.get());
EXPECT_TRUE(root->Equals(root2.get()));
// Test newline equivalence.
root2.reset(JSONReader::Read(
- "{\n"
- " \"number\":9.87654321,\n"
- " \"null\":null,\n"
- " \"\\x53\":\"str\",\n"
- "}\n", true));
+ "{\n"
+ " \"number\":9.87654321,\n"
+ " \"null\":null,\n"
+ " \"\\x53\":\"str\",\n"
+ "}\n", true));
ASSERT_TRUE(root2.get());
EXPECT_TRUE(root->Equals(root2.get()));
root2.reset(JSONReader::Read(
- "{\r\n"
- " \"number\":9.87654321,\r\n"
- " \"null\":null,\r\n"
- " \"\\x53\":\"str\",\r\n"
- "}\r\n", true));
+ "{\r\n"
+ " \"number\":9.87654321,\r\n"
+ " \"null\":null,\r\n"
+ " \"\\x53\":\"str\",\r\n"
+ "}\r\n", true));
ASSERT_TRUE(root2.get());
EXPECT_TRUE(root->Equals(root2.get()));
// Test nesting
root.reset(JSONReader::Read(
- "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
+ "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root.get());
@@ -354,9 +353,37 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
root2.reset(JSONReader::Read(
- "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
+ "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
EXPECT_TRUE(root->Equals(root2.get()));
+ // Test keys with periods
+ root.reset(JSONReader::Read(
+ "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false));
+ ASSERT_TRUE(root.get());
+ ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
+ dict_val = static_cast<DictionaryValue*>(root.get());
+ int integer_value = 0;
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
+ EXPECT_EQ(3, integer_value);
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"c", &integer_value));
+ EXPECT_EQ(2, integer_value);
+ inner_dict = NULL;
+ ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion(L"d.e.f",
+ &inner_dict));
+ ASSERT_EQ(1U, inner_dict->size());
+ EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion(L"g.h.i.j",
+ &integer_value));
+ EXPECT_EQ(1, integer_value);
+
+ root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false));
+ ASSERT_TRUE(root.get());
+ ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
+ dict_val = static_cast<DictionaryValue*>(root.get());
+ EXPECT_TRUE(dict_val->GetInteger(L"a.b", &integer_value));
+ EXPECT_EQ(2, integer_value);
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
+ EXPECT_EQ(1, integer_value);
+
// Invalid, no closing brace
root.reset(JSONReader::Read("{\"a\": true", false));
ASSERT_FALSE(root.get());
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
index 133b625..ffdad76 100644
--- a/base/json/json_writer.cc
+++ b/base/json/json_writer.cc
@@ -157,7 +157,7 @@ void JSONWriter::BuildJSONString(const Value* const node,
}
Value* value = NULL;
- bool result = dict->Get(*key_itr, &value);
+ bool result = dict->GetWithoutPathExpansion(*key_itr, &value);
DCHECK(result);
if (pretty_print_)
diff --git a/base/json/json_writer_unittest.cc b/base/json/json_writer_unittest.cc
index cd2a6fe..e7d0f05 100644
--- a/base/json/json_writer_unittest.cc
+++ b/base/json/json_writer_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -76,6 +76,23 @@ TEST(JSONWriterTest, Writing) {
"}" JSON_NEWLINE,
output_js);
#undef JSON_NEWLINE
+
+ // Test keys with periods
+ DictionaryValue period_dict;
+ period_dict.SetWithoutPathExpansion(L"a.b", Value::CreateIntegerValue(3));
+ period_dict.SetWithoutPathExpansion(L"c", Value::CreateIntegerValue(2));
+ DictionaryValue* period_dict2 = new DictionaryValue;
+ period_dict2->SetWithoutPathExpansion(L"g.h.i.j",
+ Value::CreateIntegerValue(1));
+ period_dict.SetWithoutPathExpansion(L"d.e.f", period_dict2);
+ JSONWriter::Write(&period_dict, false, &output_js);
+ ASSERT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js);
+
+ DictionaryValue period_dict3;
+ period_dict3.Set(L"a.b", Value::CreateIntegerValue(2));
+ period_dict3.SetWithoutPathExpansion(L"a.b", Value::CreateIntegerValue(1));
+ JSONWriter::Write(&period_dict3, false, &output_js);
+ ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js);
}
} // namespace base