Skip to content

Commit

Permalink
Use scoped_ptrs in JSONReader::Read functions.
Browse files Browse the repository at this point in the history
There are many callers, so all could not be updated at once. The old version is renamed to JSONReader::DeprecatedRead, and a new version of JSONReader::Read that returns scoped_ptr takes its place.

Much of this patch was generated with sed. Some callsites of the form

  scoped_ptr<Value> value(Read());

have been updated to

  scoped_ptr<Value> value = Read();

but most Read() calls are simply converted to DeprecatedRead. Actually updating them is a TODO.

BUG=none

Review URL: https://codereview.chromium.org/1136643005

Cr-Commit-Position: refs/heads/master@{#331120}
  • Loading branch information
estade authored and Commit bot committed May 22, 2015
1 parent 2d381b0 commit b09312b
Show file tree
Hide file tree
Showing 196 changed files with 494 additions and 471 deletions.
54 changes: 26 additions & 28 deletions base/json/json_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ TEST_F(JSONParserTest, ErrorMessages) {
std::string error_message;
int error_code = 0;
scoped_ptr<Value> root;
root.reset(JSONReader::ReadAndReturnError("[42]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[42]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_TRUE(error_message.empty());
EXPECT_EQ(0, error_code);

// Test line and column counting
const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
// error here ----------------------------------^
root.reset(JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
big_json, JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
Expand All @@ -225,16 +225,16 @@ TEST_F(JSONParserTest, ErrorMessages) {
const char big_json_crlf[] =
"[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
// error here ----------------------^
root.reset(JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
big_json_crlf, JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);

// Test each of the error conditions
root.reset(JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"{},{}", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
JSONReader::kUnexpectedDataAfterRoot), error_message);
Expand All @@ -245,57 +245,56 @@ TEST_F(JSONParserTest, ErrorMessages) {
nested_json.insert(nested_json.begin(), '[');
nested_json.append(1, ']');
}
root.reset(JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
nested_json, JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 100, JSONReader::kTooMuchNesting),
error_message);
EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);

root.reset(JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[1,]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
error_message);
EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);

root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"{foo:\"bar\"}", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
JSONReader::kUnquotedDictionaryKey), error_message);
EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);

root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}",
JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"{\"foo\":\"bar\",}", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
error_message);

root.reset(JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[nu]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);

root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[\"xxx\\xq\"]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);

root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[\"xxx\\uq\"]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);

root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC,
&error_code, &error_message));
root.reset(JSONReader::DeprecatedReadAndReturnError(
"[\"xxx\\q\"]", JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
Expand All @@ -309,9 +308,8 @@ TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
"[\"😇\",[],[],[],{\"google:suggesttype\":[]}]";
std::string error_message;
int error_code = 0;
scoped_ptr<Value> root(
JSONReader::ReadAndReturnError(kUtf8Data, JSON_PARSE_RFC, &error_code,
&error_message));
scoped_ptr<Value> root(JSONReader::DeprecatedReadAndReturnError(
kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message));
EXPECT_TRUE(root.get()) << error_message;
}

Expand Down
53 changes: 35 additions & 18 deletions base/json/json_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,51 @@ JSONReader::~JSONReader() {
}

// static
Value* JSONReader::Read(const StringPiece& json) {
Value* JSONReader::DeprecatedRead(const StringPiece& json) {
return Read(json).release();
}

// static
scoped_ptr<Value> JSONReader::Read(const StringPiece& json) {
internal::JSONParser parser(JSON_PARSE_RFC);
return parser.Parse(json);
return make_scoped_ptr(parser.Parse(json));
}

// static
Value* JSONReader::Read(const StringPiece& json,
int options) {
internal::JSONParser parser(options);
return parser.Parse(json);
Value* JSONReader::DeprecatedRead(const StringPiece& json, int options) {
return Read(json, options).release();
}

// static
Value* JSONReader::ReadAndReturnError(const StringPiece& json,
int options,
int* error_code_out,
std::string* error_msg_out) {
scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) {
internal::JSONParser parser(options);
Value* root = parser.Parse(json);
if (root)
return root;
return make_scoped_ptr(parser.Parse(json));
}

if (error_code_out)
*error_code_out = parser.error_code();
if (error_msg_out)
*error_msg_out = parser.GetErrorMessage();
// static
Value* JSONReader::DeprecatedReadAndReturnError(const StringPiece& json,
int options,
int* error_code_out,
std::string* error_msg_out) {
return ReadAndReturnError(json, options, error_code_out, error_msg_out)
.release();
}

// static
scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json,
int options,
int* error_code_out,
std::string* error_msg_out) {
internal::JSONParser parser(options);
scoped_ptr<Value> root(parser.Parse(json));
if (!root) {
if (error_code_out)
*error_code_out = parser.error_code();
if (error_msg_out)
*error_msg_out = parser.GetErrorMessage();
}

return NULL;
return root;
}

// static
Expand Down
21 changes: 15 additions & 6 deletions base/json/json_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,30 @@ class BASE_EXPORT JSONReader {

// Reads and parses |json|, returning a Value. The caller owns the returned
// instance. If |json| is not a properly formed JSON string, returns NULL.
static Value* Read(const StringPiece& json);
static scoped_ptr<Value> Read(const StringPiece& json);
// TODO(estade): remove this bare pointer version.
static Value* DeprecatedRead(const StringPiece& json);

// Reads and parses |json|, returning a Value owned by the caller. The
// parser respects the given |options|. If the input is not properly formed,
// returns NULL.
static Value* Read(const StringPiece& json, int options);
static scoped_ptr<Value> Read(const StringPiece& json, int options);
// TODO(estade): remove this bare pointer version.
static Value* DeprecatedRead(const StringPiece& json, int options);

// Reads and parses |json| like Read(). |error_code_out| and |error_msg_out|
// are optional. If specified and NULL is returned, they will be populated
// an error code and a formatted error message (including error location if
// appropriate). Otherwise, they will be unmodified.
static Value* ReadAndReturnError(const StringPiece& json,
int options, // JSONParserOptions
int* error_code_out,
std::string* error_msg_out);
static scoped_ptr<Value> ReadAndReturnError(const StringPiece& json,
int options, // JSONParserOptions
int* error_code_out,
std::string* error_msg_out);
// TODO(estade): remove this bare pointer version.
static Value* DeprecatedReadAndReturnError(const StringPiece& json,
int options, // JSONParserOptions
int* error_code_out,
std::string* error_msg_out);

// Converts a JSON parse error code into a human readable message.
// Returns an empty string if error_code is JSON_NO_ERROR.
Expand Down
Loading

0 comments on commit b09312b

Please sign in to comment.