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"
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"
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
);
34 copy
.reset(new ListValue
);
35 copy
->Append(child_copy
.Pass());
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());
48 copy
.reset(new DictionaryValue
);
49 copy
->SetWithoutPathExpansion(it
.key(), child_copy
.Pass());
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
));
65 return node
.CreateDeepCopy();
69 // A small functor for comparing Values for std::find_if and similar.
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
);
91 scoped_ptr
<Value
> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL
));
95 bool Value::GetAsBinary(const BinaryValue
** out_value
) const {
99 bool Value::GetAsBoolean(bool* out_value
) const {
103 bool Value::GetAsInteger(int* out_value
) const {
107 bool Value::GetAsDouble(double* out_value
) const {
111 bool Value::GetAsString(std::string
* out_value
) const {
115 bool Value::GetAsString(string16
* out_value
) const {
119 bool Value::GetAsString(const StringValue
** out_value
) const {
123 bool Value::GetAsList(ListValue
** out_value
) {
127 bool Value::GetAsList(const ListValue
** out_value
) const {
131 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
135 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
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
);
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;
164 Value::Value(Type type
) : type_(type
) {}
166 Value::Value(const Value
& that
) : type_(that
.type_
) {}
168 Value
& Value::operator=(const Value
& that
) {
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";
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 {
218 return new FundamentalValue(boolean_value_
);
221 return new FundamentalValue(integer_value_
);
224 return new FundamentalValue(double_value_
);
232 bool FundamentalValue::Equals(const Value
* other
) const {
233 if (other
->GetType() != GetType())
239 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
243 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
247 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
255 ///////////////////// StringValue ////////////////////
257 StringValue::StringValue(const std::string
& in_value
)
258 : Value(TYPE_STRING
),
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() {
275 const std::string
& StringValue::GetString() const {
279 bool StringValue::GetAsString(std::string
* out_value
) const {
285 bool StringValue::GetAsString(string16
* out_value
) const {
287 *out_value
= UTF8ToUTF16(value_
);
291 bool StringValue::GetAsString(const StringValue
** out_value
) const {
297 StringValue
* StringValue::DeepCopy() const {
298 return new StringValue(value_
);
301 bool StringValue::Equals(const Value
* other
) const {
302 if (other
->GetType() != GetType())
304 std::string lhs
, rhs
;
305 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
308 ///////////////////// BinaryValue ////////////////////
310 BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY
),
315 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
316 : Value(TYPE_BINARY
),
317 buffer_(buffer
.Pass()),
321 BinaryValue::~BinaryValue() {
325 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
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 {
339 BinaryValue
* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
343 bool BinaryValue::Equals(const Value
* other
) const {
344 if (other
->GetType() != GetType())
346 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
347 if (other_binary
->size_
!= size_
)
349 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
352 ///////////////////// DictionaryValue ////////////////////
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
);
364 DictionaryValue::DictionaryValue()
365 : Value(TYPE_DICTIONARY
) {
368 DictionaryValue::~DictionaryValue() {
372 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
378 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
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
;
401 void DictionaryValue::Set(const std::string
& path
, scoped_ptr
<Value
> in_value
) {
402 DCHECK(IsStringUTF8(path
));
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
,
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
)) {
510 current_dictionary
= child_dictionary
;
511 current_path
= current_path
.substr(delimiter_position
+ 1);
514 return current_dictionary
->GetWithoutPathExpansion(current_path
.as_string(),
518 bool DictionaryValue::Get(StringPiece path
, Value
** out_value
) {
519 return static_cast<const DictionaryValue
&>(*this).Get(
521 const_cast<const Value
**>(out_value
));
524 bool DictionaryValue::GetBoolean(const std::string
& path
,
525 bool* bool_value
) const {
527 if (!Get(path
, &value
))
530 return value
->GetAsBoolean(bool_value
);
533 bool DictionaryValue::GetInteger(const std::string
& path
,
534 int* out_value
) const {
536 if (!Get(path
, &value
))
539 return value
->GetAsInteger(out_value
);
542 bool DictionaryValue::GetDouble(const std::string
& path
,
543 double* out_value
) const {
545 if (!Get(path
, &value
))
548 return value
->GetAsDouble(out_value
);
551 bool DictionaryValue::GetString(const std::string
& path
,
552 std::string
* out_value
) const {
554 if (!Get(path
, &value
))
557 return value
->GetAsString(out_value
);
560 bool DictionaryValue::GetString(const std::string
& path
,
561 string16
* out_value
) const {
563 if (!Get(path
, &value
))
566 return value
->GetAsString(out_value
);
569 bool DictionaryValue::GetStringASCII(const std::string
& path
,
570 std::string
* out_value
) const {
572 if (!GetString(path
, &out
))
575 if (!IsStringASCII(out
)) {
580 out_value
->assign(out
);
584 bool DictionaryValue::GetBinary(const std::string
& path
,
585 const BinaryValue
** out_value
) const {
587 bool result
= Get(path
, &value
);
588 if (!result
|| !value
->IsType(TYPE_BINARY
))
592 *out_value
= static_cast<const BinaryValue
*>(value
);
597 bool DictionaryValue::GetBinary(const std::string
& path
,
598 BinaryValue
** out_value
) {
599 return static_cast<const DictionaryValue
&>(*this).GetBinary(
601 const_cast<const BinaryValue
**>(out_value
));
604 bool DictionaryValue::GetDictionary(StringPiece path
,
605 const DictionaryValue
** out_value
) const {
607 bool result
= Get(path
, &value
);
608 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
612 *out_value
= static_cast<const DictionaryValue
*>(value
);
617 bool DictionaryValue::GetDictionary(StringPiece path
,
618 DictionaryValue
** out_value
) {
619 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
621 const_cast<const DictionaryValue
**>(out_value
));
624 bool DictionaryValue::GetList(const std::string
& path
,
625 const ListValue
** out_value
) const {
627 bool result
= Get(path
, &value
);
628 if (!result
|| !value
->IsType(TYPE_LIST
))
632 *out_value
= static_cast<const ListValue
*>(value
);
637 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
638 return static_cast<const DictionaryValue
&>(*this).GetList(
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())
650 const Value
* entry
= entry_iterator
->second
;
656 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
658 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
660 const_cast<const Value
**>(out_value
));
663 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
664 bool* out_value
) const {
666 if (!GetWithoutPathExpansion(key
, &value
))
669 return value
->GetAsBoolean(out_value
);
672 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
673 int* out_value
) const {
675 if (!GetWithoutPathExpansion(key
, &value
))
678 return value
->GetAsInteger(out_value
);
681 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
682 double* out_value
) const {
684 if (!GetWithoutPathExpansion(key
, &value
))
687 return value
->GetAsDouble(out_value
);
690 bool DictionaryValue::GetStringWithoutPathExpansion(
691 const std::string
& key
,
692 std::string
* out_value
) const {
694 if (!GetWithoutPathExpansion(key
, &value
))
697 return value
->GetAsString(out_value
);
700 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
701 string16
* out_value
) const {
703 if (!GetWithoutPathExpansion(key
, &value
))
706 return value
->GetAsString(out_value
);
709 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
710 const std::string
& key
,
711 const DictionaryValue
** out_value
) const {
713 bool result
= GetWithoutPathExpansion(key
, &value
);
714 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
718 *out_value
= static_cast<const DictionaryValue
*>(value
);
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(
730 const_cast<const DictionaryValue
**>(out_value
));
733 bool DictionaryValue::GetListWithoutPathExpansion(
734 const std::string
& key
,
735 const ListValue
** out_value
) const {
737 bool result
= GetWithoutPathExpansion(key
, &value
);
738 if (!result
|| !value
->IsType(TYPE_LIST
))
742 *out_value
= static_cast<const ListValue
*>(value
);
747 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
748 ListValue
** out_value
) {
750 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
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 ¤t_dictionary
))
765 current_path
.erase(0, delimiter_position
+ 1);
768 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
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())
779 Value
* entry
= entry_iterator
->second
;
781 out_value
->reset(entry
);
784 dictionary_
.erase(entry_iterator
);
788 bool DictionaryValue::RemovePath(const std::string
& path
,
789 scoped_ptr
<Value
>* out_value
) {
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
))
800 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
802 if (result
&& subdict
->empty())
803 RemoveWithoutPathExpansion(subdict_path
, NULL
);
808 scoped_ptr
<DictionaryValue
> DictionaryValue::DeepCopyWithoutEmptyChildren()
810 scoped_ptr
<DictionaryValue
> copy
= CopyDictionaryWithoutEmptyChildren(*this);
812 copy
.reset(new DictionaryValue
);
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
));
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
)
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());
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())
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())) {
875 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
881 ///////////////////// ListValue ////////////////////
884 scoped_ptr
<ListValue
> ListValue::From(scoped_ptr
<Value
> value
) {
886 if (value
&& value
->GetAsList(&out
)) {
887 ignore_result(value
.release());
888 return make_scoped_ptr(out
);
893 ListValue::ListValue() : Value(TYPE_LIST
) {
896 ListValue::~ListValue() {
900 void ListValue::Clear() {
901 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
906 bool ListValue::Set(size_t index
, Value
* in_value
) {
910 if (index
>= list_
.size()) {
911 // Pad out any intermediate indexes with null settings
912 while (index
> list_
.size())
913 Append(CreateNullValue());
916 DCHECK(list_
[index
] != in_value
);
918 list_
[index
] = in_value
;
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())
932 *out_value
= list_
[index
];
937 bool ListValue::Get(size_t index
, Value
** out_value
) {
938 return static_cast<const ListValue
&>(*this).Get(
940 const_cast<const Value
**>(out_value
));
943 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
945 if (!Get(index
, &value
))
948 return value
->GetAsBoolean(bool_value
);
951 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
953 if (!Get(index
, &value
))
956 return value
->GetAsInteger(out_value
);
959 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
961 if (!Get(index
, &value
))
964 return value
->GetAsDouble(out_value
);
967 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
969 if (!Get(index
, &value
))
972 return value
->GetAsString(out_value
);
975 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
977 if (!Get(index
, &value
))
980 return value
->GetAsString(out_value
);
983 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
985 bool result
= Get(index
, &value
);
986 if (!result
|| !value
->IsType(TYPE_BINARY
))
990 *out_value
= static_cast<const BinaryValue
*>(value
);
995 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
996 return static_cast<const ListValue
&>(*this).GetBinary(
998 const_cast<const BinaryValue
**>(out_value
));
1001 bool ListValue::GetDictionary(size_t index
,
1002 const DictionaryValue
** out_value
) const {
1004 bool result
= Get(index
, &value
);
1005 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
1009 *out_value
= static_cast<const DictionaryValue
*>(value
);
1014 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
1015 return static_cast<const ListValue
&>(*this).GetDictionary(
1017 const_cast<const DictionaryValue
**>(out_value
));
1020 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
1022 bool result
= Get(index
, &value
);
1023 if (!result
|| !value
->IsType(TYPE_LIST
))
1027 *out_value
= static_cast<const ListValue
*>(value
);
1032 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
1033 return static_cast<const ListValue
&>(*this).GetList(
1035 const_cast<const ListValue
**>(out_value
));
1038 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
1039 if (index
>= list_
.size())
1043 out_value
->reset(list_
[index
]);
1045 delete list_
[index
];
1047 list_
.erase(list_
.begin() + index
);
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();
1059 *index
= previous_index
;
1066 ListValue::iterator
ListValue::Erase(iterator iter
,
1067 scoped_ptr
<Value
>* out_value
) {
1069 out_value
->reset(*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
) {
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
) {
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
) {
1119 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1121 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1122 if ((*i
)->Equals(in_value
)) {
1127 list_
.push_back(in_value
);
1131 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1133 if (index
> list_
.size())
1136 list_
.insert(list_
.begin() + index
, in_value
);
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
) {
1154 bool ListValue::GetAsList(const ListValue
** out_value
) const {
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());
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())
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
))
1186 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1192 ValueSerializer::~ValueSerializer() {
1195 ValueDeserializer::~ValueDeserializer() {
1198 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1200 JSONWriter::WriteWithOptions(value
, JSONWriter::OPTIONS_PRETTY_PRINT
, &json
);