summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-29 19:59:08 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-29 19:59:08 +0000
commitb4cebf87816cde49f6d4991ffa254e5ead97703b (patch)
treefb12e4b464a43af846f30bbd0dbe9484150373b0
parent45ce59f19161b517c04a4e3bcd0890b938382b06 (diff)
downloadchromium_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
-rw-r--r--base/json_reader.cc168
-rw-r--r--base/json_reader.h38
-rw-r--r--base/json_reader_unittest.cc514
-rw-r--r--base/values.h11
-rw-r--r--chrome/browser/autocomplete/search_provider.cc15
-rw-r--r--chrome/browser/bookmarks/bookmark_storage.cc2
-rw-r--r--chrome/browser/debugger/debugger_host_impl.cpp4
-rw-r--r--chrome/browser/dom_ui/dom_ui.cc8
-rw-r--r--chrome/browser/dom_ui/dom_ui_host.cc8
-rw-r--r--chrome/browser/extensions/extensions_service.cc7
-rw-r--r--chrome/browser/page_state.cc13
-rw-r--r--chrome/common/json_value_serializer.cc11
-rw-r--r--chrome/common/json_value_serializer.h16
-rw-r--r--chrome/common/json_value_serializer_perftest.cc9
-rw-r--r--chrome/common/json_value_serializer_unittest.cc96
-rw-r--r--chrome/common/pref_service.cc42
-rw-r--r--chrome/common/pref_service_uitest.cc8
-rw-r--r--chrome/common/pref_service_unittest.cc6
-rw-r--r--chrome/installer/util/master_preferences.cc11
-rw-r--r--chrome/test/automation/tab_proxy.cc4
-rw-r--r--chrome/test/ui/ui_test.cc10
21 files changed, 428 insertions, 573 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_
diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc
index 32c2fb2..17bdcc8 100644
--- a/chrome/browser/autocomplete/search_provider.cc
+++ b/chrome/browser/autocomplete/search_provider.cc
@@ -129,13 +129,14 @@ void SearchProvider::OnURLFetchComplete(const URLFetcher* source,
}
}
- JSONStringValueSerializer deserializer(json_data);
- deserializer.set_allow_trailing_comma(true);
- Value* root_val = NULL;
- have_suggest_results_ = status.is_success() && (response_code == 200) &&
- deserializer.Deserialize(&root_val, NULL) &&
- ParseSuggestResults(root_val);
- delete root_val;
+ if (status.is_success() && response_code == 200) {
+ JSONStringValueSerializer deserializer(json_data);
+ deserializer.set_allow_trailing_comma(true);
+ scoped_ptr<Value> root_val(deserializer.Deserialize(NULL));
+ have_suggest_results_ =
+ root_val.get() && ParseSuggestResults(root_val.get());
+ }
+
ConvertResultsToAutocompleteMatches();
listener_->OnProviderUpdate(!suggest_results_.empty());
}
diff --git a/chrome/browser/bookmarks/bookmark_storage.cc b/chrome/browser/bookmarks/bookmark_storage.cc
index e6e9a50..23f47b4 100644
--- a/chrome/browser/bookmarks/bookmark_storage.cc
+++ b/chrome/browser/bookmarks/bookmark_storage.cc
@@ -167,7 +167,7 @@ void BookmarkStorageBackend::Read(scoped_refptr<BookmarkStorage> service,
Value* root = NULL;
if (bookmark_file_exists) {
JSONFileValueSerializer serializer(path);
- serializer.Deserialize(&root, NULL);
+ root = serializer.Deserialize(NULL);
}
// BookmarkStorage takes ownership of root.
diff --git a/chrome/browser/debugger/debugger_host_impl.cpp b/chrome/browser/debugger/debugger_host_impl.cpp
index c2eb41d..32c13fd 100644
--- a/chrome/browser/debugger/debugger_host_impl.cpp
+++ b/chrome/browser/debugger/debugger_host_impl.cpp
@@ -83,8 +83,8 @@ void DebuggerHostImpl::Debug(TabContents* tab) {
void DebuggerHostImpl::DebugMessage(const std::wstring& msg) {
- Value* msg_value = NULL;
- if (!JSONReader::Read(WideToUTF8(msg), &msg_value, false)) {
+ Value* msg_value = JSONReader::Read(WideToUTF8(msg), false);
+ if (!msg_value) {
msg_value = Value::CreateStringValue(L"Message parse error!");
}
ListValue* argv = new ListValue;
diff --git a/chrome/browser/dom_ui/dom_ui.cc b/chrome/browser/dom_ui/dom_ui.cc
index 4fe9070..e7f2765 100644
--- a/chrome/browser/dom_ui/dom_ui.cc
+++ b/chrome/browser/dom_ui/dom_ui.cc
@@ -31,9 +31,10 @@ void DOMUI::ProcessDOMUIMessage(const std::string& message,
return;
// Convert the content JSON into a Value.
- Value* value = NULL;
+ scoped_ptr<Value> value;
if (!content.empty()) {
- if (!JSONReader::Read(content, &value, false)) {
+ value.reset(JSONReader::Read(content, false));
+ if (!value.get()) {
// The page sent us something that we didn't understand.
// This probably indicates a programming error.
NOTREACHED();
@@ -42,8 +43,7 @@ void DOMUI::ProcessDOMUIMessage(const std::string& message,
}
// Forward this message and content on.
- callback->second->Run(value);
- delete value;
+ callback->second->Run(value.get());
}
void DOMUI::CallJavascriptFunction(const std::wstring& function_name,
diff --git a/chrome/browser/dom_ui/dom_ui_host.cc b/chrome/browser/dom_ui/dom_ui_host.cc
index d9d0333..ff2ca1e 100644
--- a/chrome/browser/dom_ui/dom_ui_host.cc
+++ b/chrome/browser/dom_ui/dom_ui_host.cc
@@ -78,9 +78,10 @@ void DOMUIHost::ProcessDOMUIMessage(const std::string& message,
return;
// Convert the content JSON into a Value.
- Value* value = NULL;
+ scoped_ptr<Value> value;
if (!content.empty()) {
- if (!JSONReader::Read(content, &value, false)) {
+ value.reset(JSONReader::Read(content, false));
+ if (!value.get()) {
// The page sent us something that we didn't understand.
// This probably indicates a programming error.
NOTREACHED();
@@ -89,8 +90,7 @@ void DOMUIHost::ProcessDOMUIMessage(const std::string& message,
}
// Forward this message and content on.
- callback->second->Run(value);
- delete value;
+ callback->second->Run(value.get());
}
WebPreferences DOMUIHost::GetWebkitPrefs() {
diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc
index cd0397c..f38ff04 100644
--- a/chrome/browser/extensions/extensions_service.cc
+++ b/chrome/browser/extensions/extensions_service.cc
@@ -88,15 +88,14 @@ bool ExtensionsServiceBackend::LoadExtensionsFromDirectory(
}
JSONFileValueSerializer serializer(manifest_path.ToWStringHack());
- Value* root = NULL;
std::string error;
- if (!serializer.Deserialize(&root, &error)) {
+ scoped_ptr<Value> root(serializer.Deserialize(&error));
+ if (!root.get()) {
ReportExtensionLoadError(frontend.get(), child_path.ToWStringHack(),
error);
continue;
}
- scoped_ptr<Value> scoped_root(root);
if (!root->IsType(Value::TYPE_DICTIONARY)) {
ReportExtensionLoadError(frontend.get(), child_path.ToWStringHack(),
Extension::kInvalidManifestError);
@@ -104,7 +103,7 @@ bool ExtensionsServiceBackend::LoadExtensionsFromDirectory(
}
scoped_ptr<Extension> extension(new Extension(child_path));
- if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root),
+ if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root.get()),
&error)) {
ReportExtensionLoadError(frontend.get(), child_path.ToWStringHack(),
error);
diff --git a/chrome/browser/page_state.cc b/chrome/browser/page_state.cc
index f47a4cf..1d52ffb 100644
--- a/chrome/browser/page_state.cc
+++ b/chrome/browser/page_state.cc
@@ -40,16 +40,15 @@ void PageState::InitWithBytes(const std::string& bytes) {
state_.reset(new DictionaryValue);
JSONStringValueSerializer serializer(bytes);
- Value* root = NULL;
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
- if (!serializer.Deserialize(&root, NULL))
+ if (!root.get()) {
NOTREACHED();
-
- if (root != NULL && root->GetType() == Value::TYPE_DICTIONARY) {
- state_.reset(static_cast<DictionaryValue*>(root));
- } else if (root) {
- delete root;
+ return;
}
+
+ if (root->GetType() == Value::TYPE_DICTIONARY)
+ state_.reset(static_cast<DictionaryValue*>(root.release()));
}
void PageState::GetByteRepresentation(std::string* out) const {
diff --git a/chrome/common/json_value_serializer.cc b/chrome/common/json_value_serializer.cc
index 82938ec..54afe7a 100644
--- a/chrome/common/json_value_serializer.cc
+++ b/chrome/common/json_value_serializer.cc
@@ -21,13 +21,11 @@ bool JSONStringValueSerializer::Serialize(const Value& root) {
return true;
}
-bool JSONStringValueSerializer::Deserialize(Value** root,
- std::string* error_message) {
+Value* JSONStringValueSerializer::Deserialize(std::string* error_message) {
if (!json_string_)
return false;
- return JSONReader::ReadAndReturnError(*json_string_, root,
- allow_trailing_comma_,
+ return JSONReader::ReadAndReturnError(*json_string_, allow_trailing_comma_,
error_message);
}
@@ -50,13 +48,12 @@ bool JSONFileValueSerializer::Serialize(const Value& root) {
return true;
}
-bool JSONFileValueSerializer::Deserialize(Value** root,
- std::string* error_message) {
+Value* JSONFileValueSerializer::Deserialize(std::string* error_message) {
std::string json_string;
if (!file_util::ReadFileToString(json_file_path_, &json_string)) {
return false;
}
JSONStringValueSerializer serializer(json_string);
- return serializer.Deserialize(root, error_message);
+ return serializer.Deserialize(error_message);
}
diff --git a/chrome/common/json_value_serializer.h b/chrome/common/json_value_serializer.h
index 2e581ee..7a51528 100644
--- a/chrome/common/json_value_serializer.h
+++ b/chrome/common/json_value_serializer.h
@@ -39,12 +39,9 @@ class JSONStringValueSerializer : public ValueSerializer {
// Attempt to deserialize the data structure encoded in the string passed
// in to the constructor into a structure of Value objects. If the return
- // value is true, the |root| parameter will be set to point to a new Value
- // object that corresponds to the values represented in the string. The
- // caller takes ownership of the returned Value objects. Otherwise, the root
- // value will be changed and if |error_message| is non-null, it will contain
+ // value is NULL and |error_message| is non-null, |error-message| will contain
// a string describing the error.
- bool Deserialize(Value** root, std::string* error_message);
+ Value* Deserialize(std::string* error_message);
void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
bool pretty_print() { return pretty_print_; }
@@ -86,12 +83,9 @@ class JSONFileValueSerializer : public ValueSerializer {
// Attempt to deserialize the data structure encoded in the file passed
// in to the constructor into a structure of Value objects. If the return
- // value is true, the |root| parameter will be set to point to a new Value
- // object that corresponds to the values represented in the file. The
- // caller takes ownership of the returned Value objects. Otherwise, the root
- // value will be changed and if |error_message| is non-null, it will contain
- // a string describing the error.
- bool Deserialize(Value** root, std::string* error_message);
+ // value is NULL, and if |error_message| is non-null, |error_message| will
+ // contain a string describing the error.
+ Value* Deserialize(std::string* error_message);
private:
std::wstring json_file_path_;
diff --git a/chrome/common/json_value_serializer_perftest.cc b/chrome/common/json_value_serializer_perftest.cc
index 6570d34..bef8fbe 100644
--- a/chrome/common/json_value_serializer_perftest.cc
+++ b/chrome/common/json_value_serializer_perftest.cc
@@ -52,10 +52,9 @@ TEST_F(JSONValueSerializerTests, Reading) {
PerfTimeLogger chrome_timer("chrome");
for (int i = 0; i < kIterations; ++i) {
for (size_t j = 0; j < test_cases_.size(); ++j) {
- Value* root = NULL;
JSONStringValueSerializer reader(test_cases_[j]);
- ASSERT_TRUE(reader.Deserialize(&root, NULL));
- delete root;
+ scoped_ptr<Value> root(reader.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
}
}
chrome_timer.Done();
@@ -67,9 +66,9 @@ TEST_F(JSONValueSerializerTests, CompactWriting) {
// Convert test cases to Value objects.
std::vector<Value*> test_cases;
for (size_t i = 0; i < test_cases_.size(); ++i) {
- Value* root = NULL;
JSONStringValueSerializer reader(test_cases_[i]);
- ASSERT_TRUE(reader.Deserialize(&root, NULL));
+ Value* root = reader.Deserialize(NULL);
+ ASSERT_TRUE(root);
test_cases.push_back(root);
}
diff --git a/chrome/common/json_value_serializer_unittest.cc b/chrome/common/json_value_serializer_unittest.cc
index 3dd175b..30f6320 100644
--- a/chrome/common/json_value_serializer_unittest.cc
+++ b/chrome/common/json_value_serializer_unittest.cc
@@ -16,13 +16,12 @@
TEST(JSONValueSerializerTest, Roundtrip) {
const std::string original_serialization =
"{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}";
- Value* root = NULL;
JSONStringValueSerializer serializer(original_serialization);
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(root);
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
Value* null_value = NULL;
ASSERT_TRUE(root_dict->Get(L"null", &null_value));
@@ -61,8 +60,6 @@ TEST(JSONValueSerializerTest, Roundtrip) {
" \"real\": 3.14\r\n"
"}\r\n";
ASSERT_EQ(pretty_serialization, test_serialization);
-
- delete root;
}
TEST(JSONValueSerializerTest, StringEscape) {
@@ -119,14 +116,14 @@ TEST(JSONValueSerializerTest, UnicodeStrings) {
ASSERT_EQ(expected, actual);
// escaped ascii text -> json
- Value* deserial_root = NULL;
JSONStringValueSerializer deserializer(expected);
- ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
- DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
+ scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ DictionaryValue* dict_root =
+ static_cast<DictionaryValue*>(deserial_root.get());
std::wstring web_value;
ASSERT_TRUE(dict_root->GetString(L"web", &web_value));
ASSERT_EQ(test, web_value);
- delete deserial_root;
}
TEST(JSONValueSerializerTest, HexStrings) {
@@ -143,57 +140,53 @@ TEST(JSONValueSerializerTest, HexStrings) {
ASSERT_EQ(expected, actual);
// escaped ascii text -> json
- Value* deserial_root = NULL;
JSONStringValueSerializer deserializer(expected);
- ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
- DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
+ scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ DictionaryValue* dict_root =
+ static_cast<DictionaryValue*>(deserial_root.get());
std::wstring test_value;
ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
ASSERT_EQ(test, test_value);
- delete deserial_root;
// Test converting escaped regular chars
- deserial_root = NULL;
std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}";
JSONStringValueSerializer deserializer2(escaped_chars);
- ASSERT_TRUE(deserializer2.Deserialize(&deserial_root, NULL));
- dict_root = static_cast<DictionaryValue*>(deserial_root);
+ deserial_root.reset(deserializer2.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ dict_root = static_cast<DictionaryValue*>(deserial_root.get());
ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
ASSERT_EQ(std::wstring(L"go"), test_value);
- delete deserial_root;
}
TEST(JSONValueSerializerTest, AllowTrailingComma) {
- Value* root = NULL;
- Value* root_expected = NULL;
+ scoped_ptr<Value> root;
+ scoped_ptr<Value> root_expected;
std::string test_with_commas("{\"key\": [true,],}");
std::string test_no_commas("{\"key\": [true]}");
JSONStringValueSerializer serializer(test_with_commas);
serializer.set_allow_trailing_comma(true);
JSONStringValueSerializer serializer_expected(test_no_commas);
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(serializer_expected.Deserialize(&root_expected, NULL));
- ASSERT_TRUE(root->Equals(root_expected));
-
- delete root;
- delete root_expected;
+ root.reset(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
+ root_expected.reset(serializer_expected.Deserialize(NULL));
+ ASSERT_TRUE(root_expected.get());
+ ASSERT_TRUE(root->Equals(root_expected.get()));
}
namespace {
void ValidateJsonList(const std::string& json) {
- Value* root = NULL;
- ASSERT_TRUE(JSONReader::Read(json, &root, false));
- ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
- ListValue* list = static_cast<ListValue*>(root);
+ scoped_ptr<Value> root(JSONReader::Read(json, false));
+ ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
+ ListValue* list = static_cast<ListValue*>(root.get());
ASSERT_EQ(1U, list->GetSize());
Value* elt = NULL;
ASSERT_TRUE(list->Get(0, &elt));
int value = 0;
ASSERT_TRUE(elt && elt->GetAsInteger(&value));
ASSERT_EQ(1, value);
- delete root;
}
} // namespace
@@ -206,25 +199,26 @@ TEST(JSONValueSerializerTest, JSONReaderComments) {
ValidateJsonList("[ 1 /* one */ ] /* end */");
ValidateJsonList("[ 1 //// ,2\r\n ]");
- Value* root = NULL;
+ scoped_ptr<Value> root;
+
// It's ok to have a comment in a string.
- ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false));
- ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
- ListValue* list = static_cast<ListValue*>(root);
+ root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]", false));
+ ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
+ ListValue* list = static_cast<ListValue*>(root.get());
ASSERT_EQ(1U, list->GetSize());
Value* elt = NULL;
ASSERT_TRUE(list->Get(0, &elt));
std::wstring value;
ASSERT_TRUE(elt && elt->GetAsString(&value));
ASSERT_EQ(L"// ok\n /* foo */ ", value);
- delete root;
- root = NULL;
// You can't nest comments.
- ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false));
+ root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false));
+ ASSERT_FALSE(root.get());
// Not a open comment token.
- ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false));
+ root.reset(JSONReader::Read("/ * * / [1]", false));
+ ASSERT_FALSE(root.get());
}
namespace {
@@ -261,13 +255,13 @@ TEST_F(JSONFileValueSerializerTest, Roundtrip) {
ASSERT_TRUE(file_util::PathExists(original_file_path));
JSONFileValueSerializer deserializer(original_file_path);
- Value* root;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL));
- ASSERT_TRUE(root);
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
Value* null_value = NULL;
ASSERT_TRUE(root_dict->Get(L"null", &null_value));
@@ -298,8 +292,6 @@ TEST_F(JSONFileValueSerializerTest, Roundtrip) {
// Now compare file contents.
EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
EXPECT_TRUE(file_util::Delete(written_file_path, false));
-
- delete root;
}
TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
@@ -311,8 +303,9 @@ TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
ASSERT_TRUE(file_util::PathExists(original_file_path));
JSONFileValueSerializer deserializer(original_file_path);
- Value* root;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
// Now try writing.
std::wstring written_file_path = test_dir_;
@@ -326,8 +319,6 @@ TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
// Now compare file contents.
EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
EXPECT_TRUE(file_util::Delete(written_file_path, false));
-
- delete root;
}
TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
@@ -337,9 +328,8 @@ TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
L"serializer_test_nowhitespace.js");
ASSERT_TRUE(file_util::PathExists(source_file_path));
JSONFileValueSerializer serializer(source_file_path);
- Value* root;
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(root);
- delete root;
+ scoped_ptr<Value> root;
+ root.reset(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
}
#endif // defined(OS_WIN)
diff --git a/chrome/common/pref_service.cc b/chrome/common/pref_service.cc
index 8f3322a..870e79e 100644
--- a/chrome/common/pref_service.cc
+++ b/chrome/common/pref_service.cc
@@ -136,38 +136,34 @@ bool PrefService::LoadPersistentPrefs(const std::wstring& file_path) {
DCHECK(CalledOnValidThread());
JSONFileValueSerializer serializer(file_path);
- Value* root = NULL;
- if (serializer.Deserialize(&root, NULL)) {
- // Preferences should always have a dictionary root.
- if (!root->IsType(Value::TYPE_DICTIONARY)) {
- delete root;
- return false;
- }
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ if (!root.get())
+ return false;
- persistent_.reset(static_cast<DictionaryValue*>(root));
- return true;
- }
+ // Preferences should always have a dictionary root.
+ if (!root->IsType(Value::TYPE_DICTIONARY))
+ return false;
- return false;
+ persistent_.reset(static_cast<DictionaryValue*>(root.release()));
+ return true;
}
void PrefService::ReloadPersistentPrefs() {
DCHECK(CalledOnValidThread());
JSONFileValueSerializer serializer(pref_filename_);
- Value* root;
- if (serializer.Deserialize(&root, NULL)) {
- // Preferences should always have a dictionary root.
- if (!root->IsType(Value::TYPE_DICTIONARY)) {
- delete root;
- return;
- }
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ if (!root.get())
+ return;
- persistent_.reset(static_cast<DictionaryValue*>(root));
- for (PreferenceSet::iterator it = prefs_.begin();
- it != prefs_.end(); ++it) {
- (*it)->root_pref_ = persistent_.get();
- }
+ // Preferences should always have a dictionary root.
+ if (!root->IsType(Value::TYPE_DICTIONARY))
+ return;
+
+ persistent_.reset(static_cast<DictionaryValue*>(root.release()));
+ for (PreferenceSet::iterator it = prefs_.begin();
+ it != prefs_.end(); ++it) {
+ (*it)->root_pref_ = persistent_.get();
}
}
diff --git a/chrome/common/pref_service_uitest.cc b/chrome/common/pref_service_uitest.cc
index b9f0774..0ecd114 100644
--- a/chrome/common/pref_service_uitest.cc
+++ b/chrome/common/pref_service_uitest.cc
@@ -82,13 +82,12 @@ TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) {
ASSERT_TRUE(file_util::PathExists(tmp_pref_file_));
JSONFileValueSerializer deserializer(tmp_pref_file_);
- Value* root = NULL;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root(deserializer.Deserialize(NULL));
- ASSERT_TRUE(root);
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
// Retrieve the screen rect for the launched window
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
@@ -130,6 +129,5 @@ TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) {
ASSERT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + L".maximized",
&is_maximized));
ASSERT_EQ(is_maximized, is_window_maximized);
- delete root;
}
diff --git a/chrome/common/pref_service_unittest.cc b/chrome/common/pref_service_unittest.cc
index 2baa908..e35e44a 100644
--- a/chrome/common/pref_service_unittest.cc
+++ b/chrome/common/pref_service_unittest.cc
@@ -155,14 +155,16 @@ TEST_F(PrefServiceTest, Overlay) {
Value* transient_value;
{
JSONStringValueSerializer serializer(transient);
- ASSERT_TRUE(serializer.Deserialize(&transient_value, NULL));
+ transient_value = serializer.Deserialize(NULL);
+ ASSERT_TRUE(transient_value);
}
prefs.transient()->Set(transient_string, transient_value);
Value* both_transient_value;
{
JSONStringValueSerializer serializer(transient);
- ASSERT_TRUE(serializer.Deserialize(&both_transient_value, NULL));
+ both_transient_value = serializer.Deserialize(NULL);
+ ASSERT_TRUE(both_transient_value);
}
prefs.transient()->Set(L"both", both_transient_value);
diff --git a/chrome/installer/util/master_preferences.cc b/chrome/installer/util/master_preferences.cc
index 34d10a9..21c74dd 100644
--- a/chrome/installer/util/master_preferences.cc
+++ b/chrome/installer/util/master_preferences.cc
@@ -12,14 +12,13 @@ namespace {
DictionaryValue* ReadJSONPrefs(const std::string& data) {
JSONStringValueSerializer json(data);
- Value* root;
- if (!json.Deserialize(&root, NULL))
+ scoped_ptr<Value> root(json.Deserialize(NULL));
+ if (!root.get())
return NULL;
- if (!root->IsType(Value::TYPE_DICTIONARY)) {
- delete root;
+ if (!root->IsType(Value::TYPE_DICTIONARY))
return NULL;
- }
- return static_cast<DictionaryValue*>(root);
+
+ return static_cast<DictionaryValue*>(root.release());
}
bool GetBooleanPref(const DictionaryValue* prefs, const std::wstring& name) {
diff --git a/chrome/test/automation/tab_proxy.cc b/chrome/test/automation/tab_proxy.cc
index f17a35de..07a3488 100644
--- a/chrome/test/automation/tab_proxy.cc
+++ b/chrome/test/automation/tab_proxy.cc
@@ -555,10 +555,10 @@ bool TabProxy::ExecuteAndExtractValue(const std::wstring& frame_xpath,
json.append("]");
JSONStringValueSerializer deserializer(json);
- succeeded = deserializer.Deserialize(value, NULL);
+ *value = deserializer.Deserialize(NULL);
delete response;
- return succeeded;
+ return *value != NULL;
}
bool TabProxy::GetConstrainedWindowCount(int* count) const {
diff --git a/chrome/test/ui/ui_test.cc b/chrome/test/ui/ui_test.cc
index 4d58e6a..e1bea1b 100644
--- a/chrome/test/ui/ui_test.cc
+++ b/chrome/test/ui/ui_test.cc
@@ -497,13 +497,11 @@ static DictionaryValue* LoadDictionaryValueFromPath(const std::wstring& path) {
return NULL;
JSONFileValueSerializer serializer(path);
- Value* root_value = NULL;
- if (serializer.Deserialize(&root_value, NULL) &&
- root_value->GetType() != Value::TYPE_DICTIONARY) {
- delete root_value;
+ scoped_ptr<Value> root_value(serializer.Deserialize(NULL));
+ if (!root_value.get() || root_value->GetType() != Value::TYPE_DICTIONARY)
return NULL;
- }
- return static_cast<DictionaryValue*>(root_value);
+
+ return static_cast<DictionaryValue*>(root_value.release());
}
DictionaryValue* UITest::GetLocalState() {