0

Add a flag to JSONReader to allow trailing commas.

Lots of unittests for this behavior.

BUG=1295713

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
tc@google.com
2008-07-29 00:01:31 +00:00
parent 1b66f31cbe
commit e724599d3f
3 changed files with 185 additions and 85 deletions

@@ -98,13 +98,17 @@ bool ReadHexDigits(JSONReader::Token& token, int digits) {
} // anonymous namespace } // anonymous namespace
/* static */ /* static */
bool JSONReader::Read(const std::string& json, Value** root) { bool JSONReader::Read(const std::string& json,
return JsonToValue(json, root, true); Value** root,
bool allow_trailing_comma) {
return JsonToValue(json, root, true, allow_trailing_comma);
} }
/* static */ /* static */
bool JSONReader::JsonToValue(const std::string& json, Value** root, bool JSONReader::JsonToValue(const std::string& json,
bool check_root) { Value** root,
bool check_root,
bool allow_trailing_comma) {
// Assume input is UTF8. The conversion from UTF8 to wstring removes null // Assume input is UTF8. The conversion from UTF8 to wstring removes null
// bytes for us (a good thing). // bytes for us (a good thing).
std::wstring json_wide(UTF8ToWide(json)); std::wstring json_wide(UTF8ToWide(json));
@@ -119,7 +123,7 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root,
++json_cstr; ++json_cstr;
} }
JSONReader reader(json_cstr); JSONReader reader(json_cstr, allow_trailing_comma);
Value* temp_root = NULL; Value* temp_root = NULL;
bool success = reader.BuildValue(&temp_root, check_root); bool success = reader.BuildValue(&temp_root, check_root);
@@ -135,8 +139,11 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root,
return false; return false;
} }
JSONReader::JSONReader(const wchar_t* json_start_pos) JSONReader::JSONReader(const wchar_t* json_start_pos,
: json_pos_(json_start_pos), stack_depth_(0) {} bool allow_trailing_comma)
: json_pos_(json_start_pos),
stack_depth_(0),
allow_trailing_comma_(allow_trailing_comma) {}
bool JSONReader::BuildValue(Value** node, bool is_root) { bool JSONReader::BuildValue(Value** node, bool is_root) {
++stack_depth_; ++stack_depth_;
@@ -196,10 +203,15 @@ bool JSONReader::BuildValue(Value** node, bool is_root) {
if (token.type == Token::LIST_SEPARATOR) { if (token.type == Token::LIST_SEPARATOR) {
json_pos_ += token.length; json_pos_ += token.length;
token = ParseToken(); token = ParseToken();
// Trailing commas are invalid // Trailing commas are invalid according to the JSON RFC, but some
// consumers need the parsing leniency, so handle accordingly.
if (token.type == Token::ARRAY_END) { if (token.type == Token::ARRAY_END) {
delete array; if (!allow_trailing_comma_) {
return false; delete array;
return false;
}
// Trailing comma OK, stop parsing the Array.
break;
} }
} else if (token.type != Token::ARRAY_END) { } else if (token.type != Token::ARRAY_END) {
// Unexpected value after list value. Bail out. // Unexpected value after list value. Bail out.
@@ -259,11 +271,15 @@ bool JSONReader::BuildValue(Value** node, bool is_root) {
if (token.type == Token::LIST_SEPARATOR) { if (token.type == Token::LIST_SEPARATOR) {
json_pos_ += token.length; json_pos_ += token.length;
token = ParseToken(); token = ParseToken();
// Trailing commas are invalid. TODO(tc): Should we allow trailing // Trailing commas are invalid according to the JSON RFC, but some
// commas in objects? Seems harmless and quite convenient... // consumers need the parsing leniency, so handle accordingly.
if (token.type == Token::OBJECT_END) { if (token.type == Token::OBJECT_END) {
delete dict; if (!allow_trailing_comma_) {
return false; delete dict;
return false;
}
// Trailing comma OK, stop parsing the Object.
break;
} }
} else if (token.type != Token::OBJECT_END) { } else if (token.type != Token::OBJECT_END) {
// Unexpected value after last object value. Bail out. // Unexpected value after last object value. Bail out.

@@ -46,11 +46,11 @@
// character, the function skips a Unicode BOM at the beginning of the // character, the function skips a Unicode BOM at the beginning of the
// Unicode string (converted from the input UTF-8 string) before parsing it. // Unicode string (converted from the input UTF-8 string) before parsing it.
// //
// TODO(tc): It would be nice to give back an error string when we fail to parse JSON. // TODO(tc): It would be nice to give back an error string when we fail to
// Parsing options: // parse JSON.
// - Relax trailing commas in arrays and objects // TODO(tc): Add a parsing option to to relax object keys being wrapped in
// - Relax object keys being wrapped in double quotes // double quotes
// - Disable comment stripping // TODO(tc): Add an option to disable comment stripping
#ifndef CHROME_COMMON_JSON_READER_H__ #ifndef CHROME_COMMON_JSON_READER_H__
#define CHROME_COMMON_JSON_READER_H__ #define CHROME_COMMON_JSON_READER_H__
@@ -99,12 +99,16 @@ class JSONReader {
} }
}; };
// Reads and parses |json| and populates |root|. If |json| is not a // Reads and parses |json| and populates |root|. If |json| is not a properly
// properly formed JSON string, returns false and leaves root unaltered. // formed JSON string, returns false and leaves root unaltered. If
static bool Read(const std::string& json, Value** root); // allow_trailing_comma is true, we will ignore trailing commas in objects
// and arrays even though this goes against the RFC.
static bool Read(const std::string& json,
Value** root,
bool allow_trailing_comma);
private: private:
JSONReader(const wchar_t* json_start_pos); JSONReader(const wchar_t* json_start_pos, bool allow_trailing_comma);
DISALLOW_EVIL_CONSTRUCTORS(JSONReader); DISALLOW_EVIL_CONSTRUCTORS(JSONReader);
FRIEND_TEST(JSONReaderTest, Reading); FRIEND_TEST(JSONReaderTest, Reading);
@@ -112,7 +116,8 @@ class JSONReader {
// Pass through method from JSONReader::Read. We have this so unittests can // Pass through method from JSONReader::Read. We have this so unittests can
// disable the root check. // disable the root check.
static bool JsonToValue(const std::string& json, Value** root, static bool JsonToValue(const std::string& json, Value** root,
bool check_root); bool check_root,
bool allow_trailing_comma);
// Recursively build Value. Returns false if we don't have a valid JSON // Recursively build Value. Returns false if we don't have a valid JSON
// string. If |is_root| is true, we verify that the root element is either // string. If |is_root| is true, we verify that the root element is either
@@ -160,6 +165,9 @@ class JSONReader {
// Used to keep track of how many nested lists/dicts there are. // Used to keep track of how many nested lists/dicts there are.
int stack_depth_; int stack_depth_;
// A parser flag that allows trailing commas in objects and arrays.
bool allow_trailing_comma_;
}; };
#endif // CHROME_COMMON_JSON_READER_H__ #endif // CHROME_COMMON_JSON_READER_H__

@@ -34,26 +34,26 @@
TEST(JSONReaderTest, Reading) { TEST(JSONReaderTest, Reading) {
// some whitespace checking // some whitespace checking
Value* root = NULL; Value* root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
delete root; delete root;
// Invalid JSON string // Invalid JSON string
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Simple bool // Simple bool
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
delete root; delete root;
// Test number formats // Test number formats
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int int_val = 0; int int_val = 0;
@@ -63,19 +63,19 @@ TEST(JSONReaderTest, Reading) {
// According to RFC4627, oct, hex, and leading zeros are invalid JSON. // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Test 0 (which needs to be special cased because of the leading zero // Test 0 (which needs to be special cased because of the leading zero
// clause). // clause).
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int_val = 1; int_val = 1;
@@ -85,15 +85,15 @@ TEST(JSONReaderTest, Reading) {
// Numbers that overflow ints should fail // Numbers that overflow ints should fail
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("2147483648", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("2147483648", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("-2147483649", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("-2147483649", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Parse a double // Parse a double
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("43.1", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("43.1", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
double real_val = 0.0; double real_val = 0.0;
@@ -102,7 +102,7 @@ TEST(JSONReaderTest, Reading) {
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("4.3e-1", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("4.3e-1", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0; real_val = 0.0;
@@ -111,7 +111,7 @@ TEST(JSONReaderTest, Reading) {
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("2.1e0", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("2.1e0", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0; real_val = 0.0;
@@ -120,7 +120,7 @@ TEST(JSONReaderTest, Reading) {
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("2.1e+0001", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("2.1e+0001", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0; real_val = 0.0;
@@ -129,7 +129,7 @@ TEST(JSONReaderTest, Reading) {
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("0.01", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("0.01", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0; real_val = 0.0;
@@ -138,7 +138,7 @@ TEST(JSONReaderTest, Reading) {
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("1.00", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("1.00", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0; real_val = 0.0;
@@ -148,51 +148,51 @@ TEST(JSONReaderTest, Reading) {
// Fractional parts must have a digit before and after the decimal point. // Fractional parts must have a digit before and after the decimal point.
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Exponent must have a digit following the 'e'. // Exponent must have a digit following the 'e'.
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// INF/-INF/NaN are not valid // INF/-INF/NaN are not valid
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid number formats // Invalid number formats
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Test string parser // Test string parser
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"hello world\"", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("\"hello world\"", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
std::wstring str_val; std::wstring str_val;
@@ -202,7 +202,7 @@ TEST(JSONReaderTest, Reading) {
// Empty string // Empty string
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear(); str_val.clear();
@@ -213,7 +213,7 @@ TEST(JSONReaderTest, Reading) {
// Test basic string escapes // Test basic string escapes
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\"", &root, ASSERT_TRUE(JSONReader::JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\"", &root,
false)); false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear(); str_val.clear();
@@ -223,7 +223,8 @@ TEST(JSONReaderTest, Reading) {
// Test hex and unicode escapes including the null character. // Test hex and unicode escapes including the null character.
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\\x41\\x00\\u1234\"", &root, false)); ASSERT_TRUE(JSONReader::JsonToValue("\"\\x41\\x00\\u1234\"", &root, false,
false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear(); str_val.clear();
@@ -233,41 +234,48 @@ TEST(JSONReaderTest, Reading) {
// Test invalid strings // Test invalid strings
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false)); ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false,
false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\z invalid escape char\"", &root, ASSERT_FALSE(JSONReader::JsonToValue("\"\\z invalid escape char\"", &root,
false)); false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\xAQ invalid hex code\"", &root, ASSERT_FALSE(JSONReader::JsonToValue("\"\\xAQ invalid hex code\"", &root,
false)); false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("not enough hex chars\\x1\"", &root, ASSERT_FALSE(JSONReader::JsonToValue("not enough hex chars\\x1\"", &root,
false)); false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"not enough escape chars\\u123\"", ASSERT_FALSE(JSONReader::JsonToValue("\"not enough escape chars\\u123\"",
&root, false)); &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"extra backslash at end of input\\\"", ASSERT_FALSE(JSONReader::JsonToValue("\"extra backslash at end of input\\\"",
&root, false)); &root, false, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Basic array // Basic array
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read("[true, false, null]", &root)); ASSERT_TRUE(JSONReader::Read("[true, false, null]", &root, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
ListValue* list = static_cast<ListValue*>(root); ListValue* list = static_cast<ListValue*>(root);
ASSERT_EQ(3, list->GetSize()); ASSERT_EQ(3, list->GetSize());
// Test with trailing comma. Should be parsed the same as above.
Value* root2 = NULL;
ASSERT_TRUE(JSONReader::Read("[true, false, null, ]", &root2, true));
EXPECT_TRUE(root->Equals(root2));
delete root; delete root;
delete root2;
// Empty array // Empty array
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read("[]", &root)); ASSERT_TRUE(JSONReader::Read("[]", &root, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root); list = static_cast<ListValue*>(root);
@@ -276,43 +284,81 @@ TEST(JSONReaderTest, Reading) {
// Nested arrays // Nested arrays
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null]], null]", &root)); ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null]], null]", &root,
false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root); list = static_cast<ListValue*>(root);
ASSERT_EQ(4, list->GetSize()); ASSERT_EQ(4, list->GetSize());
// Lots of trailing commas.
root2 = NULL;
ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
&root2, true));
EXPECT_TRUE(root->Equals(root2));
delete root; delete root;
delete root2;
// Invalid, missing close brace. // Invalid, missing close brace.
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root)); ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root,
false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, too many commas // Invalid, too many commas
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("[true,, null]", &root)); ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, false));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, true));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, no commas // Invalid, no commas
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("[true null]", &root)); ASSERT_FALSE(JSONReader::Read("[true null]", &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, trailing comma // Invalid, trailing comma
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("[true,]", &root)); ASSERT_FALSE(JSONReader::Read("[true,]", &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Valid if we set |allow_trailing_comma| to true.
EXPECT_TRUE(JSONReader::Read("[true,]", &root, true));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
EXPECT_EQ(1, list->GetSize());
Value* tmp_value = NULL;
ASSERT_TRUE(list->Get(0, &tmp_value));
EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
bool bool_value = false;
ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value));
EXPECT_TRUE(bool_value);
delete root;
// Don't allow empty elements, even if |allow_trailing_comma| is
// true.
root = NULL;
EXPECT_FALSE(JSONReader::Read("[,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[true,,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[,true,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[true,,false]", &root, true));
EXPECT_FALSE(root);
// Test objects // Test objects
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read("{}", &root)); ASSERT_TRUE(JSONReader::Read("{}", &root, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
delete root; delete root;
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read( ASSERT_TRUE(JSONReader::Read(
"{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", &root)); "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", &root,
false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* dict_val = static_cast<DictionaryValue*>(root); DictionaryValue* dict_val = static_cast<DictionaryValue*>(root);
@@ -325,12 +371,19 @@ TEST(JSONReaderTest, Reading) {
str_val.clear(); str_val.clear();
ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
ASSERT_EQ(L"str", str_val); ASSERT_EQ(L"str", str_val);
root2 = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", &root2,
true));
EXPECT_TRUE(root->Equals(root2));
delete root; delete root;
delete root2;
// Test nesting // Test nesting
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::Read( ASSERT_TRUE(JSONReader::Read(
"{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", &root)); "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", &root, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root); dict_val = static_cast<DictionaryValue*>(root);
@@ -339,43 +392,66 @@ TEST(JSONReaderTest, Reading) {
ListValue* inner_array = NULL; ListValue* inner_array = NULL;
ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array)); ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
ASSERT_EQ(1, inner_array->GetSize()); ASSERT_EQ(1, inner_array->GetSize());
bool bool_value = true; bool_value = true;
ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value)); ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
ASSERT_FALSE(bool_value); ASSERT_FALSE(bool_value);
inner_dict = NULL; inner_dict = NULL;
ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict)); ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
root2 = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", &root2,
true));
EXPECT_TRUE(root->Equals(root2));
delete root; delete root;
delete root2;
// Invalid, no closing brace // Invalid, no closing brace
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root)); ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, keys must be quoted // Invalid, keys must be quoted
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("{foo:true}", &root)); ASSERT_FALSE(JSONReader::Read("{foo:true}", &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, trailing comma // Invalid, trailing comma
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root)); ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, too many commas // Invalid, too many commas
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root)); ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Invalid, no separator // Invalid, no separator
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\" \"b\"}", &root)); ASSERT_FALSE(JSONReader::Read("{\"a\" \"b\"}", &root, false));
ASSERT_FALSE(root);
// Invalid, lone comma.
root = NULL;
ASSERT_FALSE(JSONReader::Read("{,}", &root, false));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{,}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{,\"a\":true}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// Test stack overflow // Test stack overflow
root = NULL; root = NULL;
std::string evil(1000000, '['); std::string evil(1000000, '[');
evil.append(std::string(1000000, ']')); evil.append(std::string(1000000, ']'));
ASSERT_FALSE(JSONReader::Read(evil, &root)); ASSERT_FALSE(JSONReader::Read(evil, &root, false));
ASSERT_FALSE(root); ASSERT_FALSE(root);
// A few thousand adjacent lists is fine. // A few thousand adjacent lists is fine.
@@ -385,7 +461,7 @@ TEST(JSONReaderTest, Reading) {
not_evil.append("[],"); not_evil.append("[],");
} }
not_evil.append("[]]"); not_evil.append("[]]");
ASSERT_TRUE(JSONReader::Read(not_evil, &root)); ASSERT_TRUE(JSONReader::Read(not_evil, &root, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root); list = static_cast<ListValue*>(root);
@@ -395,7 +471,7 @@ TEST(JSONReaderTest, Reading) {
// Test utf8 encoded input // Test utf8 encoded input
root = NULL; root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root, ASSERT_TRUE(JSONReader::JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root,
false)); false, false));
ASSERT_TRUE(root); ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear(); str_val.clear();
@@ -405,8 +481,8 @@ TEST(JSONReaderTest, Reading) {
// Test invalid root objects. // Test invalid root objects.
root = NULL; root = NULL;
ASSERT_FALSE(JSONReader::Read("null", &root)); ASSERT_FALSE(JSONReader::Read("null", &root, false));
ASSERT_FALSE(JSONReader::Read("true", &root)); ASSERT_FALSE(JSONReader::Read("true", &root, false));
ASSERT_FALSE(JSONReader::Read("10", &root)); ASSERT_FALSE(JSONReader::Read("10", &root, false));
ASSERT_FALSE(JSONReader::Read("\"root\"", &root)); ASSERT_FALSE(JSONReader::Read("\"root\"", &root, false));
} }