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

Side by Side Diff: base/json/json_parser.cc

Issue 2539363004: Make base::Value::TYPE a scoped enum. (Closed)
Patch Set: Rebase Created 4 years 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/json/json_parser.h" 5 #include "base/json/json_parser.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 21 matching lines...) Expand all
32 // with JSONStringValue as an optimization for reducing the number of string 32 // with JSONStringValue as an optimization for reducing the number of string
33 // copies. When this optimization is active, the parser uses a hidden root to 33 // copies. When this optimization is active, the parser uses a hidden root to
34 // keep the original JSON input string live and creates JSONStringValue children 34 // keep the original JSON input string live and creates JSONStringValue children
35 // holding StringPiece references to the input string, avoiding about 2/3rds of 35 // holding StringPiece references to the input string, avoiding about 2/3rds of
36 // string memory copies. The real root value is Swap()ed into the new instance. 36 // string memory copies. The real root value is Swap()ed into the new instance.
37 class DictionaryHiddenRootValue : public DictionaryValue { 37 class DictionaryHiddenRootValue : public DictionaryValue {
38 public: 38 public:
39 DictionaryHiddenRootValue(std::unique_ptr<std::string> json, 39 DictionaryHiddenRootValue(std::unique_ptr<std::string> json,
40 std::unique_ptr<Value> root) 40 std::unique_ptr<Value> root)
41 : json_(std::move(json)) { 41 : json_(std::move(json)) {
42 DCHECK(root->IsType(Value::TYPE_DICTIONARY)); 42 DCHECK(root->IsType(Value::Type::DICTIONARY));
43 DictionaryValue::Swap(static_cast<DictionaryValue*>(root.get())); 43 DictionaryValue::Swap(static_cast<DictionaryValue*>(root.get()));
44 } 44 }
45 45
46 void Swap(DictionaryValue* other) override { 46 void Swap(DictionaryValue* other) override {
47 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently."; 47 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently.";
48 48
49 // First deep copy to convert JSONStringValue to std::string and swap that 49 // First deep copy to convert JSONStringValue to std::string and swap that
50 // copy with |other|, which contains the new contents of |this|. 50 // copy with |other|, which contains the new contents of |this|.
51 std::unique_ptr<DictionaryValue> copy(CreateDeepCopy()); 51 std::unique_ptr<DictionaryValue> copy(CreateDeepCopy());
52 copy->Swap(other); 52 copy->Swap(other);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 std::unique_ptr<std::string> json_; 84 std::unique_ptr<std::string> json_;
85 85
86 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue); 86 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue);
87 }; 87 };
88 88
89 class ListHiddenRootValue : public ListValue { 89 class ListHiddenRootValue : public ListValue {
90 public: 90 public:
91 ListHiddenRootValue(std::unique_ptr<std::string> json, 91 ListHiddenRootValue(std::unique_ptr<std::string> json,
92 std::unique_ptr<Value> root) 92 std::unique_ptr<Value> root)
93 : json_(std::move(json)) { 93 : json_(std::move(json)) {
94 DCHECK(root->IsType(Value::TYPE_LIST)); 94 DCHECK(root->IsType(Value::Type::LIST));
95 ListValue::Swap(static_cast<ListValue*>(root.get())); 95 ListValue::Swap(static_cast<ListValue*>(root.get()));
96 } 96 }
97 97
98 void Swap(ListValue* other) override { 98 void Swap(ListValue* other) override {
99 DVLOG(1) << "Swap()ing a ListValue inefficiently."; 99 DVLOG(1) << "Swap()ing a ListValue inefficiently.";
100 100
101 // First deep copy to convert JSONStringValue to std::string and swap that 101 // First deep copy to convert JSONStringValue to std::string and swap that
102 // copy with |other|, which contains the new contents of |this|. 102 // copy with |other|, which contains the new contents of |this|.
103 std::unique_ptr<ListValue> copy(CreateDeepCopy()); 103 std::unique_ptr<ListValue> copy(CreateDeepCopy());
104 copy->Swap(other); 104 copy->Swap(other);
(...skipping 28 matching lines...) Expand all
133 133
134 DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue); 134 DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue);
135 }; 135 };
136 136
137 // A variant on StringValue that uses StringPiece instead of copying the string 137 // A variant on StringValue that uses StringPiece instead of copying the string
138 // into the Value. This can only be stored in a child of hidden root (above), 138 // into the Value. This can only be stored in a child of hidden root (above),
139 // otherwise the referenced string will not be guaranteed to outlive it. 139 // otherwise the referenced string will not be guaranteed to outlive it.
140 class JSONStringValue : public Value { 140 class JSONStringValue : public Value {
141 public: 141 public:
142 explicit JSONStringValue(StringPiece piece) 142 explicit JSONStringValue(StringPiece piece)
143 : Value(TYPE_STRING), string_piece_(piece) {} 143 : Value(Type::STRING), string_piece_(piece) {}
144 144
145 // Overridden from Value: 145 // Overridden from Value:
146 bool GetAsString(std::string* out_value) const override { 146 bool GetAsString(std::string* out_value) const override {
147 string_piece_.CopyToString(out_value); 147 string_piece_.CopyToString(out_value);
148 return true; 148 return true;
149 } 149 }
150 bool GetAsString(string16* out_value) const override { 150 bool GetAsString(string16* out_value) const override {
151 *out_value = UTF8ToUTF16(string_piece_); 151 *out_value = UTF8ToUTF16(string_piece_);
152 return true; 152 return true;
153 } 153 }
154 Value* DeepCopy() const override { return new StringValue(string_piece_); } 154 Value* DeepCopy() const override { return new StringValue(string_piece_); }
155 bool Equals(const Value* other) const override { 155 bool Equals(const Value* other) const override {
156 std::string other_string; 156 std::string other_string;
157 return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) && 157 return other->IsType(Type::STRING) && other->GetAsString(&other_string) &&
158 StringPiece(other_string) == string_piece_; 158 StringPiece(other_string) == string_piece_;
159 } 159 }
160 160
161 private: 161 private:
162 // The location in the original input stream. 162 // The location in the original input stream.
163 StringPiece string_piece_; 163 StringPiece string_piece_;
164 164
165 DISALLOW_COPY_AND_ASSIGN(JSONStringValue); 165 DISALLOW_COPY_AND_ASSIGN(JSONStringValue);
166 }; 166 };
167 167
168 // Simple class that checks for maximum recursion/"stack overflow." 168 // Simple class that checks for maximum recursion/"stack overflow."
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 if (GetNextToken() != T_END_OF_INPUT) { 248 if (GetNextToken() != T_END_OF_INPUT) {
249 if (!CanConsume(1) || (NextChar() && GetNextToken() != T_END_OF_INPUT)) { 249 if (!CanConsume(1) || (NextChar() && GetNextToken() != T_END_OF_INPUT)) {
250 ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1); 250 ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1);
251 return nullptr; 251 return nullptr;
252 } 252 }
253 } 253 }
254 254
255 // Dictionaries and lists can contain JSONStringValues, so wrap them in a 255 // Dictionaries and lists can contain JSONStringValues, so wrap them in a
256 // hidden root. 256 // hidden root.
257 if (!(options_ & JSON_DETACHABLE_CHILDREN)) { 257 if (!(options_ & JSON_DETACHABLE_CHILDREN)) {
258 if (root->IsType(Value::TYPE_DICTIONARY)) { 258 if (root->IsType(Value::Type::DICTIONARY)) {
259 return MakeUnique<DictionaryHiddenRootValue>(std::move(input_copy), 259 return MakeUnique<DictionaryHiddenRootValue>(std::move(input_copy),
260 std::move(root)); 260 std::move(root));
261 } 261 }
262 if (root->IsType(Value::TYPE_LIST)) { 262 if (root->IsType(Value::Type::LIST)) {
263 return MakeUnique<ListHiddenRootValue>(std::move(input_copy), 263 return MakeUnique<ListHiddenRootValue>(std::move(input_copy),
264 std::move(root)); 264 std::move(root));
265 } 265 }
266 if (root->IsType(Value::TYPE_STRING)) { 266 if (root->IsType(Value::Type::STRING)) {
267 // A string type could be a JSONStringValue, but because there's no 267 // A string type could be a JSONStringValue, but because there's no
268 // corresponding HiddenRootValue, the memory will be lost. Deep copy to 268 // corresponding HiddenRootValue, the memory will be lost. Deep copy to
269 // preserve it. 269 // preserve it.
270 return root->CreateDeepCopy(); 270 return root->CreateDeepCopy();
271 } 271 }
272 } 272 }
273 273
274 // All other values can be returned directly. 274 // All other values can be returned directly.
275 return root; 275 return root;
276 } 276 }
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 const std::string& description) { 989 const std::string& description) {
990 if (line || column) { 990 if (line || column) {
991 return StringPrintf("Line: %i, column: %i, %s", 991 return StringPrintf("Line: %i, column: %i, %s",
992 line, column, description.c_str()); 992 line, column, description.c_str());
993 } 993 }
994 return description; 994 return description;
995 } 995 }
996 996
997 } // namespace internal 997 } // namespace internal
998 } // namespace base 998 } // namespace base
OLDNEW
« no previous file with comments | « android_webview/browser/aw_browser_policy_connector.cc ('k') | base/json/json_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698