Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1018)

Unified Diff: base/json/json_parser.cc

Issue 1927753002: Convert callers of base::DeepCopy() to base::CreateDeepCopy() in //base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/json/json_parser.h ('k') | base/json/json_reader.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_parser.cc
diff --git a/base/json/json_parser.cc b/base/json/json_parser.cc
index 801c84a24cad7d344e6266cd2ccf309b4c976c06..40d358b017901bd873c94f6389ad26429a710318 100644
--- a/base/json/json_parser.cc
+++ b/base/json/json_parser.cc
@@ -5,10 +5,11 @@
#include "base/json/json_parser.h"
#include <cmath>
-#include <memory>
+#include <utility>
#include "base/logging.h"
#include "base/macros.h"
+#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
@@ -34,7 +35,8 @@ const int32_t kExtendedASCIIStart = 0x80;
// the new instance.
class DictionaryHiddenRootValue : public DictionaryValue {
public:
- DictionaryHiddenRootValue(std::string* json, Value* root) : json_(json) {
+ DictionaryHiddenRootValue(std::string json, Value* root)
+ : json_(std::move(json)) {
DCHECK(root->IsType(Value::TYPE_DICTIONARY));
DictionaryValue::Swap(static_cast<DictionaryValue*>(root));
}
@@ -44,13 +46,13 @@ class DictionaryHiddenRootValue : public DictionaryValue {
// First deep copy to convert JSONStringValue to std::string and swap that
// copy with |other|, which contains the new contents of |this|.
- std::unique_ptr<DictionaryValue> copy(DeepCopy());
+ std::unique_ptr<DictionaryValue> copy(CreateDeepCopy());
copy->Swap(other);
// Then erase the contents of the current dictionary and swap in the
// new contents, originally from |other|.
Clear();
- json_.reset();
+ json_.clear();
DictionaryValue::Swap(copy.get());
}
@@ -71,20 +73,20 @@ class DictionaryHiddenRootValue : public DictionaryValue {
if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned))
return false;
- out->reset(out_owned->DeepCopy());
+ *out = out_owned->CreateDeepCopy();
return true;
}
private:
- std::unique_ptr<std::string> json_;
+ std::string json_;
DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue);
};
class ListHiddenRootValue : public ListValue {
public:
- ListHiddenRootValue(std::string* json, Value* root) : json_(json) {
+ ListHiddenRootValue(std::string json, Value* root) : json_(std::move(json)) {
dcheng 2016/04/27 22:12:11 How do we feel about this? Or do we prefer std::un
jbroman 2016/04/27 22:19:39 I prefer std::string to std::unique_ptr<std::strin
jbroman 2016/04/27 22:25:24 Having now read more of the surrounding code, I th
dcheng 2016/04/27 22:38:27 I don't think start_pos_ is used after parsing. Ev
DCHECK(root->IsType(Value::TYPE_LIST));
ListValue::Swap(static_cast<ListValue*>(root));
}
@@ -94,13 +96,13 @@ class ListHiddenRootValue : public ListValue {
// First deep copy to convert JSONStringValue to std::string and swap that
// copy with |other|, which contains the new contents of |this|.
- std::unique_ptr<ListValue> copy(DeepCopy());
+ std::unique_ptr<ListValue> copy(CreateDeepCopy());
copy->Swap(other);
// Then erase the contents of the current list and swap in the new contents,
// originally from |other|.
Clear();
- json_.reset();
+ json_.clear();
ListValue::Swap(copy.get());
}
@@ -117,13 +119,13 @@ class ListHiddenRootValue : public ListValue {
if (!ListValue::Remove(index, &out_owned))
return false;
- out->reset(out_owned->DeepCopy());
+ *out = out_owned->CreateDeepCopy();
return true;
}
private:
- std::unique_ptr<std::string> json_;
+ std::string json_;
DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue);
};
@@ -147,9 +149,11 @@ class JSONStringValue : public Value {
*out_value = UTF8ToUTF16(string_piece_);
return true;
}
+#if 0
jbroman 2016/04/27 22:19:39 remove before landing?
dcheng 2016/04/27 22:23:05 Should already be gone in the latest PS.
Value* DeepCopy() const override {
return new StringValue(string_piece_.as_string());
}
+#endif
bool Equals(const Value* other) const override {
std::string other_string;
return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) &&
@@ -203,14 +207,14 @@ JSONParser::JSONParser(int options)
JSONParser::~JSONParser() {
}
-Value* JSONParser::Parse(const StringPiece& input) {
- std::unique_ptr<std::string> input_copy;
+std::unique_ptr<Value> JSONParser::Parse(StringPiece input) {
+ std::string input_copy;
// If the children of a JSON root can be detached, then hidden roots cannot
// be used, so do not bother copying the input because StringPiece will not
// be used anywhere.
if (!(options_ & JSON_DETACHABLE_CHILDREN)) {
- input_copy.reset(new std::string(input.as_string()));
- start_pos_ = input_copy->data();
+ input_copy = input.as_string();
+ start_pos_ = input_copy.data();
} else {
start_pos_ = input.data();
}
@@ -251,19 +255,21 @@ Value* JSONParser::Parse(const StringPiece& input) {
// hidden root.
if (!(options_ & JSON_DETACHABLE_CHILDREN)) {
if (root->IsType(Value::TYPE_DICTIONARY)) {
- return new DictionaryHiddenRootValue(input_copy.release(), root.get());
+ return WrapUnique(
+ new DictionaryHiddenRootValue(std::move(input_copy), root.get()));
} else if (root->IsType(Value::TYPE_LIST)) {
- return new ListHiddenRootValue(input_copy.release(), root.get());
+ return WrapUnique(
+ new ListHiddenRootValue(std::move(input_copy), root.get()));
} else if (root->IsType(Value::TYPE_STRING)) {
// A string type could be a JSONStringValue, but because there's no
// corresponding HiddenRootValue, the memory will be lost. Deep copy to
// preserve it.
- return root->DeepCopy();
+ return root->CreateDeepCopy();
}
}
// All other values can be returned directly.
- return root.release();
+ return root;
}
JSONReader::JsonParseError JSONParser::error_code() const {
« no previous file with comments | « base/json/json_parser.h ('k') | base/json/json_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698