summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/json_reader.cc44
-rw-r--r--base/json_reader.h28
-rw-r--r--base/json_reader_unittest.cc198
3 files changed, 185 insertions, 85 deletions
diff --git a/base/json_reader.cc b/base/json_reader.cc
index 1ec5f637..257f635 100644
--- a/base/json_reader.cc
+++ b/base/json_reader.cc
@@ -98,13 +98,17 @@ bool ReadHexDigits(JSONReader::Token& token, int digits) {
} // anonymous namespace
/* static */
-bool JSONReader::Read(const std::string& json, Value** root) {
- return JsonToValue(json, root, true);
+bool JSONReader::Read(const std::string& json,
+ Value** root,
+ bool allow_trailing_comma) {
+ return JsonToValue(json, root, true, allow_trailing_comma);
}
/* static */
-bool JSONReader::JsonToValue(const std::string& json, Value** root,
- bool check_root) {
+bool JSONReader::JsonToValue(const std::string& json,
+ Value** root,
+ bool check_root,
+ bool allow_trailing_comma) {
// Assume input is UTF8. The conversion from UTF8 to wstring removes null
// bytes for us (a good thing).
std::wstring json_wide(UTF8ToWide(json));
@@ -119,7 +123,7 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root,
++json_cstr;
}
- JSONReader reader(json_cstr);
+ JSONReader reader(json_cstr, allow_trailing_comma);
Value* temp_root = NULL;
bool success = reader.BuildValue(&temp_root, check_root);
@@ -135,8 +139,11 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root,
return false;
}
-JSONReader::JSONReader(const wchar_t* json_start_pos)
- : json_pos_(json_start_pos), stack_depth_(0) {}
+JSONReader::JSONReader(const wchar_t* json_start_pos,
+ 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) {
++stack_depth_;
@@ -196,10 +203,15 @@ bool JSONReader::BuildValue(Value** node, bool is_root) {
if (token.type == Token::LIST_SEPARATOR) {
json_pos_ += token.length;
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) {
- delete array;
- return false;
+ if (!allow_trailing_comma_) {
+ delete array;
+ return false;
+ }
+ // Trailing comma OK, stop parsing the Array.
+ break;
}
} else if (token.type != Token::ARRAY_END) {
// 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) {
json_pos_ += token.length;
token = ParseToken();
- // Trailing commas are invalid. TODO(tc): Should we allow trailing
- // commas in objects? Seems harmless and quite convenient...
+ // Trailing commas are invalid according to the JSON RFC, but some
+ // consumers need the parsing leniency, so handle accordingly.
if (token.type == Token::OBJECT_END) {
- delete dict;
- return false;
+ if (!allow_trailing_comma_) {
+ delete dict;
+ return false;
+ }
+ // Trailing comma OK, stop parsing the Object.
+ break;
}
} else if (token.type != Token::OBJECT_END) {
// Unexpected value after last object value. Bail out.
diff --git a/base/json_reader.h b/base/json_reader.h
index 97f68fd..46e15ae 100644
--- a/base/json_reader.h
+++ b/base/json_reader.h
@@ -46,11 +46,11 @@
// character, the function skips a Unicode BOM at the beginning of the
// 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.
-// Parsing options:
-// - Relax trailing commas in arrays and objects
-// - Relax object keys being wrapped in double quotes
-// - Disable comment stripping
+// TODO(tc): It would be nice to give back an error string when we fail to
+// parse JSON.
+// TODO(tc): Add a parsing option to to relax object keys being wrapped in
+// double quotes
+// TODO(tc): Add an option to disable comment stripping
#ifndef 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
- // properly formed JSON string, returns false and leaves root unaltered.
- static bool Read(const std::string& json, Value** root);
+ // Reads and parses |json| and populates |root|. If |json| is not a properly
+ // formed JSON string, returns false and leaves root unaltered. If
+ // 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:
- JSONReader(const wchar_t* json_start_pos);
+ JSONReader(const wchar_t* json_start_pos, bool allow_trailing_comma);
DISALLOW_EVIL_CONSTRUCTORS(JSONReader);
FRIEND_TEST(JSONReaderTest, Reading);
@@ -112,7 +116,8 @@ class JSONReader {
// Pass through method from JSONReader::Read. We have this so unittests can
// disable the root check.
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
// 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.
int stack_depth_;
+
+ // A parser flag that allows trailing commas in objects and arrays.
+ bool allow_trailing_comma_;
};
#endif // CHROME_COMMON_JSON_READER_H__
diff --git a/base/json_reader_unittest.cc b/base/json_reader_unittest.cc
index f218531..130c8e8 100644
--- a/base/json_reader_unittest.cc
+++ b/base/json_reader_unittest.cc
@@ -34,26 +34,26 @@
TEST(JSONReaderTest, Reading) {
// some whitespace checking
Value* root = NULL;
- ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false));
+ ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
delete root;
// Invalid JSON string
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false, false));
ASSERT_FALSE(root);
// Simple bool
root = NULL;
- ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false));
+ ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
delete root;
// Test number formats
root = NULL;
- ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false));
+ ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int int_val = 0;
@@ -63,19 +63,19 @@ TEST(JSONReaderTest, Reading) {
// According to RFC4627, oct, hex, and leading zeros are invalid JSON.
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false, false));
ASSERT_FALSE(root);
// Test 0 (which needs to be special cased because of the leading zero
// clause).
root = NULL;
- ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false));
+ ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int_val = 1;
@@ -85,15 +85,15 @@ TEST(JSONReaderTest, Reading) {
// Numbers that overflow ints should fail
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("2147483648", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("2147483648", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("-2147483649", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("-2147483649", &root, false, false));
ASSERT_FALSE(root);
// Parse a double
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->IsType(Value::TYPE_REAL));
double real_val = 0.0;
@@ -102,7 +102,7 @@ TEST(JSONReaderTest, Reading) {
delete root;
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->IsType(Value::TYPE_REAL));
real_val = 0.0;
@@ -111,7 +111,7 @@ TEST(JSONReaderTest, Reading) {
delete root;
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->IsType(Value::TYPE_REAL));
real_val = 0.0;
@@ -120,7 +120,7 @@ TEST(JSONReaderTest, Reading) {
delete root;
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->IsType(Value::TYPE_REAL));
real_val = 0.0;
@@ -129,7 +129,7 @@ TEST(JSONReaderTest, Reading) {
delete root;
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->IsType(Value::TYPE_REAL));
real_val = 0.0;
@@ -138,7 +138,7 @@ TEST(JSONReaderTest, Reading) {
delete root;
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->IsType(Value::TYPE_REAL));
real_val = 0.0;
@@ -148,51 +148,51 @@ TEST(JSONReaderTest, Reading) {
// Fractional parts must have a digit before and after the decimal point.
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false, false));
ASSERT_FALSE(root);
// Exponent must have a digit following the 'e'.
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false, false));
ASSERT_FALSE(root);
// INF/-INF/NaN are not valid
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false, false));
ASSERT_FALSE(root);
// Invalid number formats
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false, false));
ASSERT_FALSE(root);
// Test string parser
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->IsType(Value::TYPE_STRING));
std::wstring str_val;
@@ -202,7 +202,7 @@ TEST(JSONReaderTest, Reading) {
// Empty string
root = NULL;
- ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false));
+ ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
@@ -213,7 +213,7 @@ TEST(JSONReaderTest, Reading) {
// Test basic string escapes
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\"", &root,
- false));
+ false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
@@ -223,7 +223,8 @@ TEST(JSONReaderTest, Reading) {
// Test hex and unicode escapes including the null character.
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->IsType(Value::TYPE_STRING));
str_val.clear();
@@ -233,41 +234,48 @@ TEST(JSONReaderTest, Reading) {
// Test invalid strings
root = NULL;
- ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false));
+ ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false,
+ false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\z invalid escape char\"", &root,
- false));
+ false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\xAQ invalid hex code\"", &root,
- false));
+ false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("not enough hex chars\\x1\"", &root,
- false));
+ false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"not enough escape chars\\u123\"",
- &root, false));
+ &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"extra backslash at end of input\\\"",
- &root, false));
+ &root, false, false));
ASSERT_FALSE(root);
// Basic array
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->IsType(Value::TYPE_LIST));
ListValue* list = static_cast<ListValue*>(root);
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 root2;
// Empty array
root = NULL;
- ASSERT_TRUE(JSONReader::Read("[]", &root));
+ ASSERT_TRUE(JSONReader::Read("[]", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
@@ -276,43 +284,81 @@ TEST(JSONReaderTest, Reading) {
// Nested arrays
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->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
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 root2;
// Invalid, missing close brace.
root = NULL;
- ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root));
+ ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root,
+ false));
ASSERT_FALSE(root);
// Invalid, too many commas
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);
// Invalid, no commas
root = NULL;
- ASSERT_FALSE(JSONReader::Read("[true null]", &root));
+ ASSERT_FALSE(JSONReader::Read("[true null]", &root, false));
ASSERT_FALSE(root);
// Invalid, trailing comma
root = NULL;
- ASSERT_FALSE(JSONReader::Read("[true,]", &root));
+ ASSERT_FALSE(JSONReader::Read("[true,]", &root, false));
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
root = NULL;
- ASSERT_TRUE(JSONReader::Read("{}", &root));
+ ASSERT_TRUE(JSONReader::Read("{}", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
delete root;
root = NULL;
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->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* dict_val = static_cast<DictionaryValue*>(root);
@@ -325,12 +371,19 @@ TEST(JSONReaderTest, Reading) {
str_val.clear();
ASSERT_TRUE(dict_val->GetString(L"S", &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 root2;
// Test nesting
root = NULL;
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->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root);
@@ -339,43 +392,66 @@ TEST(JSONReaderTest, Reading) {
ListValue* inner_array = NULL;
ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
ASSERT_EQ(1, inner_array->GetSize());
- bool bool_value = true;
+ bool_value = true;
ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
ASSERT_FALSE(bool_value);
inner_dict = NULL;
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 root2;
// Invalid, no closing brace
root = NULL;
- ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root));
+ ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root, false));
ASSERT_FALSE(root);
// Invalid, keys must be quoted
root = NULL;
- ASSERT_FALSE(JSONReader::Read("{foo:true}", &root));
+ ASSERT_FALSE(JSONReader::Read("{foo:true}", &root, false));
ASSERT_FALSE(root);
// Invalid, trailing comma
root = NULL;
- ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root));
+ ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root, false));
ASSERT_FALSE(root);
// Invalid, too many commas
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);
// Invalid, no separator
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);
// Test stack overflow
root = NULL;
std::string evil(1000000, '[');
evil.append(std::string(1000000, ']'));
- ASSERT_FALSE(JSONReader::Read(evil, &root));
+ ASSERT_FALSE(JSONReader::Read(evil, &root, false));
ASSERT_FALSE(root);
// A few thousand adjacent lists is fine.
@@ -385,7 +461,7 @@ TEST(JSONReaderTest, Reading) {
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->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
@@ -395,7 +471,7 @@ TEST(JSONReaderTest, Reading) {
// Test utf8 encoded input
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root,
- false));
+ false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
@@ -405,8 +481,8 @@ TEST(JSONReaderTest, Reading) {
// Test invalid root objects.
root = NULL;
- ASSERT_FALSE(JSONReader::Read("null", &root));
- ASSERT_FALSE(JSONReader::Read("true", &root));
- ASSERT_FALSE(JSONReader::Read("10", &root));
- ASSERT_FALSE(JSONReader::Read("\"root\"", &root));
+ ASSERT_FALSE(JSONReader::Read("null", &root, false));
+ ASSERT_FALSE(JSONReader::Read("true", &root, false));
+ ASSERT_FALSE(JSONReader::Read("10", &root, false));
+ ASSERT_FALSE(JSONReader::Read("\"root\"", &root, false));
}