summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-29 00:21:49 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-29 00:21:49 +0000
commit94d126b3ce7e1f3100897240884a00f3b7e96575 (patch)
tree51306cea1e41d88f10ef3554f89c2208f5512583
parentad89a6dc97029138b9f7f04135f3e8e164491eb5 (diff)
downloadchromium_src-94d126b3ce7e1f3100897240884a00f3b7e96575.zip
chromium_src-94d126b3ce7e1f3100897240884a00f3b7e96575.tar.gz
chromium_src-94d126b3ce7e1f3100897240884a00f3b7e96575.tar.bz2
Update the JSONValueSerializer to have a flag to allow trailing commas. By default, we don't support this when deserializing JSON, but the user can enable this.
BUG=1295713 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/common/json_value_serializer.cc2
-rw-r--r--chrome/common/json_value_serializer.h12
-rw-r--r--chrome/common/json_value_serializer_unittest.cc8
3 files changed, 15 insertions, 7 deletions
diff --git a/chrome/common/json_value_serializer.cc b/chrome/common/json_value_serializer.cc
index 0049fc6..02b727f 100644
--- a/chrome/common/json_value_serializer.cc
+++ b/chrome/common/json_value_serializer.cc
@@ -49,7 +49,7 @@ bool JSONStringValueSerializer::Deserialize(Value** root) {
if (!json_string_)
return false;
- return JSONReader::Read(*json_string_, root);
+ return JSONReader::Read(*json_string_, root, allow_trailing_comma_);
}
/******* File Serializer *******/
diff --git a/chrome/common/json_value_serializer.h b/chrome/common/json_value_serializer.h
index 6f3c067..ff18104 100644
--- a/chrome/common/json_value_serializer.h
+++ b/chrome/common/json_value_serializer.h
@@ -43,14 +43,16 @@ class JSONStringValueSerializer : public ValueSerializer {
JSONStringValueSerializer(std::string* json_string)
: json_string_(json_string),
initialized_with_const_string_(false),
- pretty_print_(false) {
+ pretty_print_(false),
+ allow_trailing_comma_(false) {
}
// This version allows initialization with a const string reference for
// deserialization only.
JSONStringValueSerializer(const std::string& json_string)
: json_string_(&const_cast<std::string&>(json_string)),
- initialized_with_const_string_(true) {
+ initialized_with_const_string_(true),
+ allow_trailing_comma_(false) {
}
~JSONStringValueSerializer();
@@ -70,10 +72,16 @@ class JSONStringValueSerializer : public ValueSerializer {
void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
bool pretty_print() { return pretty_print_; }
+ bool set_allow_trailing_comma(bool new_value) {
+ allow_trailing_comma_ = new_value;
+ }
+
private:
std::string* json_string_;
bool initialized_with_const_string_;
bool pretty_print_; // If true, serialization will span multiple lines.
+ // If true, deserialization will allow trailing commas.
+ bool allow_trailing_comma_;
DISALLOW_EVIL_CONSTRUCTORS(JSONStringValueSerializer);
};
diff --git a/chrome/common/json_value_serializer_unittest.cc b/chrome/common/json_value_serializer_unittest.cc
index acb2903..a4e9d89 100644
--- a/chrome/common/json_value_serializer_unittest.cc
+++ b/chrome/common/json_value_serializer_unittest.cc
@@ -191,7 +191,7 @@ namespace {
void ValidateJsonList(const std::string& json) {
Value* root = NULL;
- ASSERT_TRUE(JSONReader::Read(json, &root));
+ ASSERT_TRUE(JSONReader::Read(json, &root, false));
ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
ListValue* list = static_cast<ListValue*>(root);
ASSERT_EQ(1, list->GetSize());
@@ -215,7 +215,7 @@ TEST(JSONValueSerializerTest, JSONReaderComments) {
Value* root = NULL;
// It's ok to have a comment in a string.
- ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root));
+ ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false));
ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
ListValue* list = static_cast<ListValue*>(root);
ASSERT_EQ(1, list->GetSize());
@@ -228,10 +228,10 @@ TEST(JSONValueSerializerTest, JSONReaderComments) {
root = NULL;
// You can't nest comments.
- ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root));
+ ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false));
// Not a open comment token.
- ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root));
+ ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false));
}
namespace {