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 // 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
);
92 bool Value::GetAsBinary(const BinaryValue
** out_value
) const {
96 bool Value::GetAsBoolean(bool* out_value
) const {
100 bool Value::GetAsInteger(int* out_value
) const {
104 bool Value::GetAsDouble(double* out_value
) const {
108 bool Value::GetAsString(std::string
* out_value
) const {
112 bool Value::GetAsString(string16
* out_value
) const {
116 bool Value::GetAsString(const StringValue
** out_value
) const {
120 bool Value::GetAsList(ListValue
** out_value
) {
124 bool Value::GetAsList(const ListValue
** out_value
) const {
128 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
132 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
136 Value
* Value::DeepCopy() const {
137 // This method should only be getting called for null Values--all subclasses
138 // need to provide their own implementation;.
139 DCHECK(IsType(TYPE_NULL
));
140 return CreateNullValue();
143 bool Value::Equals(const Value
* other
) const {
144 // This method should only be getting called for null Values--all subclasses
145 // need to provide their own implementation;.
146 DCHECK(IsType(TYPE_NULL
));
147 return other
->IsType(TYPE_NULL
);
151 bool Value::Equals(const Value
* a
, const Value
* b
) {
152 if ((a
== NULL
) && (b
== NULL
)) return true;
153 if ((a
== NULL
) ^ (b
== NULL
)) return false;
157 Value::Value(Type type
) : type_(type
) {}
159 Value::Value(const Value
& that
) : type_(that
.type_
) {}
161 Value
& Value::operator=(const Value
& that
) {
166 ///////////////////// FundamentalValue ////////////////////
168 FundamentalValue::FundamentalValue(bool in_value
)
169 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
172 FundamentalValue::FundamentalValue(int in_value
)
173 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
176 FundamentalValue::FundamentalValue(double in_value
)
177 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
178 if (!std::isfinite(double_value_
)) {
179 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
180 << "values cannot be represented in JSON";
185 FundamentalValue::~FundamentalValue() {
188 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
189 if (out_value
&& IsType(TYPE_BOOLEAN
))
190 *out_value
= boolean_value_
;
191 return (IsType(TYPE_BOOLEAN
));
194 bool FundamentalValue::GetAsInteger(int* out_value
) const {
195 if (out_value
&& IsType(TYPE_INTEGER
))
196 *out_value
= integer_value_
;
197 return (IsType(TYPE_INTEGER
));
200 bool FundamentalValue::GetAsDouble(double* out_value
) const {
201 if (out_value
&& IsType(TYPE_DOUBLE
))
202 *out_value
= double_value_
;
203 else if (out_value
&& IsType(TYPE_INTEGER
))
204 *out_value
= integer_value_
;
205 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
208 FundamentalValue
* FundamentalValue::DeepCopy() const {
211 return new FundamentalValue(boolean_value_
);
214 return new FundamentalValue(integer_value_
);
217 return new FundamentalValue(double_value_
);
225 bool FundamentalValue::Equals(const Value
* other
) const {
226 if (other
->GetType() != GetType())
232 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
236 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
240 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
248 ///////////////////// StringValue ////////////////////
250 StringValue::StringValue(const std::string
& in_value
)
251 : Value(TYPE_STRING
),
253 DCHECK(IsStringUTF8(in_value
));
256 StringValue::StringValue(const string16
& in_value
)
257 : Value(TYPE_STRING
),
258 value_(UTF16ToUTF8(in_value
)) {
261 StringValue::~StringValue() {
264 std::string
* StringValue::GetString() {
268 const std::string
& StringValue::GetString() const {
272 bool StringValue::GetAsString(std::string
* out_value
) const {
278 bool StringValue::GetAsString(string16
* out_value
) const {
280 *out_value
= UTF8ToUTF16(value_
);
284 bool StringValue::GetAsString(const StringValue
** out_value
) const {
290 StringValue
* StringValue::DeepCopy() const {
291 return new StringValue(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 bool BinaryValue::GetAsBinary(const BinaryValue
** out_value
) const {
332 BinaryValue
* BinaryValue::DeepCopy() const {
333 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
336 bool BinaryValue::Equals(const Value
* other
) const {
337 if (other
->GetType() != GetType())
339 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
340 if (other_binary
->size_
!= size_
)
342 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
345 ///////////////////// DictionaryValue ////////////////////
347 DictionaryValue::DictionaryValue()
348 : Value(TYPE_DICTIONARY
) {
351 DictionaryValue::~DictionaryValue() {
355 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
361 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
367 bool DictionaryValue::HasKey(const std::string
& key
) const {
368 DCHECK(IsStringUTF8(key
));
369 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
370 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
371 return current_entry
!= dictionary_
.end();
374 void DictionaryValue::Clear() {
375 ValueMap::iterator dict_iterator
= dictionary_
.begin();
376 while (dict_iterator
!= dictionary_
.end()) {
377 delete dict_iterator
->second
;
384 void DictionaryValue::Set(const std::string
& path
, scoped_ptr
<Value
> in_value
) {
385 DCHECK(IsStringUTF8(path
));
388 std::string
current_path(path
);
389 DictionaryValue
* current_dictionary
= this;
390 for (size_t delimiter_position
= current_path
.find('.');
391 delimiter_position
!= std::string::npos
;
392 delimiter_position
= current_path
.find('.')) {
393 // Assume that we're indexing into a dictionary.
394 std::string
key(current_path
, 0, delimiter_position
);
395 DictionaryValue
* child_dictionary
= NULL
;
396 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
397 child_dictionary
= new DictionaryValue
;
398 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
401 current_dictionary
= child_dictionary
;
402 current_path
.erase(0, delimiter_position
+ 1);
405 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
.Pass());
408 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
409 Set(path
, make_scoped_ptr(in_value
));
412 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
413 Set(path
, new FundamentalValue(in_value
));
416 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
417 Set(path
, new FundamentalValue(in_value
));
420 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
421 Set(path
, new FundamentalValue(in_value
));
424 void DictionaryValue::SetString(const std::string
& path
,
425 const std::string
& in_value
) {
426 Set(path
, new StringValue(in_value
));
429 void DictionaryValue::SetString(const std::string
& path
,
430 const string16
& in_value
) {
431 Set(path
, new StringValue(in_value
));
434 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
435 scoped_ptr
<Value
> in_value
) {
436 Value
* bare_ptr
= in_value
.release();
437 // If there's an existing value here, we need to delete it, because
438 // we own all our children.
439 std::pair
<ValueMap::iterator
, bool> ins_res
=
440 dictionary_
.insert(std::make_pair(key
, bare_ptr
));
441 if (!ins_res
.second
) {
442 DCHECK_NE(ins_res
.first
->second
, bare_ptr
); // This would be bogus
443 delete ins_res
.first
->second
;
444 ins_res
.first
->second
= bare_ptr
;
448 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
450 SetWithoutPathExpansion(key
, make_scoped_ptr(in_value
));
453 void DictionaryValue::SetBooleanWithoutPathExpansion(
454 const std::string
& path
, bool in_value
) {
455 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
458 void DictionaryValue::SetIntegerWithoutPathExpansion(
459 const std::string
& path
, int in_value
) {
460 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
463 void DictionaryValue::SetDoubleWithoutPathExpansion(
464 const std::string
& path
, double in_value
) {
465 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
468 void DictionaryValue::SetStringWithoutPathExpansion(
469 const std::string
& path
, const std::string
& in_value
) {
470 SetWithoutPathExpansion(path
, new StringValue(in_value
));
473 void DictionaryValue::SetStringWithoutPathExpansion(
474 const std::string
& path
, const string16
& in_value
) {
475 SetWithoutPathExpansion(path
, new StringValue(in_value
));
478 bool DictionaryValue::Get(const std::string
& path
,
479 const Value
** out_value
) const {
480 DCHECK(IsStringUTF8(path
));
481 std::string
current_path(path
);
482 const DictionaryValue
* current_dictionary
= this;
483 for (size_t delimiter_position
= current_path
.find('.');
484 delimiter_position
!= std::string::npos
;
485 delimiter_position
= current_path
.find('.')) {
486 const DictionaryValue
* child_dictionary
= NULL
;
487 if (!current_dictionary
->GetDictionary(
488 current_path
.substr(0, delimiter_position
), &child_dictionary
))
491 current_dictionary
= child_dictionary
;
492 current_path
.erase(0, delimiter_position
+ 1);
495 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
498 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
499 return static_cast<const DictionaryValue
&>(*this).Get(
501 const_cast<const Value
**>(out_value
));
504 bool DictionaryValue::GetBoolean(const std::string
& path
,
505 bool* bool_value
) const {
507 if (!Get(path
, &value
))
510 return value
->GetAsBoolean(bool_value
);
513 bool DictionaryValue::GetInteger(const std::string
& path
,
514 int* out_value
) const {
516 if (!Get(path
, &value
))
519 return value
->GetAsInteger(out_value
);
522 bool DictionaryValue::GetDouble(const std::string
& path
,
523 double* out_value
) const {
525 if (!Get(path
, &value
))
528 return value
->GetAsDouble(out_value
);
531 bool DictionaryValue::GetString(const std::string
& path
,
532 std::string
* out_value
) const {
534 if (!Get(path
, &value
))
537 return value
->GetAsString(out_value
);
540 bool DictionaryValue::GetString(const std::string
& path
,
541 string16
* out_value
) const {
543 if (!Get(path
, &value
))
546 return value
->GetAsString(out_value
);
549 bool DictionaryValue::GetStringASCII(const std::string
& path
,
550 std::string
* out_value
) const {
552 if (!GetString(path
, &out
))
555 if (!IsStringASCII(out
)) {
560 out_value
->assign(out
);
564 bool DictionaryValue::GetBinary(const std::string
& path
,
565 const BinaryValue
** out_value
) const {
567 bool result
= Get(path
, &value
);
568 if (!result
|| !value
->IsType(TYPE_BINARY
))
572 *out_value
= static_cast<const BinaryValue
*>(value
);
577 bool DictionaryValue::GetBinary(const std::string
& path
,
578 BinaryValue
** out_value
) {
579 return static_cast<const DictionaryValue
&>(*this).GetBinary(
581 const_cast<const BinaryValue
**>(out_value
));
584 bool DictionaryValue::GetDictionary(const std::string
& path
,
585 const DictionaryValue
** out_value
) const {
587 bool result
= Get(path
, &value
);
588 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
592 *out_value
= static_cast<const DictionaryValue
*>(value
);
597 bool DictionaryValue::GetDictionary(const std::string
& path
,
598 DictionaryValue
** out_value
) {
599 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
601 const_cast<const DictionaryValue
**>(out_value
));
604 bool DictionaryValue::GetList(const std::string
& path
,
605 const ListValue
** out_value
) const {
607 bool result
= Get(path
, &value
);
608 if (!result
|| !value
->IsType(TYPE_LIST
))
612 *out_value
= static_cast<const ListValue
*>(value
);
617 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
618 return static_cast<const DictionaryValue
&>(*this).GetList(
620 const_cast<const ListValue
**>(out_value
));
623 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
624 const Value
** out_value
) const {
625 DCHECK(IsStringUTF8(key
));
626 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
627 if (entry_iterator
== dictionary_
.end())
630 const Value
* entry
= entry_iterator
->second
;
636 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
638 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
640 const_cast<const Value
**>(out_value
));
643 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
644 bool* out_value
) const {
646 if (!GetWithoutPathExpansion(key
, &value
))
649 return value
->GetAsBoolean(out_value
);
652 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
653 int* out_value
) const {
655 if (!GetWithoutPathExpansion(key
, &value
))
658 return value
->GetAsInteger(out_value
);
661 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
662 double* out_value
) const {
664 if (!GetWithoutPathExpansion(key
, &value
))
667 return value
->GetAsDouble(out_value
);
670 bool DictionaryValue::GetStringWithoutPathExpansion(
671 const std::string
& key
,
672 std::string
* out_value
) const {
674 if (!GetWithoutPathExpansion(key
, &value
))
677 return value
->GetAsString(out_value
);
680 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
681 string16
* out_value
) const {
683 if (!GetWithoutPathExpansion(key
, &value
))
686 return value
->GetAsString(out_value
);
689 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
690 const std::string
& key
,
691 const DictionaryValue
** out_value
) const {
693 bool result
= GetWithoutPathExpansion(key
, &value
);
694 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
698 *out_value
= static_cast<const DictionaryValue
*>(value
);
703 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
704 const std::string
& key
,
705 DictionaryValue
** out_value
) {
706 const DictionaryValue
& const_this
=
707 static_cast<const DictionaryValue
&>(*this);
708 return const_this
.GetDictionaryWithoutPathExpansion(
710 const_cast<const DictionaryValue
**>(out_value
));
713 bool DictionaryValue::GetListWithoutPathExpansion(
714 const std::string
& key
,
715 const ListValue
** out_value
) const {
717 bool result
= GetWithoutPathExpansion(key
, &value
);
718 if (!result
|| !value
->IsType(TYPE_LIST
))
722 *out_value
= static_cast<const ListValue
*>(value
);
727 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
728 ListValue
** out_value
) {
730 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
732 const_cast<const ListValue
**>(out_value
));
735 bool DictionaryValue::Remove(const std::string
& path
,
736 scoped_ptr
<Value
>* out_value
) {
737 DCHECK(IsStringUTF8(path
));
738 std::string
current_path(path
);
739 DictionaryValue
* current_dictionary
= this;
740 size_t delimiter_position
= current_path
.rfind('.');
741 if (delimiter_position
!= std::string::npos
) {
742 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
743 ¤t_dictionary
))
745 current_path
.erase(0, delimiter_position
+ 1);
748 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
752 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
753 scoped_ptr
<Value
>* out_value
) {
754 DCHECK(IsStringUTF8(key
));
755 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
756 if (entry_iterator
== dictionary_
.end())
759 Value
* entry
= entry_iterator
->second
;
761 out_value
->reset(entry
);
764 dictionary_
.erase(entry_iterator
);
768 bool DictionaryValue::RemovePath(const std::string
& path
,
769 scoped_ptr
<Value
>* out_value
) {
771 size_t delimiter_position
= path
.find('.');
773 if (delimiter_position
== std::string::npos
)
774 return RemoveWithoutPathExpansion(path
, out_value
);
776 const std::string subdict_path
= path
.substr(0, delimiter_position
);
777 DictionaryValue
* subdict
= NULL
;
778 if (!GetDictionary(subdict_path
, &subdict
))
780 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
782 if (result
&& subdict
->empty())
783 RemoveWithoutPathExpansion(subdict_path
, NULL
);
788 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
789 Value
* copy
= CopyWithoutEmptyChildren(this);
790 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
793 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
794 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
795 const Value
* merge_value
= &it
.value();
796 // Check whether we have to merge dictionaries.
797 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
798 DictionaryValue
* sub_dict
;
799 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
800 sub_dict
->MergeDictionary(
801 static_cast<const DictionaryValue
*>(merge_value
));
805 // All other cases: Make a copy and hook it up.
806 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
810 void DictionaryValue::Swap(DictionaryValue
* other
) {
811 dictionary_
.swap(other
->dictionary_
);
814 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
816 it_(target
.dictionary_
.begin()) {}
818 DictionaryValue::Iterator::~Iterator() {}
820 DictionaryValue
* DictionaryValue::DeepCopy() const {
821 DictionaryValue
* result
= new DictionaryValue
;
823 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
824 current_entry
!= dictionary_
.end(); ++current_entry
) {
825 result
->SetWithoutPathExpansion(current_entry
->first
,
826 current_entry
->second
->DeepCopy());
832 bool DictionaryValue::Equals(const Value
* other
) const {
833 if (other
->GetType() != GetType())
836 const DictionaryValue
* other_dict
=
837 static_cast<const DictionaryValue
*>(other
);
838 Iterator
lhs_it(*this);
839 Iterator
rhs_it(*other_dict
);
840 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
841 if (lhs_it
.key() != rhs_it
.key() ||
842 !lhs_it
.value().Equals(&rhs_it
.value())) {
848 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
854 ///////////////////// ListValue ////////////////////
856 ListValue::ListValue() : Value(TYPE_LIST
) {
859 ListValue::~ListValue() {
863 void ListValue::Clear() {
864 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
869 bool ListValue::Set(size_t index
, Value
* in_value
) {
873 if (index
>= list_
.size()) {
874 // Pad out any intermediate indexes with null settings
875 while (index
> list_
.size())
876 Append(CreateNullValue());
879 DCHECK(list_
[index
] != in_value
);
881 list_
[index
] = in_value
;
886 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
887 if (index
>= list_
.size())
891 *out_value
= list_
[index
];
896 bool ListValue::Get(size_t index
, Value
** out_value
) {
897 return static_cast<const ListValue
&>(*this).Get(
899 const_cast<const Value
**>(out_value
));
902 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
904 if (!Get(index
, &value
))
907 return value
->GetAsBoolean(bool_value
);
910 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
912 if (!Get(index
, &value
))
915 return value
->GetAsInteger(out_value
);
918 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
920 if (!Get(index
, &value
))
923 return value
->GetAsDouble(out_value
);
926 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
928 if (!Get(index
, &value
))
931 return value
->GetAsString(out_value
);
934 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
936 if (!Get(index
, &value
))
939 return value
->GetAsString(out_value
);
942 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
944 bool result
= Get(index
, &value
);
945 if (!result
|| !value
->IsType(TYPE_BINARY
))
949 *out_value
= static_cast<const BinaryValue
*>(value
);
954 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
955 return static_cast<const ListValue
&>(*this).GetBinary(
957 const_cast<const BinaryValue
**>(out_value
));
960 bool ListValue::GetDictionary(size_t index
,
961 const DictionaryValue
** out_value
) const {
963 bool result
= Get(index
, &value
);
964 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
968 *out_value
= static_cast<const DictionaryValue
*>(value
);
973 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
974 return static_cast<const ListValue
&>(*this).GetDictionary(
976 const_cast<const DictionaryValue
**>(out_value
));
979 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
981 bool result
= Get(index
, &value
);
982 if (!result
|| !value
->IsType(TYPE_LIST
))
986 *out_value
= static_cast<const ListValue
*>(value
);
991 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
992 return static_cast<const ListValue
&>(*this).GetList(
994 const_cast<const ListValue
**>(out_value
));
997 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
998 if (index
>= list_
.size())
1002 out_value
->reset(list_
[index
]);
1004 delete list_
[index
];
1006 list_
.erase(list_
.begin() + index
);
1010 bool ListValue::Remove(const Value
& value
, size_t* index
) {
1011 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1012 if ((*i
)->Equals(&value
)) {
1013 size_t previous_index
= i
- list_
.begin();
1018 *index
= previous_index
;
1025 ListValue::iterator
ListValue::Erase(iterator iter
,
1026 scoped_ptr
<Value
>* out_value
) {
1028 out_value
->reset(*iter
);
1032 return list_
.erase(iter
);
1035 void ListValue::Append(Value
* in_value
) {
1037 list_
.push_back(in_value
);
1040 void ListValue::AppendBoolean(bool in_value
) {
1041 Append(new FundamentalValue(in_value
));
1044 void ListValue::AppendInteger(int in_value
) {
1045 Append(new FundamentalValue(in_value
));
1048 void ListValue::AppendDouble(double in_value
) {
1049 Append(new FundamentalValue(in_value
));
1052 void ListValue::AppendString(const std::string
& in_value
) {
1053 Append(new StringValue(in_value
));
1056 void ListValue::AppendString(const string16
& in_value
) {
1057 Append(new StringValue(in_value
));
1060 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1061 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1062 it
!= in_values
.end(); ++it
) {
1067 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1068 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1069 it
!= in_values
.end(); ++it
) {
1074 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1076 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1077 if ((*i
)->Equals(in_value
)) {
1082 list_
.push_back(in_value
);
1086 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1088 if (index
> list_
.size())
1091 list_
.insert(list_
.begin() + index
, in_value
);
1095 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1096 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1099 void ListValue::Swap(ListValue
* other
) {
1100 list_
.swap(other
->list_
);
1103 bool ListValue::GetAsList(ListValue
** out_value
) {
1109 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1115 ListValue
* ListValue::DeepCopy() const {
1116 ListValue
* result
= new ListValue
;
1118 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1119 result
->Append((*i
)->DeepCopy());
1124 bool ListValue::Equals(const Value
* other
) const {
1125 if (other
->GetType() != GetType())
1128 const ListValue
* other_list
=
1129 static_cast<const ListValue
*>(other
);
1130 const_iterator lhs_it
, rhs_it
;
1131 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1132 lhs_it
!= end() && rhs_it
!= other_list
->end();
1133 ++lhs_it
, ++rhs_it
) {
1134 if (!(*lhs_it
)->Equals(*rhs_it
))
1137 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1143 ValueSerializer::~ValueSerializer() {
1146 ValueDeserializer::~ValueDeserializer() {
1149 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1151 JSONWriter::WriteWithOptions(&value
,
1152 JSONWriter::OPTIONS_PRETTY_PRINT
,