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"
12 #include "base/float_util.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 // Make a deep copy of |node|, but don't include empty lists or dictionaries
24 // in the copy. It's possible for this function to return NULL and it
25 // expects |node| to always be non-NULL.
26 Value
* CopyWithoutEmptyChildren(const Value
* node
) {
28 switch (node
->GetType()) {
29 case Value::TYPE_LIST
: {
30 const ListValue
* list
= static_cast<const ListValue
*>(node
);
31 ListValue
* copy
= new ListValue
;
32 for (ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
34 Value
* child_copy
= CopyWithoutEmptyChildren(*it
);
36 copy
->Append(child_copy
);
45 case Value::TYPE_DICTIONARY
: {
46 const DictionaryValue
* dict
= static_cast<const DictionaryValue
*>(node
);
47 DictionaryValue
* copy
= new DictionaryValue
;
48 for (DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd(); it
.Advance()) {
49 Value
* child_copy
= CopyWithoutEmptyChildren(&it
.value());
51 copy
->SetWithoutPathExpansion(it
.key(), child_copy
);
61 // For everything else, just make a copy.
62 return node
->DeepCopy();
66 // A small functor for comparing Values for std::find_if and similar.
69 // Pass the value against which all consecutive calls of the () operator will
70 // compare their argument to. This Value object must not be destroyed while
71 // the ValueEquals is in use.
72 explicit ValueEquals(const Value
* first
) : first_(first
) { }
74 bool operator ()(const Value
* second
) const {
75 return first_
->Equals(second
);
88 Value
* Value::CreateNullValue() {
89 return new Value(TYPE_NULL
);
93 FundamentalValue
* Value::CreateBooleanValue(bool in_value
) {
94 return new FundamentalValue(in_value
);
98 FundamentalValue
* Value::CreateIntegerValue(int in_value
) {
99 return new FundamentalValue(in_value
);
103 FundamentalValue
* Value::CreateDoubleValue(double in_value
) {
104 return new FundamentalValue(in_value
);
108 StringValue
* Value::CreateStringValue(const std::string
& in_value
) {
109 return new StringValue(in_value
);
113 StringValue
* Value::CreateStringValue(const string16
& in_value
) {
114 return new StringValue(in_value
);
117 bool Value::GetAsBoolean(bool* out_value
) const {
121 bool Value::GetAsInteger(int* out_value
) const {
125 bool Value::GetAsDouble(double* out_value
) const {
129 bool Value::GetAsString(std::string
* out_value
) const {
133 bool Value::GetAsString(string16
* out_value
) const {
137 bool Value::GetAsList(ListValue
** out_value
) {
141 bool Value::GetAsList(const ListValue
** out_value
) const {
145 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
149 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
153 Value
* Value::DeepCopy() const {
154 // This method should only be getting called for null Values--all subclasses
155 // need to provide their own implementation;.
156 DCHECK(IsType(TYPE_NULL
));
157 return CreateNullValue();
160 bool Value::Equals(const Value
* other
) const {
161 // This method should only be getting called for null Values--all subclasses
162 // need to provide their own implementation;.
163 DCHECK(IsType(TYPE_NULL
));
164 return other
->IsType(TYPE_NULL
);
168 bool Value::Equals(const Value
* a
, const Value
* b
) {
169 if ((a
== NULL
) && (b
== NULL
)) return true;
170 if ((a
== NULL
) ^ (b
== NULL
)) return false;
174 Value::Value(Type type
) : type_(type
) {}
176 Value::Value(const Value
& that
) : type_(that
.type_
) {}
178 Value
& Value::operator=(const Value
& that
) {
183 ///////////////////// FundamentalValue ////////////////////
185 FundamentalValue::FundamentalValue(bool in_value
)
186 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
189 FundamentalValue::FundamentalValue(int in_value
)
190 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
193 FundamentalValue::FundamentalValue(double in_value
)
194 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
195 if (!IsFinite(double_value_
)) {
196 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
197 << "values cannot be represented in JSON";
202 FundamentalValue::~FundamentalValue() {
205 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
206 if (out_value
&& IsType(TYPE_BOOLEAN
))
207 *out_value
= boolean_value_
;
208 return (IsType(TYPE_BOOLEAN
));
211 bool FundamentalValue::GetAsInteger(int* out_value
) const {
212 if (out_value
&& IsType(TYPE_INTEGER
))
213 *out_value
= integer_value_
;
214 return (IsType(TYPE_INTEGER
));
217 bool FundamentalValue::GetAsDouble(double* out_value
) const {
218 if (out_value
&& IsType(TYPE_DOUBLE
))
219 *out_value
= double_value_
;
220 else if (out_value
&& IsType(TYPE_INTEGER
))
221 *out_value
= integer_value_
;
222 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
225 FundamentalValue
* FundamentalValue::DeepCopy() const {
228 return CreateBooleanValue(boolean_value_
);
231 return CreateIntegerValue(integer_value_
);
234 return CreateDoubleValue(double_value_
);
242 bool FundamentalValue::Equals(const Value
* other
) const {
243 if (other
->GetType() != GetType())
249 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
253 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
257 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
265 ///////////////////// StringValue ////////////////////
267 StringValue::StringValue(const std::string
& in_value
)
268 : Value(TYPE_STRING
),
270 DCHECK(IsStringUTF8(in_value
));
273 StringValue::StringValue(const string16
& in_value
)
274 : Value(TYPE_STRING
),
275 value_(UTF16ToUTF8(in_value
)) {
278 StringValue::~StringValue() {
281 bool StringValue::GetAsString(std::string
* out_value
) const {
287 bool StringValue::GetAsString(string16
* out_value
) const {
289 *out_value
= UTF8ToUTF16(value_
);
293 StringValue
* StringValue::DeepCopy() const {
294 return CreateStringValue(value_
);
297 bool StringValue::Equals(const Value
* other
) const {
298 if (other
->GetType() != GetType())
300 std::string lhs
, rhs
;
301 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
304 ///////////////////// BinaryValue ////////////////////
306 BinaryValue::BinaryValue()
307 : Value(TYPE_BINARY
),
311 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
312 : Value(TYPE_BINARY
),
313 buffer_(buffer
.Pass()),
317 BinaryValue::~BinaryValue() {
321 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
323 char* buffer_copy
= new char[size
];
324 memcpy(buffer_copy
, buffer
, size
);
325 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
326 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
329 BinaryValue
* BinaryValue::DeepCopy() const {
330 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
333 bool BinaryValue::Equals(const Value
* other
) const {
334 if (other
->GetType() != GetType())
336 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
337 if (other_binary
->size_
!= size_
)
339 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
342 ///////////////////// DictionaryValue ////////////////////
344 DictionaryValue::DictionaryValue()
345 : Value(TYPE_DICTIONARY
) {
348 DictionaryValue::~DictionaryValue() {
352 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
358 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
364 bool DictionaryValue::HasKey(const std::string
& key
) const {
365 DCHECK(IsStringUTF8(key
));
366 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
367 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
368 return current_entry
!= dictionary_
.end();
371 void DictionaryValue::Clear() {
372 ValueMap::iterator dict_iterator
= dictionary_
.begin();
373 while (dict_iterator
!= dictionary_
.end()) {
374 delete dict_iterator
->second
;
381 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
382 DCHECK(IsStringUTF8(path
));
385 std::string
current_path(path
);
386 DictionaryValue
* current_dictionary
= this;
387 for (size_t delimiter_position
= current_path
.find('.');
388 delimiter_position
!= std::string::npos
;
389 delimiter_position
= current_path
.find('.')) {
390 // Assume that we're indexing into a dictionary.
391 std::string
key(current_path
, 0, delimiter_position
);
392 DictionaryValue
* child_dictionary
= NULL
;
393 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
394 child_dictionary
= new DictionaryValue
;
395 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
398 current_dictionary
= child_dictionary
;
399 current_path
.erase(0, delimiter_position
+ 1);
402 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
405 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
406 Set(path
, CreateBooleanValue(in_value
));
409 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
410 Set(path
, CreateIntegerValue(in_value
));
413 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
414 Set(path
, CreateDoubleValue(in_value
));
417 void DictionaryValue::SetString(const std::string
& path
,
418 const std::string
& in_value
) {
419 Set(path
, CreateStringValue(in_value
));
422 void DictionaryValue::SetString(const std::string
& path
,
423 const string16
& in_value
) {
424 Set(path
, CreateStringValue(in_value
));
427 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
429 // If there's an existing value here, we need to delete it, because
430 // we own all our children.
431 std::pair
<ValueMap::iterator
, bool> ins_res
=
432 dictionary_
.insert(std::make_pair(key
, in_value
));
433 if (!ins_res
.second
) {
434 DCHECK_NE(ins_res
.first
->second
, in_value
); // This would be bogus
435 delete ins_res
.first
->second
;
436 ins_res
.first
->second
= in_value
;
440 void DictionaryValue::SetBooleanWithoutPathExpansion(
441 const std::string
& path
, bool in_value
) {
442 SetWithoutPathExpansion(path
, CreateBooleanValue(in_value
));
445 void DictionaryValue::SetIntegerWithoutPathExpansion(
446 const std::string
& path
, int in_value
) {
447 SetWithoutPathExpansion(path
, CreateIntegerValue(in_value
));
450 void DictionaryValue::SetDoubleWithoutPathExpansion(
451 const std::string
& path
, double in_value
) {
452 SetWithoutPathExpansion(path
, CreateDoubleValue(in_value
));
455 void DictionaryValue::SetStringWithoutPathExpansion(
456 const std::string
& path
, const std::string
& in_value
) {
457 SetWithoutPathExpansion(path
, CreateStringValue(in_value
));
460 void DictionaryValue::SetStringWithoutPathExpansion(
461 const std::string
& path
, const string16
& in_value
) {
462 SetWithoutPathExpansion(path
, CreateStringValue(in_value
));
465 bool DictionaryValue::Get(const std::string
& path
,
466 const Value
** out_value
) const {
467 DCHECK(IsStringUTF8(path
));
468 std::string
current_path(path
);
469 const DictionaryValue
* current_dictionary
= this;
470 for (size_t delimiter_position
= current_path
.find('.');
471 delimiter_position
!= std::string::npos
;
472 delimiter_position
= current_path
.find('.')) {
473 const DictionaryValue
* child_dictionary
= NULL
;
474 if (!current_dictionary
->GetDictionary(
475 current_path
.substr(0, delimiter_position
), &child_dictionary
))
478 current_dictionary
= child_dictionary
;
479 current_path
.erase(0, delimiter_position
+ 1);
482 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
485 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
486 return static_cast<const DictionaryValue
&>(*this).Get(
488 const_cast<const Value
**>(out_value
));
491 bool DictionaryValue::GetBoolean(const std::string
& path
,
492 bool* bool_value
) const {
494 if (!Get(path
, &value
))
497 return value
->GetAsBoolean(bool_value
);
500 bool DictionaryValue::GetInteger(const std::string
& path
,
501 int* out_value
) const {
503 if (!Get(path
, &value
))
506 return value
->GetAsInteger(out_value
);
509 bool DictionaryValue::GetDouble(const std::string
& path
,
510 double* out_value
) const {
512 if (!Get(path
, &value
))
515 return value
->GetAsDouble(out_value
);
518 bool DictionaryValue::GetString(const std::string
& path
,
519 std::string
* out_value
) const {
521 if (!Get(path
, &value
))
524 return value
->GetAsString(out_value
);
527 bool DictionaryValue::GetString(const std::string
& path
,
528 string16
* out_value
) const {
530 if (!Get(path
, &value
))
533 return value
->GetAsString(out_value
);
536 bool DictionaryValue::GetStringASCII(const std::string
& path
,
537 std::string
* out_value
) const {
539 if (!GetString(path
, &out
))
542 if (!IsStringASCII(out
)) {
547 out_value
->assign(out
);
551 bool DictionaryValue::GetBinary(const std::string
& path
,
552 const BinaryValue
** out_value
) const {
554 bool result
= Get(path
, &value
);
555 if (!result
|| !value
->IsType(TYPE_BINARY
))
559 *out_value
= static_cast<const BinaryValue
*>(value
);
564 bool DictionaryValue::GetBinary(const std::string
& path
,
565 BinaryValue
** out_value
) {
566 return static_cast<const DictionaryValue
&>(*this).GetBinary(
568 const_cast<const BinaryValue
**>(out_value
));
571 bool DictionaryValue::GetDictionary(const std::string
& path
,
572 const DictionaryValue
** out_value
) const {
574 bool result
= Get(path
, &value
);
575 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
579 *out_value
= static_cast<const DictionaryValue
*>(value
);
584 bool DictionaryValue::GetDictionary(const std::string
& path
,
585 DictionaryValue
** out_value
) {
586 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
588 const_cast<const DictionaryValue
**>(out_value
));
591 bool DictionaryValue::GetList(const std::string
& path
,
592 const ListValue
** out_value
) const {
594 bool result
= Get(path
, &value
);
595 if (!result
|| !value
->IsType(TYPE_LIST
))
599 *out_value
= static_cast<const ListValue
*>(value
);
604 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
605 return static_cast<const DictionaryValue
&>(*this).GetList(
607 const_cast<const ListValue
**>(out_value
));
610 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
611 const Value
** out_value
) const {
612 DCHECK(IsStringUTF8(key
));
613 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
614 if (entry_iterator
== dictionary_
.end())
617 const Value
* entry
= entry_iterator
->second
;
623 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
625 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
627 const_cast<const Value
**>(out_value
));
630 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
631 bool* out_value
) const {
633 if (!GetWithoutPathExpansion(key
, &value
))
636 return value
->GetAsBoolean(out_value
);
639 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
640 int* out_value
) const {
642 if (!GetWithoutPathExpansion(key
, &value
))
645 return value
->GetAsInteger(out_value
);
648 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
649 double* out_value
) const {
651 if (!GetWithoutPathExpansion(key
, &value
))
654 return value
->GetAsDouble(out_value
);
657 bool DictionaryValue::GetStringWithoutPathExpansion(
658 const std::string
& key
,
659 std::string
* out_value
) const {
661 if (!GetWithoutPathExpansion(key
, &value
))
664 return value
->GetAsString(out_value
);
667 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
668 string16
* out_value
) const {
670 if (!GetWithoutPathExpansion(key
, &value
))
673 return value
->GetAsString(out_value
);
676 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
677 const std::string
& key
,
678 const DictionaryValue
** out_value
) const {
680 bool result
= GetWithoutPathExpansion(key
, &value
);
681 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
685 *out_value
= static_cast<const DictionaryValue
*>(value
);
690 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
691 const std::string
& key
,
692 DictionaryValue
** out_value
) {
693 const DictionaryValue
& const_this
=
694 static_cast<const DictionaryValue
&>(*this);
695 return const_this
.GetDictionaryWithoutPathExpansion(
697 const_cast<const DictionaryValue
**>(out_value
));
700 bool DictionaryValue::GetListWithoutPathExpansion(
701 const std::string
& key
,
702 const ListValue
** out_value
) const {
704 bool result
= GetWithoutPathExpansion(key
, &value
);
705 if (!result
|| !value
->IsType(TYPE_LIST
))
709 *out_value
= static_cast<const ListValue
*>(value
);
714 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
715 ListValue
** out_value
) {
717 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
719 const_cast<const ListValue
**>(out_value
));
722 bool DictionaryValue::Remove(const std::string
& path
,
723 scoped_ptr
<Value
>* out_value
) {
724 DCHECK(IsStringUTF8(path
));
725 std::string
current_path(path
);
726 DictionaryValue
* current_dictionary
= this;
727 size_t delimiter_position
= current_path
.rfind('.');
728 if (delimiter_position
!= std::string::npos
) {
729 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
730 ¤t_dictionary
))
732 current_path
.erase(0, delimiter_position
+ 1);
735 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
739 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
740 scoped_ptr
<Value
>* out_value
) {
741 DCHECK(IsStringUTF8(key
));
742 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
743 if (entry_iterator
== dictionary_
.end())
746 Value
* entry
= entry_iterator
->second
;
748 out_value
->reset(entry
);
751 dictionary_
.erase(entry_iterator
);
755 bool DictionaryValue::RemovePath(const std::string
& path
,
756 scoped_ptr
<Value
>* out_value
) {
758 size_t delimiter_position
= path
.find('.');
760 if (delimiter_position
== std::string::npos
)
761 return RemoveWithoutPathExpansion(path
, out_value
);
763 const std::string subdict_path
= path
.substr(0, delimiter_position
);
764 DictionaryValue
* subdict
= NULL
;
765 if (!GetDictionary(subdict_path
, &subdict
))
767 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
769 if (result
&& subdict
->empty())
770 RemoveWithoutPathExpansion(subdict_path
, NULL
);
775 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
776 Value
* copy
= CopyWithoutEmptyChildren(this);
777 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
780 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
781 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
782 const Value
* merge_value
= &it
.value();
783 // Check whether we have to merge dictionaries.
784 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
785 DictionaryValue
* sub_dict
;
786 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
787 sub_dict
->MergeDictionary(
788 static_cast<const DictionaryValue
*>(merge_value
));
792 // All other cases: Make a copy and hook it up.
793 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
797 void DictionaryValue::Swap(DictionaryValue
* other
) {
798 dictionary_
.swap(other
->dictionary_
);
801 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
803 it_(target
.dictionary_
.begin()) {}
805 DictionaryValue::Iterator::~Iterator() {}
807 DictionaryValue
* DictionaryValue::DeepCopy() const {
808 DictionaryValue
* result
= new DictionaryValue
;
810 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
811 current_entry
!= dictionary_
.end(); ++current_entry
) {
812 result
->SetWithoutPathExpansion(current_entry
->first
,
813 current_entry
->second
->DeepCopy());
819 bool DictionaryValue::Equals(const Value
* other
) const {
820 if (other
->GetType() != GetType())
823 const DictionaryValue
* other_dict
=
824 static_cast<const DictionaryValue
*>(other
);
825 Iterator
lhs_it(*this);
826 Iterator
rhs_it(*other_dict
);
827 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
828 if (lhs_it
.key() != rhs_it
.key() ||
829 !lhs_it
.value().Equals(&rhs_it
.value())) {
835 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
841 ///////////////////// ListValue ////////////////////
843 ListValue::ListValue() : Value(TYPE_LIST
) {
846 ListValue::~ListValue() {
850 void ListValue::Clear() {
851 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
856 bool ListValue::Set(size_t index
, Value
* in_value
) {
860 if (index
>= list_
.size()) {
861 // Pad out any intermediate indexes with null settings
862 while (index
> list_
.size())
863 Append(CreateNullValue());
866 DCHECK(list_
[index
] != in_value
);
868 list_
[index
] = in_value
;
873 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
874 if (index
>= list_
.size())
878 *out_value
= list_
[index
];
883 bool ListValue::Get(size_t index
, Value
** out_value
) {
884 return static_cast<const ListValue
&>(*this).Get(
886 const_cast<const Value
**>(out_value
));
889 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
891 if (!Get(index
, &value
))
894 return value
->GetAsBoolean(bool_value
);
897 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
899 if (!Get(index
, &value
))
902 return value
->GetAsInteger(out_value
);
905 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
907 if (!Get(index
, &value
))
910 return value
->GetAsDouble(out_value
);
913 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
915 if (!Get(index
, &value
))
918 return value
->GetAsString(out_value
);
921 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
923 if (!Get(index
, &value
))
926 return value
->GetAsString(out_value
);
929 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
931 bool result
= Get(index
, &value
);
932 if (!result
|| !value
->IsType(TYPE_BINARY
))
936 *out_value
= static_cast<const BinaryValue
*>(value
);
941 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
942 return static_cast<const ListValue
&>(*this).GetBinary(
944 const_cast<const BinaryValue
**>(out_value
));
947 bool ListValue::GetDictionary(size_t index
,
948 const DictionaryValue
** out_value
) const {
950 bool result
= Get(index
, &value
);
951 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
955 *out_value
= static_cast<const DictionaryValue
*>(value
);
960 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
961 return static_cast<const ListValue
&>(*this).GetDictionary(
963 const_cast<const DictionaryValue
**>(out_value
));
966 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
968 bool result
= Get(index
, &value
);
969 if (!result
|| !value
->IsType(TYPE_LIST
))
973 *out_value
= static_cast<const ListValue
*>(value
);
978 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
979 return static_cast<const ListValue
&>(*this).GetList(
981 const_cast<const ListValue
**>(out_value
));
984 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
985 if (index
>= list_
.size())
989 out_value
->reset(list_
[index
]);
993 list_
.erase(list_
.begin() + index
);
997 bool ListValue::Remove(const Value
& value
, size_t* index
) {
998 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
999 if ((*i
)->Equals(&value
)) {
1000 size_t previous_index
= i
- list_
.begin();
1005 *index
= previous_index
;
1012 ListValue::iterator
ListValue::Erase(iterator iter
,
1013 scoped_ptr
<Value
>* out_value
) {
1015 out_value
->reset(*iter
);
1019 return list_
.erase(iter
);
1022 void ListValue::Append(Value
* in_value
) {
1024 list_
.push_back(in_value
);
1027 void ListValue::AppendBoolean(bool in_value
) {
1028 Append(CreateBooleanValue(in_value
));
1031 void ListValue::AppendInteger(int in_value
) {
1032 Append(CreateIntegerValue(in_value
));
1035 void ListValue::AppendDouble(double in_value
) {
1036 Append(CreateDoubleValue(in_value
));
1039 void ListValue::AppendString(const std::string
& in_value
) {
1040 Append(CreateStringValue(in_value
));
1043 void ListValue::AppendString(const string16
& in_value
) {
1044 Append(CreateStringValue(in_value
));
1047 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1048 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1049 it
!= in_values
.end(); ++it
) {
1054 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1055 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1056 it
!= in_values
.end(); ++it
) {
1061 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1063 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1064 if ((*i
)->Equals(in_value
)) {
1069 list_
.push_back(in_value
);
1073 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1075 if (index
> list_
.size())
1078 list_
.insert(list_
.begin() + index
, in_value
);
1082 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1083 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1086 void ListValue::Swap(ListValue
* other
) {
1087 list_
.swap(other
->list_
);
1090 bool ListValue::GetAsList(ListValue
** out_value
) {
1096 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1102 ListValue
* ListValue::DeepCopy() const {
1103 ListValue
* result
= new ListValue
;
1105 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1106 result
->Append((*i
)->DeepCopy());
1111 bool ListValue::Equals(const Value
* other
) const {
1112 if (other
->GetType() != GetType())
1115 const ListValue
* other_list
=
1116 static_cast<const ListValue
*>(other
);
1117 const_iterator lhs_it
, rhs_it
;
1118 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1119 lhs_it
!= end() && rhs_it
!= other_list
->end();
1120 ++lhs_it
, ++rhs_it
) {
1121 if (!(*lhs_it
)->Equals(*rhs_it
))
1124 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1130 ValueSerializer::~ValueSerializer() {
1133 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1135 JSONWriter::WriteWithOptions(&value
,
1136 JSONWriter::OPTIONS_PRETTY_PRINT
,