OLD | NEW |
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/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
11 #include <ostream> | 11 #include <ostream> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char* const kTypeNames[] = {"null", "boolean", "integer", "double", | 24 const char* const kTypeNames[] = {"null", "boolean", "integer", "double", |
25 "string", "binary", "dictionary", "list"}; | 25 "string", "binary", "dictionary", "list"}; |
26 static_assert(arraysize(kTypeNames) == Value::TYPE_LIST + 1, | 26 static_assert(arraysize(kTypeNames) == |
| 27 static_cast<size_t>(Value::Type::LIST) + 1, |
27 "kTypeNames Has Wrong Size"); | 28 "kTypeNames Has Wrong Size"); |
28 | 29 |
29 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); | 30 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
30 | 31 |
31 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 32 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
32 // in the copy. It's possible for this function to return NULL and it | 33 // in the copy. It's possible for this function to return NULL and it |
33 // expects |node| to always be non-NULL. | 34 // expects |node| to always be non-NULL. |
34 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { | 35 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { |
35 std::unique_ptr<ListValue> copy; | 36 std::unique_ptr<ListValue> copy; |
36 for (const auto& entry : list) { | 37 for (const auto& entry : list) { |
(...skipping 16 matching lines...) Expand all Loading... |
53 if (!copy) | 54 if (!copy) |
54 copy.reset(new DictionaryValue); | 55 copy.reset(new DictionaryValue); |
55 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); | 56 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); |
56 } | 57 } |
57 } | 58 } |
58 return copy; | 59 return copy; |
59 } | 60 } |
60 | 61 |
61 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { | 62 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { |
62 switch (node.GetType()) { | 63 switch (node.GetType()) { |
63 case Value::TYPE_LIST: | 64 case Value::Type::LIST: |
64 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); | 65 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); |
65 | 66 |
66 case Value::TYPE_DICTIONARY: | 67 case Value::Type::DICTIONARY: |
67 return CopyDictionaryWithoutEmptyChildren( | 68 return CopyDictionaryWithoutEmptyChildren( |
68 static_cast<const DictionaryValue&>(node)); | 69 static_cast<const DictionaryValue&>(node)); |
69 | 70 |
70 default: | 71 default: |
71 return node.CreateDeepCopy(); | 72 return node.CreateDeepCopy(); |
72 } | 73 } |
73 } | 74 } |
74 | 75 |
75 } // namespace | 76 } // namespace |
76 | 77 |
77 Value::~Value() { | 78 Value::~Value() { |
78 } | 79 } |
79 | 80 |
80 // static | 81 // static |
81 std::unique_ptr<Value> Value::CreateNullValue() { | 82 std::unique_ptr<Value> Value::CreateNullValue() { |
82 return WrapUnique(new Value(TYPE_NULL)); | 83 return WrapUnique(new Value(Type::NONE)); |
83 } | 84 } |
84 | 85 |
85 // static | 86 // static |
86 const char* Value::GetTypeName(Value::Type type) { | 87 const char* Value::GetTypeName(Value::Type type) { |
87 DCHECK_GE(type, 0); | 88 DCHECK_GE(static_cast<int>(type), 0); |
88 DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames)); | 89 DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames)); |
89 return kTypeNames[type]; | 90 return kTypeNames[static_cast<size_t>(type)]; |
90 } | 91 } |
91 | 92 |
92 bool Value::GetAsBinary(const BinaryValue** out_value) const { | 93 bool Value::GetAsBinary(const BinaryValue** out_value) const { |
93 return false; | 94 return false; |
94 } | 95 } |
95 | 96 |
96 bool Value::GetAsBoolean(bool* out_value) const { | 97 bool Value::GetAsBoolean(bool* out_value) const { |
97 return false; | 98 return false; |
98 } | 99 } |
99 | 100 |
(...skipping 29 matching lines...) Expand all Loading... |
129 return false; | 130 return false; |
130 } | 131 } |
131 | 132 |
132 bool Value::GetAsDictionary(const DictionaryValue** out_value) const { | 133 bool Value::GetAsDictionary(const DictionaryValue** out_value) const { |
133 return false; | 134 return false; |
134 } | 135 } |
135 | 136 |
136 Value* Value::DeepCopy() const { | 137 Value* Value::DeepCopy() const { |
137 // This method should only be getting called for null Values--all subclasses | 138 // This method should only be getting called for null Values--all subclasses |
138 // need to provide their own implementation;. | 139 // need to provide their own implementation;. |
139 DCHECK(IsType(TYPE_NULL)); | 140 DCHECK(IsType(Type::NONE)); |
140 return CreateNullValue().release(); | 141 return CreateNullValue().release(); |
141 } | 142 } |
142 | 143 |
143 std::unique_ptr<Value> Value::CreateDeepCopy() const { | 144 std::unique_ptr<Value> Value::CreateDeepCopy() const { |
144 return WrapUnique(DeepCopy()); | 145 return WrapUnique(DeepCopy()); |
145 } | 146 } |
146 | 147 |
147 bool Value::Equals(const Value* other) const { | 148 bool Value::Equals(const Value* other) const { |
148 // This method should only be getting called for null Values--all subclasses | 149 // This method should only be getting called for null Values--all subclasses |
149 // need to provide their own implementation;. | 150 // need to provide their own implementation;. |
150 DCHECK(IsType(TYPE_NULL)); | 151 DCHECK(IsType(Type::NONE)); |
151 return other->IsType(TYPE_NULL); | 152 return other->IsType(Type::NONE); |
152 } | 153 } |
153 | 154 |
154 // static | 155 // static |
155 bool Value::Equals(const Value* a, const Value* b) { | 156 bool Value::Equals(const Value* a, const Value* b) { |
156 if ((a == NULL) && (b == NULL)) return true; | 157 if ((a == NULL) && (b == NULL)) return true; |
157 if ((a == NULL) ^ (b == NULL)) return false; | 158 if ((a == NULL) ^ (b == NULL)) return false; |
158 return a->Equals(b); | 159 return a->Equals(b); |
159 } | 160 } |
160 | 161 |
161 Value::Value(Type type) : type_(type) {} | 162 Value::Value(Type type) : type_(type) {} |
162 | 163 |
163 Value::Value(const Value& that) : type_(that.type_) {} | 164 Value::Value(const Value& that) : type_(that.type_) {} |
164 | 165 |
165 Value& Value::operator=(const Value& that) { | 166 Value& Value::operator=(const Value& that) { |
166 type_ = that.type_; | 167 type_ = that.type_; |
167 return *this; | 168 return *this; |
168 } | 169 } |
169 | 170 |
170 ///////////////////// FundamentalValue //////////////////// | 171 ///////////////////// FundamentalValue //////////////////// |
171 | 172 |
172 FundamentalValue::FundamentalValue(bool in_value) | 173 FundamentalValue::FundamentalValue(bool in_value) |
173 : Value(TYPE_BOOLEAN), boolean_value_(in_value) { | 174 : Value(Type::BOOLEAN), boolean_value_(in_value) {} |
174 } | |
175 | 175 |
176 FundamentalValue::FundamentalValue(int in_value) | 176 FundamentalValue::FundamentalValue(int in_value) |
177 : Value(TYPE_INTEGER), integer_value_(in_value) { | 177 : Value(Type::INTEGER), integer_value_(in_value) {} |
178 } | |
179 | 178 |
180 FundamentalValue::FundamentalValue(double in_value) | 179 FundamentalValue::FundamentalValue(double in_value) |
181 : Value(TYPE_DOUBLE), double_value_(in_value) { | 180 : Value(Type::DOUBLE), double_value_(in_value) { |
182 if (!std::isfinite(double_value_)) { | 181 if (!std::isfinite(double_value_)) { |
183 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " | 182 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " |
184 << "values cannot be represented in JSON"; | 183 << "values cannot be represented in JSON"; |
185 double_value_ = 0.0; | 184 double_value_ = 0.0; |
186 } | 185 } |
187 } | 186 } |
188 | 187 |
189 FundamentalValue::~FundamentalValue() { | 188 FundamentalValue::~FundamentalValue() { |
190 } | 189 } |
191 | 190 |
192 bool FundamentalValue::GetAsBoolean(bool* out_value) const { | 191 bool FundamentalValue::GetAsBoolean(bool* out_value) const { |
193 if (out_value && IsType(TYPE_BOOLEAN)) | 192 if (out_value && IsType(Type::BOOLEAN)) |
194 *out_value = boolean_value_; | 193 *out_value = boolean_value_; |
195 return (IsType(TYPE_BOOLEAN)); | 194 return (IsType(Type::BOOLEAN)); |
196 } | 195 } |
197 | 196 |
198 bool FundamentalValue::GetAsInteger(int* out_value) const { | 197 bool FundamentalValue::GetAsInteger(int* out_value) const { |
199 if (out_value && IsType(TYPE_INTEGER)) | 198 if (out_value && IsType(Type::INTEGER)) |
200 *out_value = integer_value_; | 199 *out_value = integer_value_; |
201 return (IsType(TYPE_INTEGER)); | 200 return (IsType(Type::INTEGER)); |
202 } | 201 } |
203 | 202 |
204 bool FundamentalValue::GetAsDouble(double* out_value) const { | 203 bool FundamentalValue::GetAsDouble(double* out_value) const { |
205 if (out_value && IsType(TYPE_DOUBLE)) | 204 if (out_value && IsType(Type::DOUBLE)) |
206 *out_value = double_value_; | 205 *out_value = double_value_; |
207 else if (out_value && IsType(TYPE_INTEGER)) | 206 else if (out_value && IsType(Type::INTEGER)) |
208 *out_value = integer_value_; | 207 *out_value = integer_value_; |
209 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); | 208 return (IsType(Type::DOUBLE) || IsType(Type::INTEGER)); |
210 } | 209 } |
211 | 210 |
212 FundamentalValue* FundamentalValue::DeepCopy() const { | 211 FundamentalValue* FundamentalValue::DeepCopy() const { |
213 switch (GetType()) { | 212 switch (GetType()) { |
214 case TYPE_BOOLEAN: | 213 case Type::BOOLEAN: |
215 return new FundamentalValue(boolean_value_); | 214 return new FundamentalValue(boolean_value_); |
216 | 215 |
217 case TYPE_INTEGER: | 216 case Type::INTEGER: |
218 return new FundamentalValue(integer_value_); | 217 return new FundamentalValue(integer_value_); |
219 | 218 |
220 case TYPE_DOUBLE: | 219 case Type::DOUBLE: |
221 return new FundamentalValue(double_value_); | 220 return new FundamentalValue(double_value_); |
222 | 221 |
223 default: | 222 default: |
224 NOTREACHED(); | 223 NOTREACHED(); |
225 return NULL; | 224 return NULL; |
226 } | 225 } |
227 } | 226 } |
228 | 227 |
229 bool FundamentalValue::Equals(const Value* other) const { | 228 bool FundamentalValue::Equals(const Value* other) const { |
230 if (other->GetType() != GetType()) | 229 if (other->GetType() != GetType()) |
231 return false; | 230 return false; |
232 | 231 |
233 switch (GetType()) { | 232 switch (GetType()) { |
234 case TYPE_BOOLEAN: { | 233 case Type::BOOLEAN: { |
235 bool lhs, rhs; | 234 bool lhs, rhs; |
236 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; | 235 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; |
237 } | 236 } |
238 case TYPE_INTEGER: { | 237 case Type::INTEGER: { |
239 int lhs, rhs; | 238 int lhs, rhs; |
240 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; | 239 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; |
241 } | 240 } |
242 case TYPE_DOUBLE: { | 241 case Type::DOUBLE: { |
243 double lhs, rhs; | 242 double lhs, rhs; |
244 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; | 243 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
245 } | 244 } |
246 default: | 245 default: |
247 NOTREACHED(); | 246 NOTREACHED(); |
248 return false; | 247 return false; |
249 } | 248 } |
250 } | 249 } |
251 | 250 |
252 ///////////////////// StringValue //////////////////// | 251 ///////////////////// StringValue //////////////////// |
253 | 252 |
254 StringValue::StringValue(StringPiece in_value) | 253 StringValue::StringValue(StringPiece in_value) |
255 : Value(TYPE_STRING), value_(in_value.as_string()) { | 254 : Value(Type::STRING), value_(in_value.as_string()) { |
256 DCHECK(IsStringUTF8(in_value)); | 255 DCHECK(IsStringUTF8(in_value)); |
257 } | 256 } |
258 | 257 |
259 StringValue::StringValue(const string16& in_value) | 258 StringValue::StringValue(const string16& in_value) |
260 : Value(TYPE_STRING), | 259 : Value(Type::STRING), value_(UTF16ToUTF8(in_value)) {} |
261 value_(UTF16ToUTF8(in_value)) { | |
262 } | |
263 | 260 |
264 StringValue::~StringValue() { | 261 StringValue::~StringValue() { |
265 } | 262 } |
266 | 263 |
267 std::string* StringValue::GetString() { | 264 std::string* StringValue::GetString() { |
268 return &value_; | 265 return &value_; |
269 } | 266 } |
270 | 267 |
271 const std::string& StringValue::GetString() const { | 268 const std::string& StringValue::GetString() const { |
272 return value_; | 269 return value_; |
(...skipping 23 matching lines...) Expand all Loading... |
296 | 293 |
297 bool StringValue::Equals(const Value* other) const { | 294 bool StringValue::Equals(const Value* other) const { |
298 if (other->GetType() != GetType()) | 295 if (other->GetType() != GetType()) |
299 return false; | 296 return false; |
300 std::string lhs, rhs; | 297 std::string lhs, rhs; |
301 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 298 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
302 } | 299 } |
303 | 300 |
304 ///////////////////// BinaryValue //////////////////// | 301 ///////////////////// BinaryValue //////////////////// |
305 | 302 |
306 BinaryValue::BinaryValue() | 303 BinaryValue::BinaryValue() : Value(Type::BINARY), size_(0) {} |
307 : Value(TYPE_BINARY), | |
308 size_(0) { | |
309 } | |
310 | 304 |
311 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) | 305 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) |
312 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} | 306 : Value(Type::BINARY), buffer_(std::move(buffer)), size_(size) {} |
313 | 307 |
314 BinaryValue::~BinaryValue() { | 308 BinaryValue::~BinaryValue() { |
315 } | 309 } |
316 | 310 |
317 // static | 311 // static |
318 std::unique_ptr<BinaryValue> BinaryValue::CreateWithCopiedBuffer( | 312 std::unique_ptr<BinaryValue> BinaryValue::CreateWithCopiedBuffer( |
319 const char* buffer, | 313 const char* buffer, |
320 size_t size) { | 314 size_t size) { |
321 std::unique_ptr<char[]> buffer_copy(new char[size]); | 315 std::unique_ptr<char[]> buffer_copy(new char[size]); |
322 memcpy(buffer_copy.get(), buffer, size); | 316 memcpy(buffer_copy.get(), buffer, size); |
(...skipping 25 matching lines...) Expand all Loading... |
348 std::unique_ptr<DictionaryValue> DictionaryValue::From( | 342 std::unique_ptr<DictionaryValue> DictionaryValue::From( |
349 std::unique_ptr<Value> value) { | 343 std::unique_ptr<Value> value) { |
350 DictionaryValue* out; | 344 DictionaryValue* out; |
351 if (value && value->GetAsDictionary(&out)) { | 345 if (value && value->GetAsDictionary(&out)) { |
352 ignore_result(value.release()); | 346 ignore_result(value.release()); |
353 return WrapUnique(out); | 347 return WrapUnique(out); |
354 } | 348 } |
355 return nullptr; | 349 return nullptr; |
356 } | 350 } |
357 | 351 |
358 DictionaryValue::DictionaryValue() | 352 DictionaryValue::DictionaryValue() : Value(Type::DICTIONARY) {} |
359 : Value(TYPE_DICTIONARY) { | |
360 } | |
361 | 353 |
362 DictionaryValue::~DictionaryValue() { | 354 DictionaryValue::~DictionaryValue() { |
363 Clear(); | 355 Clear(); |
364 } | 356 } |
365 | 357 |
366 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) { | 358 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) { |
367 if (out_value) | 359 if (out_value) |
368 *out_value = this; | 360 *out_value = this; |
369 return true; | 361 return true; |
370 } | 362 } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 } | 546 } |
555 | 547 |
556 out_value->assign(out); | 548 out_value->assign(out); |
557 return true; | 549 return true; |
558 } | 550 } |
559 | 551 |
560 bool DictionaryValue::GetBinary(StringPiece path, | 552 bool DictionaryValue::GetBinary(StringPiece path, |
561 const BinaryValue** out_value) const { | 553 const BinaryValue** out_value) const { |
562 const Value* value; | 554 const Value* value; |
563 bool result = Get(path, &value); | 555 bool result = Get(path, &value); |
564 if (!result || !value->IsType(TYPE_BINARY)) | 556 if (!result || !value->IsType(Type::BINARY)) |
565 return false; | 557 return false; |
566 | 558 |
567 if (out_value) | 559 if (out_value) |
568 *out_value = static_cast<const BinaryValue*>(value); | 560 *out_value = static_cast<const BinaryValue*>(value); |
569 | 561 |
570 return true; | 562 return true; |
571 } | 563 } |
572 | 564 |
573 bool DictionaryValue::GetBinary(StringPiece path, BinaryValue** out_value) { | 565 bool DictionaryValue::GetBinary(StringPiece path, BinaryValue** out_value) { |
574 return static_cast<const DictionaryValue&>(*this).GetBinary( | 566 return static_cast<const DictionaryValue&>(*this).GetBinary( |
575 path, | 567 path, |
576 const_cast<const BinaryValue**>(out_value)); | 568 const_cast<const BinaryValue**>(out_value)); |
577 } | 569 } |
578 | 570 |
579 bool DictionaryValue::GetDictionary(StringPiece path, | 571 bool DictionaryValue::GetDictionary(StringPiece path, |
580 const DictionaryValue** out_value) const { | 572 const DictionaryValue** out_value) const { |
581 const Value* value; | 573 const Value* value; |
582 bool result = Get(path, &value); | 574 bool result = Get(path, &value); |
583 if (!result || !value->IsType(TYPE_DICTIONARY)) | 575 if (!result || !value->IsType(Type::DICTIONARY)) |
584 return false; | 576 return false; |
585 | 577 |
586 if (out_value) | 578 if (out_value) |
587 *out_value = static_cast<const DictionaryValue*>(value); | 579 *out_value = static_cast<const DictionaryValue*>(value); |
588 | 580 |
589 return true; | 581 return true; |
590 } | 582 } |
591 | 583 |
592 bool DictionaryValue::GetDictionary(StringPiece path, | 584 bool DictionaryValue::GetDictionary(StringPiece path, |
593 DictionaryValue** out_value) { | 585 DictionaryValue** out_value) { |
594 return static_cast<const DictionaryValue&>(*this).GetDictionary( | 586 return static_cast<const DictionaryValue&>(*this).GetDictionary( |
595 path, | 587 path, |
596 const_cast<const DictionaryValue**>(out_value)); | 588 const_cast<const DictionaryValue**>(out_value)); |
597 } | 589 } |
598 | 590 |
599 bool DictionaryValue::GetList(StringPiece path, | 591 bool DictionaryValue::GetList(StringPiece path, |
600 const ListValue** out_value) const { | 592 const ListValue** out_value) const { |
601 const Value* value; | 593 const Value* value; |
602 bool result = Get(path, &value); | 594 bool result = Get(path, &value); |
603 if (!result || !value->IsType(TYPE_LIST)) | 595 if (!result || !value->IsType(Type::LIST)) |
604 return false; | 596 return false; |
605 | 597 |
606 if (out_value) | 598 if (out_value) |
607 *out_value = static_cast<const ListValue*>(value); | 599 *out_value = static_cast<const ListValue*>(value); |
608 | 600 |
609 return true; | 601 return true; |
610 } | 602 } |
611 | 603 |
612 bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) { | 604 bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) { |
613 return static_cast<const DictionaryValue&>(*this).GetList( | 605 return static_cast<const DictionaryValue&>(*this).GetList( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 return false; | 670 return false; |
679 | 671 |
680 return value->GetAsString(out_value); | 672 return value->GetAsString(out_value); |
681 } | 673 } |
682 | 674 |
683 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 675 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
684 StringPiece key, | 676 StringPiece key, |
685 const DictionaryValue** out_value) const { | 677 const DictionaryValue** out_value) const { |
686 const Value* value; | 678 const Value* value; |
687 bool result = GetWithoutPathExpansion(key, &value); | 679 bool result = GetWithoutPathExpansion(key, &value); |
688 if (!result || !value->IsType(TYPE_DICTIONARY)) | 680 if (!result || !value->IsType(Type::DICTIONARY)) |
689 return false; | 681 return false; |
690 | 682 |
691 if (out_value) | 683 if (out_value) |
692 *out_value = static_cast<const DictionaryValue*>(value); | 684 *out_value = static_cast<const DictionaryValue*>(value); |
693 | 685 |
694 return true; | 686 return true; |
695 } | 687 } |
696 | 688 |
697 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 689 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
698 StringPiece key, | 690 StringPiece key, |
699 DictionaryValue** out_value) { | 691 DictionaryValue** out_value) { |
700 const DictionaryValue& const_this = | 692 const DictionaryValue& const_this = |
701 static_cast<const DictionaryValue&>(*this); | 693 static_cast<const DictionaryValue&>(*this); |
702 return const_this.GetDictionaryWithoutPathExpansion( | 694 return const_this.GetDictionaryWithoutPathExpansion( |
703 key, | 695 key, |
704 const_cast<const DictionaryValue**>(out_value)); | 696 const_cast<const DictionaryValue**>(out_value)); |
705 } | 697 } |
706 | 698 |
707 bool DictionaryValue::GetListWithoutPathExpansion( | 699 bool DictionaryValue::GetListWithoutPathExpansion( |
708 StringPiece key, | 700 StringPiece key, |
709 const ListValue** out_value) const { | 701 const ListValue** out_value) const { |
710 const Value* value; | 702 const Value* value; |
711 bool result = GetWithoutPathExpansion(key, &value); | 703 bool result = GetWithoutPathExpansion(key, &value); |
712 if (!result || !value->IsType(TYPE_LIST)) | 704 if (!result || !value->IsType(Type::LIST)) |
713 return false; | 705 return false; |
714 | 706 |
715 if (out_value) | 707 if (out_value) |
716 *out_value = static_cast<const ListValue*>(value); | 708 *out_value = static_cast<const ListValue*>(value); |
717 | 709 |
718 return true; | 710 return true; |
719 } | 711 } |
720 | 712 |
721 bool DictionaryValue::GetListWithoutPathExpansion(StringPiece key, | 713 bool DictionaryValue::GetListWithoutPathExpansion(StringPiece key, |
722 ListValue** out_value) { | 714 ListValue** out_value) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 CopyDictionaryWithoutEmptyChildren(*this); | 775 CopyDictionaryWithoutEmptyChildren(*this); |
784 if (!copy) | 776 if (!copy) |
785 copy.reset(new DictionaryValue); | 777 copy.reset(new DictionaryValue); |
786 return copy; | 778 return copy; |
787 } | 779 } |
788 | 780 |
789 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 781 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
790 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { | 782 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
791 const Value* merge_value = &it.value(); | 783 const Value* merge_value = &it.value(); |
792 // Check whether we have to merge dictionaries. | 784 // Check whether we have to merge dictionaries. |
793 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 785 if (merge_value->IsType(Value::Type::DICTIONARY)) { |
794 DictionaryValue* sub_dict; | 786 DictionaryValue* sub_dict; |
795 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) { | 787 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) { |
796 sub_dict->MergeDictionary( | 788 sub_dict->MergeDictionary( |
797 static_cast<const DictionaryValue*>(merge_value)); | 789 static_cast<const DictionaryValue*>(merge_value)); |
798 continue; | 790 continue; |
799 } | 791 } |
800 } | 792 } |
801 // All other cases: Make a copy and hook it up. | 793 // All other cases: Make a copy and hook it up. |
802 SetWithoutPathExpansion(it.key(), | 794 SetWithoutPathExpansion(it.key(), |
803 base::WrapUnique(merge_value->DeepCopy())); | 795 base::WrapUnique(merge_value->DeepCopy())); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 // static | 850 // static |
859 std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) { | 851 std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) { |
860 ListValue* out; | 852 ListValue* out; |
861 if (value && value->GetAsList(&out)) { | 853 if (value && value->GetAsList(&out)) { |
862 ignore_result(value.release()); | 854 ignore_result(value.release()); |
863 return WrapUnique(out); | 855 return WrapUnique(out); |
864 } | 856 } |
865 return nullptr; | 857 return nullptr; |
866 } | 858 } |
867 | 859 |
868 ListValue::ListValue() : Value(TYPE_LIST) { | 860 ListValue::ListValue() : Value(Type::LIST) {} |
869 } | |
870 | 861 |
871 ListValue::~ListValue() { | 862 ListValue::~ListValue() { |
872 Clear(); | 863 Clear(); |
873 } | 864 } |
874 | 865 |
875 void ListValue::Clear() { | 866 void ListValue::Clear() { |
876 list_.clear(); | 867 list_.clear(); |
877 } | 868 } |
878 | 869 |
879 bool ListValue::Set(size_t index, Value* in_value) { | 870 bool ListValue::Set(size_t index, Value* in_value) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 const Value* value; | 940 const Value* value; |
950 if (!Get(index, &value)) | 941 if (!Get(index, &value)) |
951 return false; | 942 return false; |
952 | 943 |
953 return value->GetAsString(out_value); | 944 return value->GetAsString(out_value); |
954 } | 945 } |
955 | 946 |
956 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const { | 947 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const { |
957 const Value* value; | 948 const Value* value; |
958 bool result = Get(index, &value); | 949 bool result = Get(index, &value); |
959 if (!result || !value->IsType(TYPE_BINARY)) | 950 if (!result || !value->IsType(Type::BINARY)) |
960 return false; | 951 return false; |
961 | 952 |
962 if (out_value) | 953 if (out_value) |
963 *out_value = static_cast<const BinaryValue*>(value); | 954 *out_value = static_cast<const BinaryValue*>(value); |
964 | 955 |
965 return true; | 956 return true; |
966 } | 957 } |
967 | 958 |
968 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) { | 959 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) { |
969 return static_cast<const ListValue&>(*this).GetBinary( | 960 return static_cast<const ListValue&>(*this).GetBinary( |
970 index, | 961 index, |
971 const_cast<const BinaryValue**>(out_value)); | 962 const_cast<const BinaryValue**>(out_value)); |
972 } | 963 } |
973 | 964 |
974 bool ListValue::GetDictionary(size_t index, | 965 bool ListValue::GetDictionary(size_t index, |
975 const DictionaryValue** out_value) const { | 966 const DictionaryValue** out_value) const { |
976 const Value* value; | 967 const Value* value; |
977 bool result = Get(index, &value); | 968 bool result = Get(index, &value); |
978 if (!result || !value->IsType(TYPE_DICTIONARY)) | 969 if (!result || !value->IsType(Type::DICTIONARY)) |
979 return false; | 970 return false; |
980 | 971 |
981 if (out_value) | 972 if (out_value) |
982 *out_value = static_cast<const DictionaryValue*>(value); | 973 *out_value = static_cast<const DictionaryValue*>(value); |
983 | 974 |
984 return true; | 975 return true; |
985 } | 976 } |
986 | 977 |
987 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) { | 978 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) { |
988 return static_cast<const ListValue&>(*this).GetDictionary( | 979 return static_cast<const ListValue&>(*this).GetDictionary( |
989 index, | 980 index, |
990 const_cast<const DictionaryValue**>(out_value)); | 981 const_cast<const DictionaryValue**>(out_value)); |
991 } | 982 } |
992 | 983 |
993 bool ListValue::GetList(size_t index, const ListValue** out_value) const { | 984 bool ListValue::GetList(size_t index, const ListValue** out_value) const { |
994 const Value* value; | 985 const Value* value; |
995 bool result = Get(index, &value); | 986 bool result = Get(index, &value); |
996 if (!result || !value->IsType(TYPE_LIST)) | 987 if (!result || !value->IsType(Type::LIST)) |
997 return false; | 988 return false; |
998 | 989 |
999 if (out_value) | 990 if (out_value) |
1000 *out_value = static_cast<const ListValue*>(value); | 991 *out_value = static_cast<const ListValue*>(value); |
1001 | 992 |
1002 return true; | 993 return true; |
1003 } | 994 } |
1004 | 995 |
1005 bool ListValue::GetList(size_t index, ListValue** out_value) { | 996 bool ListValue::GetList(size_t index, ListValue** out_value) { |
1006 return static_cast<const ListValue&>(*this).GetList( | 997 return static_cast<const ListValue&>(*this).GetList( |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1166 | 1157 |
1167 ValueDeserializer::~ValueDeserializer() { | 1158 ValueDeserializer::~ValueDeserializer() { |
1168 } | 1159 } |
1169 | 1160 |
1170 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1161 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1171 std::string json; | 1162 std::string json; |
1172 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1163 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
1173 return out << json; | 1164 return out << json; |
1174 } | 1165 } |
1175 | 1166 |
| 1167 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
| 1168 if (static_cast<int>(type) < 0 || |
| 1169 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
| 1170 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
| 1171 return out << Value::GetTypeName(type); |
| 1172 } |
| 1173 |
1176 } // namespace base | 1174 } // namespace base |
OLD | NEW |