Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / values.cc
blob9b2483e7c18c538e877f8563118b633a4433507f
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
3 // found in the LICENSE file.
5 #include "base/values.h"
7 #include <string.h>
9 #include <algorithm>
10 #include <cmath>
11 #include <ostream>
13 #include "base/json/json_writer.h"
14 #include "base/logging.h"
15 #include "base/move.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
19 namespace base {
21 namespace {
23 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
25 // Make a deep copy of |node|, but don't include empty lists or dictionaries
26 // in the copy. It's possible for this function to return NULL and it
27 // expects |node| to always be non-NULL.
28 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) {
29 scoped_ptr<ListValue> copy;
30 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) {
31 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it);
32 if (child_copy) {
33 if (!copy)
34 copy.reset(new ListValue);
35 copy->Append(child_copy.Pass());
38 return copy;
41 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
42 const DictionaryValue& dict) {
43 scoped_ptr<DictionaryValue> copy;
44 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
45 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value());
46 if (child_copy) {
47 if (!copy)
48 copy.reset(new DictionaryValue);
49 copy->SetWithoutPathExpansion(it.key(), child_copy.Pass());
52 return copy;
55 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
56 switch (node.GetType()) {
57 case Value::TYPE_LIST:
58 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node));
60 case Value::TYPE_DICTIONARY:
61 return CopyDictionaryWithoutEmptyChildren(
62 static_cast<const DictionaryValue&>(node));
64 default:
65 return node.CreateDeepCopy();
69 // A small functor for comparing Values for std::find_if and similar.
70 class ValueEquals {
71 public:
72 // Pass the value against which all consecutive calls of the () operator will
73 // compare their argument to. This Value object must not be destroyed while
74 // the ValueEquals is in use.
75 explicit ValueEquals(const Value* first) : first_(first) { }
77 bool operator ()(const Value* second) const {
78 return first_->Equals(second);
81 private:
82 const Value* first_;
85 } // namespace
87 Value::~Value() {
90 // static
91 scoped_ptr<Value> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL));
95 bool Value::GetAsBinary(const BinaryValue** out_value) const {
96 return false;
99 bool Value::GetAsBoolean(bool* out_value) const {
100 return false;
103 bool Value::GetAsInteger(int* out_value) const {
104 return false;
107 bool Value::GetAsDouble(double* out_value) const {
108 return false;
111 bool Value::GetAsString(std::string* out_value) const {
112 return false;
115 bool Value::GetAsString(string16* out_value) const {
116 return false;
119 bool Value::GetAsString(const StringValue** out_value) const {
120 return false;
123 bool Value::GetAsList(ListValue** out_value) {
124 return false;
127 bool Value::GetAsList(const ListValue** out_value) const {
128 return false;
131 bool Value::GetAsDictionary(DictionaryValue** out_value) {
132 return false;
135 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
136 return false;
139 Value* Value::DeepCopy() const {
140 // This method should only be getting called for null Values--all subclasses
141 // need to provide their own implementation;.
142 DCHECK(IsType(TYPE_NULL));
143 return CreateNullValue().release();
146 scoped_ptr<Value> Value::CreateDeepCopy() const {
147 return make_scoped_ptr(DeepCopy());
150 bool Value::Equals(const Value* other) const {
151 // This method should only be getting called for null Values--all subclasses
152 // need to provide their own implementation;.
153 DCHECK(IsType(TYPE_NULL));
154 return other->IsType(TYPE_NULL);
157 // static
158 bool Value::Equals(const Value* a, const Value* b) {
159 if ((a == NULL) && (b == NULL)) return true;
160 if ((a == NULL) ^ (b == NULL)) return false;
161 return a->Equals(b);
164 Value::Value(Type type) : type_(type) {}
166 Value::Value(const Value& that) : type_(that.type_) {}
168 Value& Value::operator=(const Value& that) {
169 type_ = that.type_;
170 return *this;
173 ///////////////////// FundamentalValue ////////////////////
175 FundamentalValue::FundamentalValue(bool in_value)
176 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
179 FundamentalValue::FundamentalValue(int in_value)
180 : Value(TYPE_INTEGER), integer_value_(in_value) {
183 FundamentalValue::FundamentalValue(double in_value)
184 : Value(TYPE_DOUBLE), double_value_(in_value) {
185 if (!std::isfinite(double_value_)) {
186 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
187 << "values cannot be represented in JSON";
188 double_value_ = 0.0;
192 FundamentalValue::~FundamentalValue() {
195 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
196 if (out_value && IsType(TYPE_BOOLEAN))
197 *out_value = boolean_value_;
198 return (IsType(TYPE_BOOLEAN));
201 bool FundamentalValue::GetAsInteger(int* out_value) const {
202 if (out_value && IsType(TYPE_INTEGER))
203 *out_value = integer_value_;
204 return (IsType(TYPE_INTEGER));
207 bool FundamentalValue::GetAsDouble(double* out_value) const {
208 if (out_value && IsType(TYPE_DOUBLE))
209 *out_value = double_value_;
210 else if (out_value && IsType(TYPE_INTEGER))
211 *out_value = integer_value_;
212 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
215 FundamentalValue* FundamentalValue::DeepCopy() const {
216 switch (GetType()) {
217 case TYPE_BOOLEAN:
218 return new FundamentalValue(boolean_value_);
220 case TYPE_INTEGER:
221 return new FundamentalValue(integer_value_);
223 case TYPE_DOUBLE:
224 return new FundamentalValue(double_value_);
226 default:
227 NOTREACHED();
228 return NULL;
232 bool FundamentalValue::Equals(const Value* other) const {
233 if (other->GetType() != GetType())
234 return false;
236 switch (GetType()) {
237 case TYPE_BOOLEAN: {
238 bool lhs, rhs;
239 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
241 case TYPE_INTEGER: {
242 int lhs, rhs;
243 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
245 case TYPE_DOUBLE: {
246 double lhs, rhs;
247 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
249 default:
250 NOTREACHED();
251 return false;
255 ///////////////////// StringValue ////////////////////
257 StringValue::StringValue(const std::string& in_value)
258 : Value(TYPE_STRING),
259 value_(in_value) {
260 DCHECK(IsStringUTF8(in_value));
263 StringValue::StringValue(const string16& in_value)
264 : Value(TYPE_STRING),
265 value_(UTF16ToUTF8(in_value)) {
268 StringValue::~StringValue() {
271 std::string* StringValue::GetString() {
272 return &value_;
275 const std::string& StringValue::GetString() const {
276 return value_;
279 bool StringValue::GetAsString(std::string* out_value) const {
280 if (out_value)
281 *out_value = value_;
282 return true;
285 bool StringValue::GetAsString(string16* out_value) const {
286 if (out_value)
287 *out_value = UTF8ToUTF16(value_);
288 return true;
291 bool StringValue::GetAsString(const StringValue** out_value) const {
292 if (out_value)
293 *out_value = this;
294 return true;
297 StringValue* StringValue::DeepCopy() const {
298 return new StringValue(value_);
301 bool StringValue::Equals(const Value* other) const {
302 if (other->GetType() != GetType())
303 return false;
304 std::string lhs, rhs;
305 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
308 ///////////////////// BinaryValue ////////////////////
310 BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY),
312 size_(0) {
315 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
316 : Value(TYPE_BINARY),
317 buffer_(buffer.Pass()),
318 size_(size) {
321 BinaryValue::~BinaryValue() {
324 // static
325 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
326 size_t size) {
327 char* buffer_copy = new char[size];
328 memcpy(buffer_copy, buffer, size);
329 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
330 return new BinaryValue(scoped_buffer_copy.Pass(), size);
333 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
334 if (out_value)
335 *out_value = this;
336 return true;
339 BinaryValue* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_.get(), size_);
343 bool BinaryValue::Equals(const Value* other) const {
344 if (other->GetType() != GetType())
345 return false;
346 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
347 if (other_binary->size_ != size_)
348 return false;
349 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
352 ///////////////////// DictionaryValue ////////////////////
354 // static
355 scoped_ptr<DictionaryValue> DictionaryValue::From(scoped_ptr<Value> value) {
356 DictionaryValue* out;
357 if (value && value->GetAsDictionary(&out)) {
358 ignore_result(value.release());
359 return make_scoped_ptr(out);
361 return nullptr;
364 DictionaryValue::DictionaryValue()
365 : Value(TYPE_DICTIONARY) {
368 DictionaryValue::~DictionaryValue() {
369 Clear();
372 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
373 if (out_value)
374 *out_value = this;
375 return true;
378 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
379 if (out_value)
380 *out_value = this;
381 return true;
384 bool DictionaryValue::HasKey(const std::string& key) const {
385 DCHECK(IsStringUTF8(key));
386 ValueMap::const_iterator current_entry = dictionary_.find(key);
387 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
388 return current_entry != dictionary_.end();
391 void DictionaryValue::Clear() {
392 ValueMap::iterator dict_iterator = dictionary_.begin();
393 while (dict_iterator != dictionary_.end()) {
394 delete dict_iterator->second;
395 ++dict_iterator;
398 dictionary_.clear();
401 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
402 DCHECK(IsStringUTF8(path));
403 DCHECK(in_value);
405 std::string current_path(path);
406 DictionaryValue* current_dictionary = this;
407 for (size_t delimiter_position = current_path.find('.');
408 delimiter_position != std::string::npos;
409 delimiter_position = current_path.find('.')) {
410 // Assume that we're indexing into a dictionary.
411 std::string key(current_path, 0, delimiter_position);
412 DictionaryValue* child_dictionary = NULL;
413 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
414 child_dictionary = new DictionaryValue;
415 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
418 current_dictionary = child_dictionary;
419 current_path.erase(0, delimiter_position + 1);
422 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
425 void DictionaryValue::Set(const std::string& path, Value* in_value) {
426 Set(path, make_scoped_ptr(in_value));
429 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
430 Set(path, new FundamentalValue(in_value));
433 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
434 Set(path, new FundamentalValue(in_value));
437 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
438 Set(path, new FundamentalValue(in_value));
441 void DictionaryValue::SetString(const std::string& path,
442 const std::string& in_value) {
443 Set(path, new StringValue(in_value));
446 void DictionaryValue::SetString(const std::string& path,
447 const string16& in_value) {
448 Set(path, new StringValue(in_value));
451 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
452 scoped_ptr<Value> in_value) {
453 Value* bare_ptr = in_value.release();
454 // If there's an existing value here, we need to delete it, because
455 // we own all our children.
456 std::pair<ValueMap::iterator, bool> ins_res =
457 dictionary_.insert(std::make_pair(key, bare_ptr));
458 if (!ins_res.second) {
459 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
460 delete ins_res.first->second;
461 ins_res.first->second = bare_ptr;
465 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
466 Value* in_value) {
467 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
470 void DictionaryValue::SetBooleanWithoutPathExpansion(
471 const std::string& path, bool in_value) {
472 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
475 void DictionaryValue::SetIntegerWithoutPathExpansion(
476 const std::string& path, int in_value) {
477 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
480 void DictionaryValue::SetDoubleWithoutPathExpansion(
481 const std::string& path, double in_value) {
482 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
485 void DictionaryValue::SetStringWithoutPathExpansion(
486 const std::string& path, const std::string& in_value) {
487 SetWithoutPathExpansion(path, new StringValue(in_value));
490 void DictionaryValue::SetStringWithoutPathExpansion(
491 const std::string& path, const string16& in_value) {
492 SetWithoutPathExpansion(path, new StringValue(in_value));
495 bool DictionaryValue::Get(StringPiece path,
496 const Value** out_value) const {
497 DCHECK(IsStringUTF8(path));
498 StringPiece current_path(path);
499 const DictionaryValue* current_dictionary = this;
500 for (size_t delimiter_position = current_path.find('.');
501 delimiter_position != std::string::npos;
502 delimiter_position = current_path.find('.')) {
503 const DictionaryValue* child_dictionary = NULL;
504 if (!current_dictionary->GetDictionaryWithoutPathExpansion(
505 current_path.substr(0, delimiter_position).as_string(),
506 &child_dictionary)) {
507 return false;
510 current_dictionary = child_dictionary;
511 current_path = current_path.substr(delimiter_position + 1);
514 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(),
515 out_value);
518 bool DictionaryValue::Get(StringPiece path, Value** out_value) {
519 return static_cast<const DictionaryValue&>(*this).Get(
520 path,
521 const_cast<const Value**>(out_value));
524 bool DictionaryValue::GetBoolean(const std::string& path,
525 bool* bool_value) const {
526 const Value* value;
527 if (!Get(path, &value))
528 return false;
530 return value->GetAsBoolean(bool_value);
533 bool DictionaryValue::GetInteger(const std::string& path,
534 int* out_value) const {
535 const Value* value;
536 if (!Get(path, &value))
537 return false;
539 return value->GetAsInteger(out_value);
542 bool DictionaryValue::GetDouble(const std::string& path,
543 double* out_value) const {
544 const Value* value;
545 if (!Get(path, &value))
546 return false;
548 return value->GetAsDouble(out_value);
551 bool DictionaryValue::GetString(const std::string& path,
552 std::string* out_value) const {
553 const Value* value;
554 if (!Get(path, &value))
555 return false;
557 return value->GetAsString(out_value);
560 bool DictionaryValue::GetString(const std::string& path,
561 string16* out_value) const {
562 const Value* value;
563 if (!Get(path, &value))
564 return false;
566 return value->GetAsString(out_value);
569 bool DictionaryValue::GetStringASCII(const std::string& path,
570 std::string* out_value) const {
571 std::string out;
572 if (!GetString(path, &out))
573 return false;
575 if (!IsStringASCII(out)) {
576 NOTREACHED();
577 return false;
580 out_value->assign(out);
581 return true;
584 bool DictionaryValue::GetBinary(const std::string& path,
585 const BinaryValue** out_value) const {
586 const Value* value;
587 bool result = Get(path, &value);
588 if (!result || !value->IsType(TYPE_BINARY))
589 return false;
591 if (out_value)
592 *out_value = static_cast<const BinaryValue*>(value);
594 return true;
597 bool DictionaryValue::GetBinary(const std::string& path,
598 BinaryValue** out_value) {
599 return static_cast<const DictionaryValue&>(*this).GetBinary(
600 path,
601 const_cast<const BinaryValue**>(out_value));
604 bool DictionaryValue::GetDictionary(StringPiece path,
605 const DictionaryValue** out_value) const {
606 const Value* value;
607 bool result = Get(path, &value);
608 if (!result || !value->IsType(TYPE_DICTIONARY))
609 return false;
611 if (out_value)
612 *out_value = static_cast<const DictionaryValue*>(value);
614 return true;
617 bool DictionaryValue::GetDictionary(StringPiece path,
618 DictionaryValue** out_value) {
619 return static_cast<const DictionaryValue&>(*this).GetDictionary(
620 path,
621 const_cast<const DictionaryValue**>(out_value));
624 bool DictionaryValue::GetList(const std::string& path,
625 const ListValue** out_value) const {
626 const Value* value;
627 bool result = Get(path, &value);
628 if (!result || !value->IsType(TYPE_LIST))
629 return false;
631 if (out_value)
632 *out_value = static_cast<const ListValue*>(value);
634 return true;
637 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
638 return static_cast<const DictionaryValue&>(*this).GetList(
639 path,
640 const_cast<const ListValue**>(out_value));
643 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
644 const Value** out_value) const {
645 DCHECK(IsStringUTF8(key));
646 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
647 if (entry_iterator == dictionary_.end())
648 return false;
650 const Value* entry = entry_iterator->second;
651 if (out_value)
652 *out_value = entry;
653 return true;
656 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
657 Value** out_value) {
658 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
659 key,
660 const_cast<const Value**>(out_value));
663 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
664 bool* out_value) const {
665 const Value* value;
666 if (!GetWithoutPathExpansion(key, &value))
667 return false;
669 return value->GetAsBoolean(out_value);
672 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
673 int* out_value) const {
674 const Value* value;
675 if (!GetWithoutPathExpansion(key, &value))
676 return false;
678 return value->GetAsInteger(out_value);
681 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
682 double* out_value) const {
683 const Value* value;
684 if (!GetWithoutPathExpansion(key, &value))
685 return false;
687 return value->GetAsDouble(out_value);
690 bool DictionaryValue::GetStringWithoutPathExpansion(
691 const std::string& key,
692 std::string* out_value) const {
693 const Value* value;
694 if (!GetWithoutPathExpansion(key, &value))
695 return false;
697 return value->GetAsString(out_value);
700 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
701 string16* out_value) const {
702 const Value* value;
703 if (!GetWithoutPathExpansion(key, &value))
704 return false;
706 return value->GetAsString(out_value);
709 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
710 const std::string& key,
711 const DictionaryValue** out_value) const {
712 const Value* value;
713 bool result = GetWithoutPathExpansion(key, &value);
714 if (!result || !value->IsType(TYPE_DICTIONARY))
715 return false;
717 if (out_value)
718 *out_value = static_cast<const DictionaryValue*>(value);
720 return true;
723 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
724 const std::string& key,
725 DictionaryValue** out_value) {
726 const DictionaryValue& const_this =
727 static_cast<const DictionaryValue&>(*this);
728 return const_this.GetDictionaryWithoutPathExpansion(
729 key,
730 const_cast<const DictionaryValue**>(out_value));
733 bool DictionaryValue::GetListWithoutPathExpansion(
734 const std::string& key,
735 const ListValue** out_value) const {
736 const Value* value;
737 bool result = GetWithoutPathExpansion(key, &value);
738 if (!result || !value->IsType(TYPE_LIST))
739 return false;
741 if (out_value)
742 *out_value = static_cast<const ListValue*>(value);
744 return true;
747 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
748 ListValue** out_value) {
749 return
750 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
751 key,
752 const_cast<const ListValue**>(out_value));
755 bool DictionaryValue::Remove(const std::string& path,
756 scoped_ptr<Value>* out_value) {
757 DCHECK(IsStringUTF8(path));
758 std::string current_path(path);
759 DictionaryValue* current_dictionary = this;
760 size_t delimiter_position = current_path.rfind('.');
761 if (delimiter_position != std::string::npos) {
762 if (!GetDictionary(current_path.substr(0, delimiter_position),
763 &current_dictionary))
764 return false;
765 current_path.erase(0, delimiter_position + 1);
768 return current_dictionary->RemoveWithoutPathExpansion(current_path,
769 out_value);
772 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
773 scoped_ptr<Value>* out_value) {
774 DCHECK(IsStringUTF8(key));
775 ValueMap::iterator entry_iterator = dictionary_.find(key);
776 if (entry_iterator == dictionary_.end())
777 return false;
779 Value* entry = entry_iterator->second;
780 if (out_value)
781 out_value->reset(entry);
782 else
783 delete entry;
784 dictionary_.erase(entry_iterator);
785 return true;
788 bool DictionaryValue::RemovePath(const std::string& path,
789 scoped_ptr<Value>* out_value) {
790 bool result = false;
791 size_t delimiter_position = path.find('.');
793 if (delimiter_position == std::string::npos)
794 return RemoveWithoutPathExpansion(path, out_value);
796 const std::string subdict_path = path.substr(0, delimiter_position);
797 DictionaryValue* subdict = NULL;
798 if (!GetDictionary(subdict_path, &subdict))
799 return false;
800 result = subdict->RemovePath(path.substr(delimiter_position + 1),
801 out_value);
802 if (result && subdict->empty())
803 RemoveWithoutPathExpansion(subdict_path, NULL);
805 return result;
808 scoped_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
809 const {
810 scoped_ptr<DictionaryValue> copy = CopyDictionaryWithoutEmptyChildren(*this);
811 if (!copy)
812 copy.reset(new DictionaryValue);
813 return copy;
816 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
817 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
818 const Value* merge_value = &it.value();
819 // Check whether we have to merge dictionaries.
820 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
821 DictionaryValue* sub_dict;
822 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
823 sub_dict->MergeDictionary(
824 static_cast<const DictionaryValue*>(merge_value));
825 continue;
828 // All other cases: Make a copy and hook it up.
829 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
833 void DictionaryValue::Swap(DictionaryValue* other) {
834 dictionary_.swap(other->dictionary_);
837 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
838 : target_(target),
839 it_(target.dictionary_.begin()) {}
841 DictionaryValue::Iterator::~Iterator() {}
843 DictionaryValue* DictionaryValue::DeepCopy() const {
844 DictionaryValue* result = new DictionaryValue;
846 for (ValueMap::const_iterator current_entry(dictionary_.begin());
847 current_entry != dictionary_.end(); ++current_entry) {
848 result->SetWithoutPathExpansion(current_entry->first,
849 current_entry->second->DeepCopy());
852 return result;
855 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
856 return make_scoped_ptr(DeepCopy());
859 bool DictionaryValue::Equals(const Value* other) const {
860 if (other->GetType() != GetType())
861 return false;
863 const DictionaryValue* other_dict =
864 static_cast<const DictionaryValue*>(other);
865 Iterator lhs_it(*this);
866 Iterator rhs_it(*other_dict);
867 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
868 if (lhs_it.key() != rhs_it.key() ||
869 !lhs_it.value().Equals(&rhs_it.value())) {
870 return false;
872 lhs_it.Advance();
873 rhs_it.Advance();
875 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
876 return false;
878 return true;
881 ///////////////////// ListValue ////////////////////
883 // static
884 scoped_ptr<ListValue> ListValue::From(scoped_ptr<Value> value) {
885 ListValue* out;
886 if (value && value->GetAsList(&out)) {
887 ignore_result(value.release());
888 return make_scoped_ptr(out);
890 return nullptr;
893 ListValue::ListValue() : Value(TYPE_LIST) {
896 ListValue::~ListValue() {
897 Clear();
900 void ListValue::Clear() {
901 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
902 delete *i;
903 list_.clear();
906 bool ListValue::Set(size_t index, Value* in_value) {
907 if (!in_value)
908 return false;
910 if (index >= list_.size()) {
911 // Pad out any intermediate indexes with null settings
912 while (index > list_.size())
913 Append(CreateNullValue());
914 Append(in_value);
915 } else {
916 DCHECK(list_[index] != in_value);
917 delete list_[index];
918 list_[index] = in_value;
920 return true;
923 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
924 return Set(index, in_value.release());
927 bool ListValue::Get(size_t index, const Value** out_value) const {
928 if (index >= list_.size())
929 return false;
931 if (out_value)
932 *out_value = list_[index];
934 return true;
937 bool ListValue::Get(size_t index, Value** out_value) {
938 return static_cast<const ListValue&>(*this).Get(
939 index,
940 const_cast<const Value**>(out_value));
943 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
944 const Value* value;
945 if (!Get(index, &value))
946 return false;
948 return value->GetAsBoolean(bool_value);
951 bool ListValue::GetInteger(size_t index, int* out_value) const {
952 const Value* value;
953 if (!Get(index, &value))
954 return false;
956 return value->GetAsInteger(out_value);
959 bool ListValue::GetDouble(size_t index, double* out_value) const {
960 const Value* value;
961 if (!Get(index, &value))
962 return false;
964 return value->GetAsDouble(out_value);
967 bool ListValue::GetString(size_t index, std::string* out_value) const {
968 const Value* value;
969 if (!Get(index, &value))
970 return false;
972 return value->GetAsString(out_value);
975 bool ListValue::GetString(size_t index, string16* out_value) const {
976 const Value* value;
977 if (!Get(index, &value))
978 return false;
980 return value->GetAsString(out_value);
983 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
984 const Value* value;
985 bool result = Get(index, &value);
986 if (!result || !value->IsType(TYPE_BINARY))
987 return false;
989 if (out_value)
990 *out_value = static_cast<const BinaryValue*>(value);
992 return true;
995 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
996 return static_cast<const ListValue&>(*this).GetBinary(
997 index,
998 const_cast<const BinaryValue**>(out_value));
1001 bool ListValue::GetDictionary(size_t index,
1002 const DictionaryValue** out_value) const {
1003 const Value* value;
1004 bool result = Get(index, &value);
1005 if (!result || !value->IsType(TYPE_DICTIONARY))
1006 return false;
1008 if (out_value)
1009 *out_value = static_cast<const DictionaryValue*>(value);
1011 return true;
1014 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
1015 return static_cast<const ListValue&>(*this).GetDictionary(
1016 index,
1017 const_cast<const DictionaryValue**>(out_value));
1020 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
1021 const Value* value;
1022 bool result = Get(index, &value);
1023 if (!result || !value->IsType(TYPE_LIST))
1024 return false;
1026 if (out_value)
1027 *out_value = static_cast<const ListValue*>(value);
1029 return true;
1032 bool ListValue::GetList(size_t index, ListValue** out_value) {
1033 return static_cast<const ListValue&>(*this).GetList(
1034 index,
1035 const_cast<const ListValue**>(out_value));
1038 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1039 if (index >= list_.size())
1040 return false;
1042 if (out_value)
1043 out_value->reset(list_[index]);
1044 else
1045 delete list_[index];
1047 list_.erase(list_.begin() + index);
1048 return true;
1051 bool ListValue::Remove(const Value& value, size_t* index) {
1052 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1053 if ((*i)->Equals(&value)) {
1054 size_t previous_index = i - list_.begin();
1055 delete *i;
1056 list_.erase(i);
1058 if (index)
1059 *index = previous_index;
1060 return true;
1063 return false;
1066 ListValue::iterator ListValue::Erase(iterator iter,
1067 scoped_ptr<Value>* out_value) {
1068 if (out_value)
1069 out_value->reset(*iter);
1070 else
1071 delete *iter;
1073 return list_.erase(iter);
1076 void ListValue::Append(scoped_ptr<Value> in_value) {
1077 Append(in_value.release());
1080 void ListValue::Append(Value* in_value) {
1081 DCHECK(in_value);
1082 list_.push_back(in_value);
1085 void ListValue::AppendBoolean(bool in_value) {
1086 Append(new FundamentalValue(in_value));
1089 void ListValue::AppendInteger(int in_value) {
1090 Append(new FundamentalValue(in_value));
1093 void ListValue::AppendDouble(double in_value) {
1094 Append(new FundamentalValue(in_value));
1097 void ListValue::AppendString(const std::string& in_value) {
1098 Append(new StringValue(in_value));
1101 void ListValue::AppendString(const string16& in_value) {
1102 Append(new StringValue(in_value));
1105 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1106 for (std::vector<std::string>::const_iterator it = in_values.begin();
1107 it != in_values.end(); ++it) {
1108 AppendString(*it);
1112 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1113 for (std::vector<string16>::const_iterator it = in_values.begin();
1114 it != in_values.end(); ++it) {
1115 AppendString(*it);
1119 bool ListValue::AppendIfNotPresent(Value* in_value) {
1120 DCHECK(in_value);
1121 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1122 if ((*i)->Equals(in_value)) {
1123 delete in_value;
1124 return false;
1127 list_.push_back(in_value);
1128 return true;
1131 bool ListValue::Insert(size_t index, Value* in_value) {
1132 DCHECK(in_value);
1133 if (index > list_.size())
1134 return false;
1136 list_.insert(list_.begin() + index, in_value);
1137 return true;
1140 ListValue::const_iterator ListValue::Find(const Value& value) const {
1141 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1144 void ListValue::Swap(ListValue* other) {
1145 list_.swap(other->list_);
1148 bool ListValue::GetAsList(ListValue** out_value) {
1149 if (out_value)
1150 *out_value = this;
1151 return true;
1154 bool ListValue::GetAsList(const ListValue** out_value) const {
1155 if (out_value)
1156 *out_value = this;
1157 return true;
1160 ListValue* ListValue::DeepCopy() const {
1161 ListValue* result = new ListValue;
1163 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1164 result->Append((*i)->DeepCopy());
1166 return result;
1169 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1170 return make_scoped_ptr(DeepCopy());
1173 bool ListValue::Equals(const Value* other) const {
1174 if (other->GetType() != GetType())
1175 return false;
1177 const ListValue* other_list =
1178 static_cast<const ListValue*>(other);
1179 const_iterator lhs_it, rhs_it;
1180 for (lhs_it = begin(), rhs_it = other_list->begin();
1181 lhs_it != end() && rhs_it != other_list->end();
1182 ++lhs_it, ++rhs_it) {
1183 if (!(*lhs_it)->Equals(*rhs_it))
1184 return false;
1186 if (lhs_it != end() || rhs_it != other_list->end())
1187 return false;
1189 return true;
1192 ValueSerializer::~ValueSerializer() {
1195 ValueDeserializer::~ValueDeserializer() {
1198 std::ostream& operator<<(std::ostream& out, const Value& value) {
1199 std::string json;
1200 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1201 return out << json;
1204 } // namespace base