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"
10 #include "base/float_util.h"
11 #include "base/json/json_writer.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
20 // Make a deep copy of |node|, but don't include empty lists or dictionaries
21 // in the copy. It's possible for this function to return NULL and it
22 // expects |node| to always be non-NULL.
23 Value
* CopyWithoutEmptyChildren(const Value
* node
) {
25 switch (node
->GetType()) {
26 case Value::TYPE_LIST
: {
27 const ListValue
* list
= static_cast<const ListValue
*>(node
);
28 ListValue
* copy
= new ListValue
;
29 for (ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
31 Value
* child_copy
= CopyWithoutEmptyChildren(*it
);
33 copy
->Append(child_copy
);
42 case Value::TYPE_DICTIONARY
: {
43 const DictionaryValue
* dict
= static_cast<const DictionaryValue
*>(node
);
44 DictionaryValue
* copy
= new DictionaryValue
;
45 for (DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd(); it
.Advance()) {
46 Value
* child_copy
= CopyWithoutEmptyChildren(&it
.value());
48 copy
->SetWithoutPathExpansion(it
.key(), child_copy
);
58 // For everything else, just make a copy.
59 return node
->DeepCopy();
63 // A small functor for comparing Values for std::find_if and similar.
66 // Pass the value against which all consecutive calls of the () operator will
67 // compare their argument to. This Value object must not be destroyed while
68 // the ValueEquals is in use.
69 explicit ValueEquals(const Value
* first
) : first_(first
) { }
71 bool operator ()(const Value
* second
) const {
72 return first_
->Equals(second
);
85 Value
* Value::CreateNullValue() {
86 return new Value(TYPE_NULL
);
90 FundamentalValue
* Value::CreateBooleanValue(bool in_value
) {
91 return new FundamentalValue(in_value
);
95 FundamentalValue
* Value::CreateIntegerValue(int in_value
) {
96 return new FundamentalValue(in_value
);
100 FundamentalValue
* Value::CreateDoubleValue(double in_value
) {
101 return new FundamentalValue(in_value
);
105 StringValue
* Value::CreateStringValue(const std::string
& in_value
) {
106 return new StringValue(in_value
);
110 StringValue
* Value::CreateStringValue(const string16
& in_value
) {
111 return new StringValue(in_value
);
114 bool Value::GetAsBoolean(bool* out_value
) const {
118 bool Value::GetAsInteger(int* out_value
) const {
122 bool Value::GetAsDouble(double* out_value
) const {
126 bool Value::GetAsString(std::string
* out_value
) const {
130 bool Value::GetAsString(string16
* out_value
) const {
134 bool Value::GetAsList(ListValue
** out_value
) {
138 bool Value::GetAsList(const ListValue
** out_value
) const {
142 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
146 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
150 Value
* Value::DeepCopy() 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 CreateNullValue();
157 bool Value::Equals(const Value
* other
) const {
158 // This method should only be getting called for null Values--all subclasses
159 // need to provide their own implementation;.
160 DCHECK(IsType(TYPE_NULL
));
161 return other
->IsType(TYPE_NULL
);
165 bool Value::Equals(const Value
* a
, const Value
* b
) {
166 if ((a
== NULL
) && (b
== NULL
)) return true;
167 if ((a
== NULL
) ^ (b
== NULL
)) return false;
171 Value::Value(Type type
) : type_(type
) {}
173 Value::Value(const Value
& that
) : type_(that
.type_
) {}
175 Value
& Value::operator=(const Value
& that
) {
180 ///////////////////// FundamentalValue ////////////////////
182 FundamentalValue::FundamentalValue(bool in_value
)
183 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
186 FundamentalValue::FundamentalValue(int in_value
)
187 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
190 FundamentalValue::FundamentalValue(double in_value
)
191 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
192 if (!IsFinite(double_value_
)) {
193 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
194 << "values cannot be represented in JSON";
199 FundamentalValue::~FundamentalValue() {
202 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
203 if (out_value
&& IsType(TYPE_BOOLEAN
))
204 *out_value
= boolean_value_
;
205 return (IsType(TYPE_BOOLEAN
));
208 bool FundamentalValue::GetAsInteger(int* out_value
) const {
209 if (out_value
&& IsType(TYPE_INTEGER
))
210 *out_value
= integer_value_
;
211 return (IsType(TYPE_INTEGER
));
214 bool FundamentalValue::GetAsDouble(double* out_value
) const {
215 if (out_value
&& IsType(TYPE_DOUBLE
))
216 *out_value
= double_value_
;
217 else if (out_value
&& IsType(TYPE_INTEGER
))
218 *out_value
= integer_value_
;
219 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
222 FundamentalValue
* FundamentalValue::DeepCopy() const {
225 return CreateBooleanValue(boolean_value_
);
228 return CreateIntegerValue(integer_value_
);
231 return CreateDoubleValue(double_value_
);
239 bool FundamentalValue::Equals(const Value
* other
) const {
240 if (other
->GetType() != GetType())
246 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
250 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
254 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
262 ///////////////////// StringValue ////////////////////
264 StringValue::StringValue(const std::string
& in_value
)
265 : Value(TYPE_STRING
),
267 DCHECK(IsStringUTF8(in_value
));
270 StringValue::StringValue(const string16
& in_value
)
271 : Value(TYPE_STRING
),
272 value_(UTF16ToUTF8(in_value
)) {
275 StringValue::~StringValue() {
278 bool StringValue::GetAsString(std::string
* out_value
) const {
284 bool StringValue::GetAsString(string16
* out_value
) const {
286 *out_value
= UTF8ToUTF16(value_
);
290 StringValue
* StringValue::DeepCopy() const {
291 return CreateStringValue(value_
);
294 bool StringValue::Equals(const Value
* other
) const {
295 if (other
->GetType() != GetType())
297 std::string lhs
, rhs
;
298 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
301 ///////////////////// BinaryValue ////////////////////
303 BinaryValue::BinaryValue()
304 : Value(TYPE_BINARY
),
308 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
309 : Value(TYPE_BINARY
),
310 buffer_(buffer
.Pass()),
314 BinaryValue::~BinaryValue() {
318 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
320 char* buffer_copy
= new char[size
];
321 memcpy(buffer_copy
, buffer
, size
);
322 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
323 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
326 BinaryValue
* BinaryValue::DeepCopy() const {
327 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
330 bool BinaryValue::Equals(const Value
* other
) const {
331 if (other
->GetType() != GetType())
333 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
334 if (other_binary
->size_
!= size_
)
336 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
339 ///////////////////// DictionaryValue ////////////////////
341 DictionaryValue::DictionaryValue()
342 : Value(TYPE_DICTIONARY
) {
345 DictionaryValue::~DictionaryValue() {
349 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
355 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
361 bool DictionaryValue::HasKey(const std::string
& key
) const {
362 DCHECK(IsStringUTF8(key
));
363 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
364 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
365 return current_entry
!= dictionary_
.end();
368 void DictionaryValue::Clear() {
369 ValueMap::iterator dict_iterator
= dictionary_
.begin();
370 while (dict_iterator
!= dictionary_
.end()) {
371 delete dict_iterator
->second
;
378 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
379 DCHECK(IsStringUTF8(path
));
382 std::string
current_path(path
);
383 DictionaryValue
* current_dictionary
= this;
384 for (size_t delimiter_position
= current_path
.find('.');
385 delimiter_position
!= std::string::npos
;
386 delimiter_position
= current_path
.find('.')) {
387 // Assume that we're indexing into a dictionary.
388 std::string
key(current_path
, 0, delimiter_position
);
389 DictionaryValue
* child_dictionary
= NULL
;
390 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
391 child_dictionary
= new DictionaryValue
;
392 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
395 current_dictionary
= child_dictionary
;
396 current_path
.erase(0, delimiter_position
+ 1);
399 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
402 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
403 Set(path
, CreateBooleanValue(in_value
));
406 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
407 Set(path
, CreateIntegerValue(in_value
));
410 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
411 Set(path
, CreateDoubleValue(in_value
));
414 void DictionaryValue::SetString(const std::string
& path
,
415 const std::string
& in_value
) {
416 Set(path
, CreateStringValue(in_value
));
419 void DictionaryValue::SetString(const std::string
& path
,
420 const string16
& in_value
) {
421 Set(path
, CreateStringValue(in_value
));
424 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
426 // If there's an existing value here, we need to delete it, because
427 // we own all our children.
428 std::pair
<ValueMap::iterator
, bool> ins_res
=
429 dictionary_
.insert(std::make_pair(key
, in_value
));
430 if (!ins_res
.second
) {
431 DCHECK_NE(ins_res
.first
->second
, in_value
); // This would be bogus
432 delete ins_res
.first
->second
;
433 ins_res
.first
->second
= in_value
;
437 void DictionaryValue::SetBooleanWithoutPathExpansion(
438 const std::string
& path
, bool in_value
) {
439 SetWithoutPathExpansion(path
, CreateBooleanValue(in_value
));
442 void DictionaryValue::SetIntegerWithoutPathExpansion(
443 const std::string
& path
, int in_value
) {
444 SetWithoutPathExpansion(path
, CreateIntegerValue(in_value
));
447 void DictionaryValue::SetDoubleWithoutPathExpansion(
448 const std::string
& path
, double in_value
) {
449 SetWithoutPathExpansion(path
, CreateDoubleValue(in_value
));
452 void DictionaryValue::SetStringWithoutPathExpansion(
453 const std::string
& path
, const std::string
& in_value
) {
454 SetWithoutPathExpansion(path
, CreateStringValue(in_value
));
457 void DictionaryValue::SetStringWithoutPathExpansion(
458 const std::string
& path
, const string16
& in_value
) {
459 SetWithoutPathExpansion(path
, CreateStringValue(in_value
));
462 bool DictionaryValue::Get(
463 const std::string
& path
, const Value
** out_value
) const {
464 DCHECK(IsStringUTF8(path
));
465 // LOG(WARNING) << "\n1\n";
466 std::string
current_path(path
);
467 const DictionaryValue
* current_dictionary
= this;
468 // LOG(WARNING) << "\n2\n";
469 for (size_t delimiter_position
= current_path
.find('.');
470 delimiter_position
!= std::string::npos
;
471 delimiter_position
= current_path
.find('.')) {
472 const DictionaryValue
* child_dictionary
= NULL
;
473 if (!current_dictionary
->GetDictionary(
474 current_path
.substr(0, delimiter_position
), &child_dictionary
))
477 current_dictionary
= child_dictionary
;
478 current_path
.erase(0, delimiter_position
+ 1);
480 // LOG(WARNING) << "\n3\n";
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 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() {
756 Value
* copy
= CopyWithoutEmptyChildren(this);
757 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
760 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
761 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
762 const Value
* merge_value
= &it
.value();
763 // Check whether we have to merge dictionaries.
764 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
765 DictionaryValue
* sub_dict
;
766 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
767 sub_dict
->MergeDictionary(
768 static_cast<const DictionaryValue
*>(merge_value
));
772 // All other cases: Make a copy and hook it up.
773 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
777 void DictionaryValue::Swap(DictionaryValue
* other
) {
778 dictionary_
.swap(other
->dictionary_
);
781 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
783 it_(target
.dictionary_
.begin()) {}
785 DictionaryValue
* DictionaryValue::DeepCopy() const {
786 DictionaryValue
* result
= new DictionaryValue
;
788 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
789 current_entry
!= dictionary_
.end(); ++current_entry
) {
790 result
->SetWithoutPathExpansion(current_entry
->first
,
791 current_entry
->second
->DeepCopy());
797 bool DictionaryValue::Equals(const Value
* other
) const {
798 if (other
->GetType() != GetType())
801 const DictionaryValue
* other_dict
=
802 static_cast<const DictionaryValue
*>(other
);
803 Iterator
lhs_it(*this);
804 Iterator
rhs_it(*other_dict
);
805 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
806 if (lhs_it
.key() != rhs_it
.key() ||
807 !lhs_it
.value().Equals(&rhs_it
.value())) {
813 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
819 ///////////////////// ListValue ////////////////////
821 ListValue::ListValue() : Value(TYPE_LIST
) {
824 ListValue::~ListValue() {
828 void ListValue::Clear() {
829 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
834 bool ListValue::Set(size_t index
, Value
* in_value
) {
838 if (index
>= list_
.size()) {
839 // Pad out any intermediate indexes with null settings
840 while (index
> list_
.size())
841 Append(CreateNullValue());
844 DCHECK(list_
[index
] != in_value
);
846 list_
[index
] = in_value
;
851 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
852 if (index
>= list_
.size())
856 *out_value
= list_
[index
];
861 bool ListValue::Get(size_t index
, Value
** out_value
) {
862 return static_cast<const ListValue
&>(*this).Get(
864 const_cast<const Value
**>(out_value
));
867 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
869 if (!Get(index
, &value
))
872 return value
->GetAsBoolean(bool_value
);
875 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
877 if (!Get(index
, &value
))
880 return value
->GetAsInteger(out_value
);
883 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
885 if (!Get(index
, &value
))
888 return value
->GetAsDouble(out_value
);
891 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
893 if (!Get(index
, &value
))
896 return value
->GetAsString(out_value
);
899 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
901 if (!Get(index
, &value
))
904 return value
->GetAsString(out_value
);
907 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
909 bool result
= Get(index
, &value
);
910 if (!result
|| !value
->IsType(TYPE_BINARY
))
914 *out_value
= static_cast<const BinaryValue
*>(value
);
919 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
920 return static_cast<const ListValue
&>(*this).GetBinary(
922 const_cast<const BinaryValue
**>(out_value
));
925 bool ListValue::GetDictionary(size_t index
,
926 const DictionaryValue
** out_value
) const {
928 bool result
= Get(index
, &value
);
929 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
933 *out_value
= static_cast<const DictionaryValue
*>(value
);
938 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
939 return static_cast<const ListValue
&>(*this).GetDictionary(
941 const_cast<const DictionaryValue
**>(out_value
));
944 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
946 bool result
= Get(index
, &value
);
947 if (!result
|| !value
->IsType(TYPE_LIST
))
951 *out_value
= static_cast<const ListValue
*>(value
);
956 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
957 return static_cast<const ListValue
&>(*this).GetList(
959 const_cast<const ListValue
**>(out_value
));
962 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
963 if (index
>= list_
.size())
967 out_value
->reset(list_
[index
]);
971 list_
.erase(list_
.begin() + index
);
975 bool ListValue::Remove(const Value
& value
, size_t* index
) {
976 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
977 if ((*i
)->Equals(&value
)) {
978 size_t previous_index
= i
- list_
.begin();
983 *index
= previous_index
;
990 ListValue::iterator
ListValue::Erase(iterator iter
,
991 scoped_ptr
<Value
>* out_value
) {
993 out_value
->reset(*iter
);
997 return list_
.erase(iter
);
1000 void ListValue::Append(Value
* in_value
) {
1002 list_
.push_back(in_value
);
1005 void ListValue::AppendBoolean(bool in_value
) {
1006 Append(CreateBooleanValue(in_value
));
1009 void ListValue::AppendInteger(int in_value
) {
1010 Append(CreateIntegerValue(in_value
));
1013 void ListValue::AppendDouble(double in_value
) {
1014 Append(CreateDoubleValue(in_value
));
1017 void ListValue::AppendString(const std::string
& in_value
) {
1018 Append(CreateStringValue(in_value
));
1021 void ListValue::AppendString(const string16
& in_value
) {
1022 Append(CreateStringValue(in_value
));
1025 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1026 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1027 it
!= in_values
.end(); ++it
) {
1032 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1033 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1034 it
!= in_values
.end(); ++it
) {
1039 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1041 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1042 if ((*i
)->Equals(in_value
)) {
1047 list_
.push_back(in_value
);
1051 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1053 if (index
> list_
.size())
1056 list_
.insert(list_
.begin() + index
, in_value
);
1060 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1061 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1064 void ListValue::Swap(ListValue
* other
) {
1065 list_
.swap(other
->list_
);
1068 bool ListValue::GetAsList(ListValue
** out_value
) {
1074 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1080 ListValue
* ListValue::DeepCopy() const {
1081 ListValue
* result
= new ListValue
;
1083 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1084 result
->Append((*i
)->DeepCopy());
1089 bool ListValue::Equals(const Value
* other
) const {
1090 if (other
->GetType() != GetType())
1093 const ListValue
* other_list
=
1094 static_cast<const ListValue
*>(other
);
1095 const_iterator lhs_it
, rhs_it
;
1096 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1097 lhs_it
!= end() && rhs_it
!= other_list
->end();
1098 ++lhs_it
, ++rhs_it
) {
1099 if (!(*lhs_it
)->Equals(*rhs_it
))
1102 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1108 ValueSerializer::~ValueSerializer() {
1111 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1113 JSONWriter::WriteWithOptions(&value
,
1114 JSONWriter::OPTIONS_PRETTY_PRINT
,