Commit 69505dec authored by estade's avatar estade Committed by Commit bot

Change JSONReader::ReadToValue to return a scoped_ptr

BUG=none

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

Cr-Commit-Position: refs/heads/master@{#330823}
parent 678b1e5a
......@@ -6,6 +6,7 @@
#include "base/json/json_parser.h"
#include "base/logging.h"
#include "base/values.h"
namespace base {
......@@ -99,8 +100,8 @@ std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
}
}
Value* JSONReader::ReadToValue(const std::string& json) {
return parser_->Parse(json);
scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) {
return make_scoped_ptr(parser_->Parse(json));
}
JSONReader::JsonParseError JSONReader::error_code() const {
......
......@@ -115,7 +115,7 @@ class BASE_EXPORT JSONReader {
static std::string ErrorCodeToString(JsonParseError error_code);
// Parses an input string into a Value that is owned by the caller.
Value* ReadToValue(const std::string& json);
scoped_ptr<Value> ReadToValue(const std::string& json);
// Returns the error code if the last call to ReadToValue() failed.
// Returns JSON_NO_ERROR otherwise.
......
This diff is collapsed.
......@@ -303,10 +303,7 @@ void PrivetURLFetcher::OnURLFetchCompleteParseData(
}
base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
scoped_ptr<base::Value> value;
value.reset(json_reader.ReadToValue(response_str));
scoped_ptr<base::Value> value = json_reader.ReadToValue(response_str);
if (!value) {
delegate_->OnError(this, JSON_PARSE_ERROR);
return;
......
......@@ -16,8 +16,8 @@ MobileDevice::~MobileDevice() {}
Status FindMobileDevice(std::string device_name,
scoped_ptr<MobileDevice>* mobile_device) {
base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
scoped_ptr<base::Value> devices_value;
devices_value.reset(json_reader.ReadToValue(kMobileDevices));
scoped_ptr<base::Value> devices_value =
json_reader.ReadToValue(kMobileDevices);
if (!devices_value.get())
return Status(kUnknownError,
"could not parse mobile device list because " +
......
......@@ -24,9 +24,8 @@ NetworkConditions::~NetworkConditions() {}
Status FindPresetNetwork(std::string network_name,
NetworkConditions* network_conditions) {
base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
scoped_ptr<base::Value> networks_value;
networks_value.reset(json_reader.ReadToValue(kNetworks));
if (!networks_value.get())
scoped_ptr<base::Value> networks_value = json_reader.ReadToValue(kNetworks);
if (!networks_value)
return Status(kUnknownError,
"could not parse network list because " +
json_reader.GetErrorMessage());
......@@ -38,7 +37,6 @@ Status FindPresetNetwork(std::string network_name,
for (base::ListValue::iterator it = networks->begin();
it != networks->end();
++it) {
base::DictionaryValue* network = NULL;
if (!(*it)->GetAsDictionary(&network)) {
return Status(kUnknownError,
......
......@@ -24,12 +24,11 @@ typedef content::TestMessageHandler::MessageResponse MessageResponse;
MessageResponse StructuredMessageHandler::HandleMessage(
const std::string& json) {
scoped_ptr<base::Value> value;
base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
// Automation messages are stringified before they are sent because the
// automation channel cannot handle arbitrary objects. This means we
// need to decode the json twice to get the original message.
value.reset(reader.ReadToValue(json));
scoped_ptr<base::Value> value = reader.ReadToValue(json);
if (!value.get())
return InternalError("Could parse automation JSON: " + json +
" because " + reader.GetErrorMessage());
......@@ -38,7 +37,7 @@ MessageResponse StructuredMessageHandler::HandleMessage(
if (!value->GetAsString(&temp))
return InternalError("Message was not a string: " + json);
value.reset(reader.ReadToValue(temp));
value = reader.ReadToValue(temp);
if (!value.get())
return InternalError("Could not parse message JSON: " + temp +
" because " + reader.GetErrorMessage());
......
......@@ -150,8 +150,8 @@ bool ExecuteScriptHelper(RenderFrameHost* render_frame_host,
return true;
base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
result->reset(reader.ReadToValue(json));
if (!result->get()) {
*result = reader.ReadToValue(json);
if (!*result) {
DLOG(ERROR) << reader.GetErrorMessage();
return false;
}
......
......@@ -146,12 +146,13 @@ ValueStore::ReadResult LeveldbValueStore::Get() {
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
std::string key = it->key().ToString();
base::Value* value = json_reader.ReadToValue(it->value().ToString());
scoped_ptr<base::Value> value =
json_reader.ReadToValue(it->value().ToString());
if (!value) {
return MakeReadResult(
Error::Create(CORRUPTION, kInvalidJson, util::NewKey(key)));
}
settings->SetWithoutPathExpansion(key, value);
settings->SetWithoutPathExpansion(key, value.Pass());
}
if (it->status().IsNotFound()) {
......@@ -357,11 +358,11 @@ scoped_ptr<ValueStore::Error> LeveldbValueStore::ReadFromDb(
if (!s.ok())
return ToValueStoreError(s, util::NewKey(key));
base::Value* value = base::JSONReader().ReadToValue(value_as_json);
scoped_ptr<base::Value> value = base::JSONReader().ReadToValue(value_as_json);
if (!value)
return Error::Create(CORRUPTION, kInvalidJson, util::NewKey(key));
setting->reset(value);
*setting = value.Pass();
return util::NoError();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment