diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-29 19:59:08 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-29 19:59:08 +0000 |
commit | b4cebf87816cde49f6d4991ffa254e5ead97703b (patch) | |
tree | fb12e4b464a43af846f30bbd0dbe9484150373b0 /base | |
parent | 45ce59f19161b517c04a4e3bcd0890b938382b06 (diff) | |
download | chromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.zip chromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.tar.gz chromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.tar.bz2 |
Change the signature of JSONReader::Read() and related
methods to be more friendly to use with scoped_ptr. Change
all the callsites.
Review URL: http://codereview.chromium.org/16270
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/json_reader.cc | 168 | ||||
-rw-r--r-- | base/json_reader.h | 38 | ||||
-rw-r--r-- | base/json_reader_unittest.cc | 514 | ||||
-rw-r--r-- | base/values.h | 11 |
4 files changed, 307 insertions, 424 deletions
diff --git a/base/json_reader.cc b/base/json_reader.cc index 2cea239..bf233a6 100644 --- a/base/json_reader.cc +++ b/base/json_reader.cc @@ -89,25 +89,24 @@ const char* JSONReader::kUnquotedDictionaryKey = "Dictionary keys must be quoted."; /* static */ -bool JSONReader::Read(const std::string& json, - Value** root, - bool allow_trailing_comma) { - return ReadAndReturnError(json, root, allow_trailing_comma, NULL); +Value* JSONReader::Read(const std::string& json, + bool allow_trailing_comma) { + return ReadAndReturnError(json, allow_trailing_comma, NULL); } /* static */ -bool JSONReader::ReadAndReturnError(const std::string& json, - Value** root, - bool allow_trailing_comma, - std::string *error_message_out) { +Value* JSONReader::ReadAndReturnError(const std::string& json, + bool allow_trailing_comma, + std::string *error_message_out) { JSONReader reader = JSONReader(); - if (reader.JsonToValue(json, root, true, allow_trailing_comma)) { - return true; - } else { - if (error_message_out) - *error_message_out = reader.error_message(); - return false; - } + Value* root = reader.JsonToValue(json, true, allow_trailing_comma); + if (root) + return root; + + if (error_message_out) + *error_message_out = reader.error_message(); + + return NULL; } /* static */ @@ -121,12 +120,12 @@ JSONReader::JSONReader() : start_pos_(NULL), json_pos_(NULL), stack_depth_(0), allow_trailing_comma_(false) {} -bool JSONReader::JsonToValue(const std::string& json, Value** root, - bool check_root, bool allow_trailing_comma) { +Value* JSONReader::JsonToValue(const std::string& json, bool check_root, + bool allow_trailing_comma) { // The input must be in UTF-8. if (!IsStringUTF8(json.c_str())) { error_message_ = kUnsupportedEncoding; - return false; + return NULL; } // The conversion from UTF8 to wstring removes null bytes for us @@ -148,13 +147,10 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root, stack_depth_ = 0; error_message_.clear(); - Value* temp_root = NULL; - - // Only modify root_ if we have valid JSON and nothing else. - if (BuildValue(&temp_root, check_root)) { + scoped_ptr<Value> root(BuildValue(check_root)); + if (root.get()) { if (ParseToken().type == Token::END_OF_INPUT) { - *root = temp_root; - return true; + return root.release(); } else { SetErrorMessage(kUnexpectedDataAfterRoot, json_pos_); } @@ -164,16 +160,14 @@ bool JSONReader::JsonToValue(const std::string& json, Value** root, if (error_message_.empty()) SetErrorMessage(kSyntaxError, json_pos_); - if (temp_root) - delete temp_root; - return false; + return NULL; } -bool JSONReader::BuildValue(Value** node, bool is_root) { +Value* JSONReader::BuildValue(bool is_root) { ++stack_depth_; if (stack_depth_ > kStackLimit) { SetErrorMessage(kTooMuchNesting, json_pos_); - return false; + return NULL; } Token token = ParseToken(); @@ -181,34 +175,38 @@ bool JSONReader::BuildValue(Value** node, bool is_root) { if (is_root && token.type != Token::OBJECT_BEGIN && token.type != Token::ARRAY_BEGIN) { SetErrorMessage(kBadRootElementType, json_pos_); - return false; + return NULL; } + scoped_ptr<Value> node; + switch (token.type) { case Token::END_OF_INPUT: case Token::INVALID_TOKEN: - return false; + return NULL; case Token::NULL_TOKEN: - *node = Value::CreateNullValue(); + node.reset(Value::CreateNullValue()); break; case Token::BOOL_TRUE: - *node = Value::CreateBooleanValue(true); + node.reset(Value::CreateBooleanValue(true)); break; case Token::BOOL_FALSE: - *node = Value::CreateBooleanValue(false); + node.reset(Value::CreateBooleanValue(false)); break; case Token::NUMBER: - if (!DecodeNumber(token, node)) - return false; + node.reset(DecodeNumber(token)); + if (!node.get()) + return NULL; break; case Token::STRING: - if (!DecodeString(token, node)) - return false; + node.reset(DecodeString(token)); + if (!node.get()) + return NULL; break; case Token::ARRAY_BEGIN: @@ -216,14 +214,13 @@ bool JSONReader::BuildValue(Value** node, bool is_root) { json_pos_ += token.length; token = ParseToken(); - ListValue* array = new ListValue; + node.reset(new ListValue()); while (token.type != Token::ARRAY_END) { - Value* array_node = NULL; - if (!BuildValue(&array_node, false)) { - delete array; - return false; + Value* array_node = BuildValue(false); + if (!array_node) { + return NULL; } - array->Append(array_node); + static_cast<ListValue*>(node.get())->Append(array_node); // After a list value, we expect a comma or the end of the list. token = ParseToken(); @@ -235,23 +232,19 @@ bool JSONReader::BuildValue(Value** node, bool is_root) { if (token.type == Token::ARRAY_END) { if (!allow_trailing_comma_) { SetErrorMessage(kTrailingComma, json_pos_); - delete array; - return false; + return NULL; } // Trailing comma OK, stop parsing the Array. break; } } else if (token.type != Token::ARRAY_END) { // Unexpected value after list value. Bail out. - delete array; - return false; + return NULL; } } if (token.type != Token::ARRAY_END) { - delete array; - return false; + return NULL; } - *node = array; break; } @@ -260,39 +253,32 @@ bool JSONReader::BuildValue(Value** node, bool is_root) { json_pos_ += token.length; token = ParseToken(); - DictionaryValue* dict = new DictionaryValue; + node.reset(new DictionaryValue); while (token.type != Token::OBJECT_END) { if (token.type != Token::STRING) { SetErrorMessage(kUnquotedDictionaryKey, json_pos_); - delete dict; - return false; - } - Value* dict_key_value = NULL; - if (!DecodeString(token, &dict_key_value)) { - delete dict; - return false; + return NULL; } + scoped_ptr<Value> dict_key_value(DecodeString(token)); + if (!dict_key_value.get()) + return NULL; + // Convert the key into a wstring. std::wstring dict_key; bool success = dict_key_value->GetAsString(&dict_key); DCHECK(success); - delete dict_key_value; json_pos_ += token.length; token = ParseToken(); - if (token.type != Token::OBJECT_PAIR_SEPARATOR) { - delete dict; - return false; - } + if (token.type != Token::OBJECT_PAIR_SEPARATOR) + return NULL; json_pos_ += token.length; token = ParseToken(); - Value* dict_value = NULL; - if (!BuildValue(&dict_value, false)) { - delete dict; - return false; - } - dict->Set(dict_key, dict_value); + Value* dict_value = BuildValue(false); + if (!dict_value) + return NULL; + static_cast<DictionaryValue*>(node.get())->Set(dict_key, dict_value); // After a key/value pair, we expect a comma or the end of the // object. @@ -305,34 +291,30 @@ bool JSONReader::BuildValue(Value** node, bool is_root) { if (token.type == Token::OBJECT_END) { if (!allow_trailing_comma_) { SetErrorMessage(kTrailingComma, json_pos_); - delete dict; - return false; + return NULL; } // Trailing comma OK, stop parsing the Object. break; } } else if (token.type != Token::OBJECT_END) { // Unexpected value after last object value. Bail out. - delete dict; - return false; + return NULL; } } - if (token.type != Token::OBJECT_END) { - delete dict; - return false; - } - *node = dict; + if (token.type != Token::OBJECT_END) + return NULL; + break; } default: // We got a token that's not a value. - return false; + return NULL; } json_pos_ += token.length; --stack_depth_; - return true; + return node.release(); } JSONReader::Token JSONReader::ParseNumberToken() { @@ -372,22 +354,18 @@ JSONReader::Token JSONReader::ParseNumberToken() { return token; } -bool JSONReader::DecodeNumber(const Token& token, Value** node) { +Value* JSONReader::DecodeNumber(const Token& token) { const std::wstring num_string(token.begin, token.length); int num_int; - if (StringToInt(num_string, &num_int)) { - *node = Value::CreateIntegerValue(num_int); - return true; - } + if (StringToInt(num_string, &num_int)) + return Value::CreateIntegerValue(num_int); double num_double; - if (StringToDouble(num_string, &num_double) && base::IsFinite(num_double)) { - *node = Value::CreateRealValue(num_double); - return true; - } + if (StringToDouble(num_string, &num_double) && base::IsFinite(num_double)) + return Value::CreateRealValue(num_double); - return false; + return NULL; } JSONReader::Token JSONReader::ParseStringToken() { @@ -435,7 +413,7 @@ JSONReader::Token JSONReader::ParseStringToken() { return kInvalidToken; } -bool JSONReader::DecodeString(const Token& token, Value** node) { +Value* JSONReader::DecodeString(const Token& token) { std::wstring decoded_str; decoded_str.reserve(token.length - 2); @@ -486,16 +464,14 @@ bool JSONReader::DecodeString(const Token& token, Value** node) { // We should only have valid strings at this point. If not, // ParseStringToken didn't do it's job. NOTREACHED(); - return false; + return NULL; } } else { // Not escaped decoded_str.push_back(c); } } - *node = Value::CreateStringValue(decoded_str); - - return true; + return Value::CreateStringValue(decoded_str); } JSONReader::Token JSONReader::ParseToken() { diff --git a/base/json_reader.h b/base/json_reader.h index 2c9f27a..6231581 100644 --- a/base/json_reader.h +++ b/base/json_reader.h @@ -85,22 +85,19 @@ class JSONReader { static const char* kUnsupportedEncoding; static const char* kUnquotedDictionaryKey; - // Reads and parses |json| and populates |root|. If |json| is not a properly - // formed JSON string, returns false, leaves root unaltered and sets - // error_message if it was non-null. 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); + // Reads and parses |json|, returning a Value. The caller owns the returned + // instance. If |json| is not a properly formed JSON string, returns NULL. + // If allow_trailing_comma is true, we will ignore trailing commas in objects + // and arrays even though this goes against the RFC. + static Value* Read(const std::string& json, + bool allow_trailing_comma); // Reads and parses |json| like Read(). |error_message_out| is optional. If - // specified and false is returned, error_message_out will be populated with + // specified and NULL is returned, error_message_out will be populated with // a string describing the error. Otherwise, error_message_out is unmodified. - static bool ReadAndReturnError(const std::string& json, - Value** root, - bool allow_trailing_comma, - std::string *error_message_out); + static Value* ReadAndReturnError(const std::string& json, + bool allow_trailing_comma, + std::string *error_message_out); private: static std::string FormatErrorMessage(int line, int column, @@ -118,13 +115,13 @@ class JSONReader { // Pass through method from JSONReader::Read. We have this so unittests can // disable the root check. - bool JsonToValue(const std::string& json, Value** root, bool check_root, - bool allow_trailing_comma); + Value* JsonToValue(const std::string& json, bool check_root, + bool allow_trailing_comma); - // Recursively build Value. Returns false if we don't have a valid JSON + // Recursively build Value. Returns NULL if we don't have a valid JSON // string. If |is_root| is true, we verify that the root element is either // an object or an array. - bool BuildValue(Value** root, bool is_root); + Value* BuildValue(bool is_root); // Parses a sequence of characters into a Token::NUMBER. If the sequence of // characters is not a valid number, returns a Token::INVALID_TOKEN. Note @@ -133,9 +130,8 @@ class JSONReader { Token ParseNumberToken(); // Try and convert the substring that token holds into an int or a double. If - // we can (ie., no overflow), return true and create the appropriate value - // for |node|. Return false if we can't do the conversion. - bool DecodeNumber(const Token& token, Value** node); + // we can (ie., no overflow), return the value, else return NULL. + Value* DecodeNumber(const Token& token); // Parses a sequence of characters into a Token::STRING. If the sequence of // characters is not a valid string, returns a Token::INVALID_TOKEN. Note @@ -146,7 +142,7 @@ class JSONReader { // Convert the substring into a value string. This should always succeed // (otherwise ParseStringToken would have failed), but returns a success bool // just in case. - bool DecodeString(const Token& token, Value** node); + Value* DecodeString(const Token& token); // Grabs the next token in the JSON stream. This does not increment the // stream so it can be used to look ahead at the next token. diff --git a/base/json_reader_unittest.cc b/base/json_reader_unittest.cc index 7ded39f..9153289 100644 --- a/base/json_reader_unittest.cc +++ b/base/json_reader_unittest.cc @@ -4,66 +4,55 @@ #include "testing/gtest/include/gtest/gtest.h" #include "base/json_reader.h" +#include "base/scoped_ptr.h" #include "base/values.h" #include "build/build_config.h" TEST(JSONReaderTest, Reading) { // some whitespace checking - Value* root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue(" null ", &root, false, false)); - ASSERT_TRUE(root); + scoped_ptr<Value> root; + root.reset(JSONReader().JsonToValue(" null ", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); - delete root; // Invalid JSON string - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("nu", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("nu", false, false)); + ASSERT_FALSE(root.get()); // Simple bool - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("true ", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("true ", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); - delete root; // Test number formats - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("43", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("43", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); int int_val = 0; ASSERT_TRUE(root->GetAsInteger(&int_val)); ASSERT_EQ(43, int_val); - delete root; // According to RFC4627, oct, hex, and leading zeros are invalid JSON. - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("043", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("0x43", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("00", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("043", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("0x43", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("00", false, false)); + ASSERT_FALSE(root.get()); // Test 0 (which needs to be special cased because of the leading zero // clause). - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("0", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("0", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); int_val = 1; ASSERT_TRUE(root->GetAsInteger(&int_val)); ASSERT_EQ(0, int_val); - delete root; // Numbers that overflow ints should succeed, being internally promoted to // storage as doubles - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("2147483648", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("2147483648", false, false)); + ASSERT_TRUE(root.get()); double real_val; #ifdef ARCH_CPU_32_BITS ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); @@ -76,10 +65,8 @@ TEST(JSONReaderTest, Reading) { ASSERT_TRUE(root->GetAsInteger(&int_val)); ASSERT_EQ(2147483648, int_val); #endif - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("-2147483649", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("-2147483649", false, false)); + ASSERT_TRUE(root.get()); #ifdef ARCH_CPU_32_BITS ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; @@ -91,250 +78,194 @@ TEST(JSONReaderTest, Reading) { ASSERT_TRUE(root->GetAsInteger(&int_val)); ASSERT_EQ(-2147483649, int_val); #endif - delete root; // Parse a double - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("43.1", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("43.1", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(43.1, real_val); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("4.3e-1", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(.43, real_val); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("2.1e0", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("2.1e0", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(2.1, real_val); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("2.1e+0001", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("2.1e+0001", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(21.0, real_val); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("0.01", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("0.01", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(0.01, real_val); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("1.00", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("1.00", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); real_val = 0.0; ASSERT_TRUE(root->GetAsReal(&real_val)); ASSERT_DOUBLE_EQ(1.0, real_val); - delete root; // Fractional parts must have a digit before and after the decimal point. - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1.", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue(".1", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1.e10", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("1.", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue(".1", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("1.e10", false, false)); + ASSERT_FALSE(root.get()); // Exponent must have a digit following the 'e'. - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1e", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1E", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1e1.", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1e1.0", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("1e", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("1E", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("1e1.", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("1e1.0", false, false)); + ASSERT_FALSE(root.get()); // INF/-INF/NaN are not valid - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("1e1000", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("-1e1000", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("NaN", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("nan", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("inf", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("1e1000", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("-1e1000", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("NaN", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("nan", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("inf", false, false)); + ASSERT_FALSE(root.get()); // Invalid number formats - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("4.3.1", &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("4e3.1", &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("4.3.1", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("4e3.1", false, false)); + ASSERT_FALSE(root.get()); // Test string parser - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("\"hello world\"", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("\"hello world\"", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); std::wstring str_val; ASSERT_TRUE(root->GetAsString(&str_val)); ASSERT_EQ(L"hello world", str_val); - delete root; // Empty string - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("\"\"", &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("\"\"", false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); str_val.clear(); ASSERT_TRUE(root->GetAsString(&str_val)); ASSERT_EQ(L"", str_val); - delete root; // Test basic string escapes - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", - &root, false, false)); - ASSERT_TRUE(root); + root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", + false, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); str_val.clear(); ASSERT_TRUE(root->GetAsString(&str_val)); ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val); - delete root; // Test hex and unicode escapes including the null character. - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", &root, false, + root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false, false)); - ASSERT_TRUE(root); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); str_val.clear(); ASSERT_TRUE(root->GetAsString(&str_val)); ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val); - delete root; // Test invalid strings - root = NULL; - 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)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", &root, - false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("not enough hex chars\\x1\"", &root, - false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", - &root, false, false)); - ASSERT_FALSE(root); - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", - &root, false, false)); - ASSERT_FALSE(root); + root.reset(JSONReader().JsonToValue("\"no closing quote", false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false, + false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false, + false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("not enough hex chars\\x1\"", false, + false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", + false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", + false, false)); + ASSERT_FALSE(root.get()); // Basic array - root = NULL; - ASSERT_TRUE(JSONReader::Read("[true, false, null]", &root, false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read("[true, false, null]", false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); - ListValue* list = static_cast<ListValue*>(root); + ListValue* list = static_cast<ListValue*>(root.get()); ASSERT_EQ(3U, 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; + scoped_ptr<Value> root2; + root2.reset(JSONReader::Read("[true, false, null, ]", true)); + EXPECT_TRUE(root->Equals(root2.get())); // Empty array - root = NULL; - ASSERT_TRUE(JSONReader::Read("[]", &root, false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read("[]", false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); - list = static_cast<ListValue*>(root); + list = static_cast<ListValue*>(root.get()); ASSERT_EQ(0U, list->GetSize()); - delete root; // Nested arrays - root = NULL; - ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null]], null]", &root, - false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]", + false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); - list = static_cast<ListValue*>(root); + list = static_cast<ListValue*>(root.get()); ASSERT_EQ(4U, 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; + root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", + true)); + EXPECT_TRUE(root->Equals(root2.get())); // Invalid, missing close brace. - root = NULL; - ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root, - false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false)); + ASSERT_FALSE(root.get()); // Invalid, too many commas - root = NULL; - ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, false)); - ASSERT_FALSE(root); - ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, true)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("[true,, null]", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("[true,, null]", true)); + ASSERT_FALSE(root.get()); // Invalid, no commas - root = NULL; - ASSERT_FALSE(JSONReader::Read("[true null]", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("[true null]", false)); + ASSERT_FALSE(root.get()); // Invalid, trailing comma - root = NULL; - ASSERT_FALSE(JSONReader::Read("[true,]", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("[true,]", false)); + ASSERT_FALSE(root.get()); // Valid if we set |allow_trailing_comma| to true. - EXPECT_TRUE(JSONReader::Read("[true,]", &root, true)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read("[true,]", true)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); - list = static_cast<ListValue*>(root); + list = static_cast<ListValue*>(root.get()); EXPECT_EQ(1U, list->GetSize()); Value* tmp_value = NULL; ASSERT_TRUE(list->Get(0, &tmp_value)); @@ -342,34 +273,29 @@ TEST(JSONReaderTest, Reading) { 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); + root.reset(JSONReader::Read("[,]", true)); + EXPECT_FALSE(root.get()); + root.reset(JSONReader::Read("[true,,]", true)); + EXPECT_FALSE(root.get()); + root.reset(JSONReader::Read("[,true,]", true)); + EXPECT_FALSE(root.get()); + root.reset(JSONReader::Read("[true,,false]", true)); + EXPECT_FALSE(root.get()); // Test objects - root = NULL; - ASSERT_TRUE(JSONReader::Read("{}", &root, false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read("{}", false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); - delete root; - root = NULL; - ASSERT_TRUE(JSONReader::Read( - "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", &root, + root.reset(JSONReader::Read( + "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false)); - ASSERT_TRUE(root); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); - DictionaryValue* dict_val = static_cast<DictionaryValue*>(root); + DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); real_val = 0.0; ASSERT_TRUE(dict_val->GetReal(L"number", &real_val)); ASSERT_DOUBLE_EQ(9.87654321, real_val); @@ -380,21 +306,16 @@ TEST(JSONReaderTest, Reading) { 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; + root2.reset(JSONReader::Read( + "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); + EXPECT_TRUE(root->Equals(root2.get())); // Test nesting - root = NULL; - ASSERT_TRUE(JSONReader::Read( - "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", &root, false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read( + "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); - dict_val = static_cast<DictionaryValue*>(root); + dict_val = static_cast<DictionaryValue*>(root.get()); DictionaryValue* inner_dict = NULL; ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict)); ListValue* inner_array = NULL; @@ -406,61 +327,49 @@ TEST(JSONReaderTest, Reading) { 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; + root2.reset(JSONReader::Read( + "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); + EXPECT_TRUE(root->Equals(root2.get())); // Invalid, no closing brace - root = NULL; - ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("{\"a\": true", false)); + ASSERT_FALSE(root.get()); // Invalid, keys must be quoted - root = NULL; - ASSERT_FALSE(JSONReader::Read("{foo:true}", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("{foo:true}", false)); + ASSERT_FALSE(root.get()); // Invalid, trailing comma - root = NULL; - ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("{\"a\":true,}", false)); + ASSERT_FALSE(root.get()); // Invalid, too many commas - root = NULL; - 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); + root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); + ASSERT_FALSE(root.get()); // Invalid, no separator - root = NULL; - ASSERT_FALSE(JSONReader::Read("{\"a\" \"b\"}", &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read("{\"a\" \"b\"}", false)); + ASSERT_FALSE(root.get()); // 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); + root.reset(JSONReader::Read("{,}", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("{,}", true)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("{\"a\":true,,}", true)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("{,\"a\":true}", true)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); + ASSERT_FALSE(root.get()); // Test stack overflow - root = NULL; std::string evil(1000000, '['); evil.append(std::string(1000000, ']')); - ASSERT_FALSE(JSONReader::Read(evil, &root, false)); - ASSERT_FALSE(root); + root.reset(JSONReader::Read(evil, false)); + ASSERT_FALSE(root.get()); // A few thousand adjacent lists is fine. std::string not_evil("["); @@ -469,58 +378,58 @@ TEST(JSONReaderTest, Reading) { not_evil.append("[],"); } not_evil.append("[]]"); - ASSERT_TRUE(JSONReader::Read(not_evil, &root, false)); - ASSERT_TRUE(root); + root.reset(JSONReader::Read(not_evil, false)); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); - list = static_cast<ListValue*>(root); + list = static_cast<ListValue*>(root.get()); ASSERT_EQ(5001U, list->GetSize()); - delete root; // Test utf8 encoded input - root = NULL; - ASSERT_TRUE(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root, + root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", false, false)); - ASSERT_TRUE(root); + ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); str_val.clear(); ASSERT_TRUE(root->GetAsString(&str_val)); ASSERT_EQ(L"\x7f51\x9875", str_val); - delete root; // Test invalid utf8 encoded input - root = NULL; - ASSERT_FALSE(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", &root, - false, false)); - ASSERT_FALSE(JSONReader().JsonToValue("\"123\xc0\x81\"", &root, - false, false)); + root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", + false, false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", + false, false)); + ASSERT_FALSE(root.get()); // Test invalid root objects. - root = NULL; - 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)); + root.reset(JSONReader::Read("null", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("true", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("10", false)); + ASSERT_FALSE(root.get()); + root.reset(JSONReader::Read("\"root\"", false)); + ASSERT_FALSE(root.get()); } TEST(JSONReaderTest, ErrorMessages) { // Error strings should not be modified in case of success. std::string error_message; - Value* root = NULL; - EXPECT_TRUE(JSONReader::ReadAndReturnError("[42]", &root, false, - &error_message)); + scoped_ptr<Value> root; + root.reset(JSONReader::ReadAndReturnError("[42]", false, &error_message)); EXPECT_TRUE(error_message.empty()); // Test line and column counting const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; // error here --------------------------------^ - EXPECT_FALSE(JSONReader::ReadAndReturnError(big_json, &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError(big_json, false, &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError), error_message); // Test each of the error conditions - EXPECT_FALSE(JSONReader::ReadAndReturnError("{},{}", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("{},{}", false, &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3, JSONReader::kUnexpectedDataAfterRoot), error_message); @@ -529,50 +438,55 @@ TEST(JSONReaderTest, ErrorMessages) { nested_json.insert(nested_json.begin(), '['); nested_json.append(1, ']'); } - EXPECT_FALSE(JSONReader::ReadAndReturnError(nested_json, &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError(nested_json, false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("42", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("42", false, &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1, JSONReader::kBadRootElementType), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("[1,]", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("[1,]", false, &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("{foo:\"bar\"}", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kUnquotedDictionaryKey), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", &root, - false, &error_message)); + root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("[nu]", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("[nu]", false, &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), error_message); - EXPECT_FALSE(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", &root, false, - &error_message)); + root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, + &error_message)); + EXPECT_FALSE(root.get()); EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), error_message); - delete root; } diff --git a/base/values.h b/base/values.h index 2f28f11..ebd2117 100644 --- a/base/values.h +++ b/base/values.h @@ -358,13 +358,10 @@ class ValueSerializer { virtual bool Serialize(const Value& root) = 0; // This method deserializes the subclass-specific format into a Value object. - // The method should return true if and only if the root parameter is set - // to a complete Value representation of the serialized form. If the - // return value is true, the caller takes ownership of the objects pointed - // to by root. If the return value is false, root should be unchanged and if - // error_message is non-null, it should be filled with a message describing - // the error. - virtual bool Deserialize(Value** root, std::string* error_message) = 0; + // If the return value is non-NULL, the caller takes ownership of returned + // Value. If the return value is NULL, and if error_message is non-NULL, + // error_message should be filled with a message describing the error. + virtual Value* Deserialize(std::string* error_message) = 0; }; #endif // BASE_VALUES_H_ |