diff options
author | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-29 00:01:31 +0000 |
---|---|---|
committer | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-29 00:01:31 +0000 |
commit | e724599d3f3120f186bcb6e45fa7a2d932fe8d6c (patch) | |
tree | 6530a909acf95968f7c1f111b448dd62cfefec65 /base/json_reader.cc | |
parent | 1b66f31cbe79793c038c5afe09bb24dd2f25b8ad (diff) | |
download | chromium_src-e724599d3f3120f186bcb6e45fa7a2d932fe8d6c.zip chromium_src-e724599d3f3120f186bcb6e45fa7a2d932fe8d6c.tar.gz chromium_src-e724599d3f3120f186bcb6e45fa7a2d932fe8d6c.tar.bz2 |
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
Diffstat (limited to 'base/json_reader.cc')
-rw-r--r-- | base/json_reader.cc | 44 |
1 files changed, 30 insertions, 14 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. |