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::GetAsString(const StringValue
** out_value
) const {
141 bool Value::GetAsList(ListValue
** out_value
) {
145 bool Value::GetAsList(const ListValue
** out_value
) const {
149 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
153 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
157 Value
* Value::DeepCopy() 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 CreateNullValue();
164 bool Value::Equals(const Value
* other
) const {
165 // This method should only be getting called for null Values--all subclasses
166 // need to provide their own implementation;.
167 DCHECK(IsType(TYPE_NULL
));
168 return other
->IsType(TYPE_NULL
);
172 bool Value::Equals(const Value
* a
, const Value
* b
) {
173 if ((a
== NULL
) && (b
== NULL
)) return true;
174 if ((a
== NULL
) ^ (b
== NULL
)) return false;
178 Value::Value(Type type
) : type_(type
) {}
180 Value::Value(const Value
& that
) : type_(that
.type_
) {}
182 Value
& Value::operator=(const Value
& that
) {
187 ///////////////////// FundamentalValue ////////////////////
189 FundamentalValue::FundamentalValue(bool in_value
)
190 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
193 FundamentalValue::FundamentalValue(int in_value
)
194 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
197 FundamentalValue::FundamentalValue(double in_value
)
198 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
199 if (!IsFinite(double_value_
)) {
200 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
201 << "values cannot be represented in JSON";
206 FundamentalValue::~FundamentalValue() {
209 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
210 if (out_value
&& IsType(TYPE_BOOLEAN
))
211 *out_value
= boolean_value_
;
212 return (IsType(TYPE_BOOLEAN
));
215 bool FundamentalValue::GetAsInteger(int* out_value
) const {
216 if (out_value
&& IsType(TYPE_INTEGER
))
217 *out_value
= integer_value_
;
218 return (IsType(TYPE_INTEGER
));
221 bool FundamentalValue::GetAsDouble(double* out_value
) const {
222 if (out_value
&& IsType(TYPE_DOUBLE
))
223 *out_value
= double_value_
;
224 else if (out_value
&& IsType(TYPE_INTEGER
))
225 *out_value
= integer_value_
;
226 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
229 FundamentalValue
* FundamentalValue::DeepCopy() const {
232 return new FundamentalValue(boolean_value_
);
235 return new FundamentalValue(integer_value_
);
238 return new FundamentalValue(double_value_
);
246 bool FundamentalValue::Equals(const Value
* other
) const {
247 if (other
->GetType() != GetType())
253 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
257 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
261 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
269 ///////////////////// StringValue ////////////////////
271 StringValue::StringValue(const std::string
& in_value
)
272 : Value(TYPE_STRING
),
274 DCHECK(IsStringUTF8(in_value
));
277 StringValue::StringValue(const string16
& in_value
)
278 : Value(TYPE_STRING
),
279 value_(UTF16ToUTF8(in_value
)) {
282 StringValue::~StringValue() {
285 std::string
* StringValue::GetString() {
289 const std::string
& StringValue::GetString() const {
293 bool StringValue::GetAsString(std::string
* out_value
) const {
299 bool StringValue::GetAsString(string16
* out_value
) const {
301 *out_value
= UTF8ToUTF16(value_
);
305 bool StringValue::GetAsString(const StringValue
** out_value
) const {
311 StringValue
* StringValue::DeepCopy() const {
312 return new StringValue(value_
);
315 bool StringValue::Equals(const Value
* other
) const {
316 if (other
->GetType() != GetType())
318 std::string lhs
, rhs
;
319 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
322 ///////////////////// BinaryValue ////////////////////
324 BinaryValue::BinaryValue()
325 : Value(TYPE_BINARY
),
329 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
330 : Value(TYPE_BINARY
),
331 buffer_(buffer
.Pass()),
335 BinaryValue::~BinaryValue() {
339 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
341 char* buffer_copy
= new char[size
];
342 memcpy(buffer_copy
, buffer
, size
);
343 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
344 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
347 BinaryValue
* BinaryValue::DeepCopy() const {
348 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
351 bool BinaryValue::Equals(const Value
* other
) const {
352 if (other
->GetType() != GetType())
354 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
355 if (other_binary
->size_
!= size_
)
357 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
360 ///////////////////// DictionaryValue ////////////////////
362 DictionaryValue::DictionaryValue()
363 : Value(TYPE_DICTIONARY
) {
366 DictionaryValue::~DictionaryValue() {
370 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
376 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
382 bool DictionaryValue::HasKey(const std::string
& key
) const {
383 DCHECK(IsStringUTF8(key
));
384 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
385 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
386 return current_entry
!= dictionary_
.end();
389 void DictionaryValue::Clear() {
390 ValueMap::iterator dict_iterator
= dictionary_
.begin();
391 while (dict_iterator
!= dictionary_
.end()) {
392 delete dict_iterator
->second
;
399 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
400 DCHECK(IsStringUTF8(path
));
403 std::string
current_path(path
);
404 DictionaryValue
* current_dictionary
= this;
405 for (size_t delimiter_position
= current_path
.find('.');
406 delimiter_position
!= std::string::npos
;
407 delimiter_position
= current_path
.find('.')) {
408 // Assume that we're indexing into a dictionary.
409 std::string
key(current_path
, 0, delimiter_position
);
410 DictionaryValue
* child_dictionary
= NULL
;
411 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
412 child_dictionary
= new DictionaryValue
;
413 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
416 current_dictionary
= child_dictionary
;
417 current_path
.erase(0, delimiter_position
+ 1);
420 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
423 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
424 Set(path
, new FundamentalValue(in_value
));
427 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
428 Set(path
, new FundamentalValue(in_value
));
431 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
432 Set(path
, new FundamentalValue(in_value
));
435 void DictionaryValue::SetString(const std::string
& path
,
436 const std::string
& in_value
) {
437 Set(path
, new StringValue(in_value
));
440 void DictionaryValue::SetString(const std::string
& path
,
441 const string16
& in_value
) {
442 Set(path
, new StringValue(in_value
));
445 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
447 // If there's an existing value here, we need to delete it, because
448 // we own all our children.
449 std::pair
<ValueMap::iterator
, bool> ins_res
=
450 dictionary_
.insert(std::make_pair(key
, in_value
));
451 if (!ins_res
.second
) {
452 DCHECK_NE(ins_res
.first
->second
, in_value
); // This would be bogus
453 delete ins_res
.first
->second
;
454 ins_res
.first
->second
= in_value
;
458 void DictionaryValue::SetBooleanWithoutPathExpansion(
459 const std::string
& path
, bool in_value
) {
460 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
463 void DictionaryValue::SetIntegerWithoutPathExpansion(
464 const std::string
& path
, int in_value
) {
465 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
468 void DictionaryValue::SetDoubleWithoutPathExpansion(
469 const std::string
& path
, double in_value
) {
470 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
473 void DictionaryValue::SetStringWithoutPathExpansion(
474 const std::string
& path
, const std::string
& in_value
) {
475 SetWithoutPathExpansion(path
, new StringValue(in_value
));
478 void DictionaryValue::SetStringWithoutPathExpansion(
479 const std::string
& path
, const string16
& in_value
) {
480 SetWithoutPathExpansion(path
, new StringValue(in_value
));
483 bool DictionaryValue::Get(const std::string
& path
,
484 const Value
** out_value
) const {
485 DCHECK(IsStringUTF8(path
));
486 std::string
current_path(path
);
487 const DictionaryValue
* current_dictionary
= this;
488 for (size_t delimiter_position
= current_path
.find('.');
489 delimiter_position
!= std::string::npos
;
490 delimiter_position
= current_path
.find('.')) {
491 const DictionaryValue
* child_dictionary
= NULL
;
492 if (!current_dictionary
->GetDictionary(
493 current_path
.substr(0, delimiter_position
), &child_dictionary
))
496 current_dictionary
= child_dictionary
;
497 current_path
.erase(0, delimiter_position
+ 1);
500 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
503 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
504 return static_cast<const DictionaryValue
&>(*this).Get(
506 const_cast<const Value
**>(out_value
));
509 bool DictionaryValue::GetBoolean(const std::string
& path
,
510 bool* bool_value
) const {
512 if (!Get(path
, &value
))
515 return value
->GetAsBoolean(bool_value
);
518 bool DictionaryValue::GetInteger(const std::string
& path
,
519 int* out_value
) const {
521 if (!Get(path
, &value
))
524 return value
->GetAsInteger(out_value
);
527 bool DictionaryValue::GetDouble(const std::string
& path
,
528 double* out_value
) const {
530 if (!Get(path
, &value
))
533 return value
->GetAsDouble(out_value
);
536 bool DictionaryValue::GetString(const std::string
& path
,
537 std::string
* out_value
) const {
539 if (!Get(path
, &value
))
542 return value
->GetAsString(out_value
);
545 bool DictionaryValue::GetString(const std::string
& path
,
546 string16
* out_value
) const {
548 if (!Get(path
, &value
))
551 return value
->GetAsString(out_value
);
554 bool DictionaryValue::GetStringASCII(const std::string
& path
,
555 std::string
* out_value
) const {
557 if (!GetString(path
, &out
))
560 if (!IsStringASCII(out
)) {
565 out_value
->assign(out
);
569 bool DictionaryValue::GetBinary(const std::string
& path
,
570 const BinaryValue
** out_value
) const {
572 bool result
= Get(path
, &value
);
573 if (!result
|| !value
->IsType(TYPE_BINARY
))
577 *out_value
= static_cast<const BinaryValue
*>(value
);
582 bool DictionaryValue::GetBinary(const std::string
& path
,
583 BinaryValue
** out_value
) {
584 return static_cast<const DictionaryValue
&>(*this).GetBinary(
586 const_cast<const BinaryValue
**>(out_value
));
589 bool DictionaryValue::GetDictionary(const std::string
& path
,
590 const DictionaryValue
** out_value
) const {
592 bool result
= Get(path
, &value
);
593 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
597 *out_value
= static_cast<const DictionaryValue
*>(value
);
602 bool DictionaryValue::GetDictionary(const std::string
& path
,
603 DictionaryValue
** out_value
) {
604 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
606 const_cast<const DictionaryValue
**>(out_value
));
609 bool DictionaryValue::GetList(const std::string
& path
,
610 const ListValue
** out_value
) const {
612 bool result
= Get(path
, &value
);
613 if (!result
|| !value
->IsType(TYPE_LIST
))
617 *out_value
= static_cast<const ListValue
*>(value
);
622 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
623 return static_cast<const DictionaryValue
&>(*this).GetList(
625 const_cast<const ListValue
**>(out_value
));
628 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
629 const Value
** out_value
) const {
630 DCHECK(IsStringUTF8(key
));
631 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
632 if (entry_iterator
== dictionary_
.end())
635 const Value
* entry
= entry_iterator
->second
;
641 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
643 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
645 const_cast<const Value
**>(out_value
));
648 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
649 bool* out_value
) const {
651 if (!GetWithoutPathExpansion(key
, &value
))
654 return value
->GetAsBoolean(out_value
);
657 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
658 int* out_value
) const {
660 if (!GetWithoutPathExpansion(key
, &value
))
663 return value
->GetAsInteger(out_value
);
666 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
667 double* out_value
) const {
669 if (!GetWithoutPathExpansion(key
, &value
))
672 return value
->GetAsDouble(out_value
);
675 bool DictionaryValue::GetStringWithoutPathExpansion(
676 const std::string
& key
,
677 std::string
* out_value
) const {
679 if (!GetWithoutPathExpansion(key
, &value
))
682 return value
->GetAsString(out_value
);
685 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
686 string16
* out_value
) const {
688 if (!GetWithoutPathExpansion(key
, &value
))
691 return value
->GetAsString(out_value
);
694 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
695 const std::string
& key
,
696 const DictionaryValue
** out_value
) const {
698 bool result
= GetWithoutPathExpansion(key
, &value
);
699 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
703 *out_value
= static_cast<const DictionaryValue
*>(value
);
708 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
709 const std::string
& key
,
710 DictionaryValue
** out_value
) {
711 const DictionaryValue
& const_this
=
712 static_cast<const DictionaryValue
&>(*this);
713 return const_this
.GetDictionaryWithoutPathExpansion(
715 const_cast<const DictionaryValue
**>(out_value
));
718 bool DictionaryValue::GetListWithoutPathExpansion(
719 const std::string
& key
,
720 const ListValue
** out_value
) const {
722 bool result
= GetWithoutPathExpansion(key
, &value
);
723 if (!result
|| !value
->IsType(TYPE_LIST
))
727 *out_value
= static_cast<const ListValue
*>(value
);
732 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
733 ListValue
** out_value
) {
735 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
737 const_cast<const ListValue
**>(out_value
));
740 bool DictionaryValue::Remove(const std::string
& path
,
741 scoped_ptr
<Value
>* out_value
) {
742 DCHECK(IsStringUTF8(path
));
743 std::string
current_path(path
);
744 DictionaryValue
* current_dictionary
= this;
745 size_t delimiter_position
= current_path
.rfind('.');
746 if (delimiter_position
!= std::string::npos
) {
747 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
748 ¤t_dictionary
))
750 current_path
.erase(0, delimiter_position
+ 1);
753 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
757 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
758 scoped_ptr
<Value
>* out_value
) {
759 DCHECK(IsStringUTF8(key
));
760 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
761 if (entry_iterator
== dictionary_
.end())
764 Value
* entry
= entry_iterator
->second
;
766 out_value
->reset(entry
);
769 dictionary_
.erase(entry_iterator
);
773 bool DictionaryValue::RemovePath(const std::string
& path
,
774 scoped_ptr
<Value
>* out_value
) {
776 size_t delimiter_position
= path
.find('.');
778 if (delimiter_position
== std::string::npos
)
779 return RemoveWithoutPathExpansion(path
, out_value
);
781 const std::string subdict_path
= path
.substr(0, delimiter_position
);
782 DictionaryValue
* subdict
= NULL
;
783 if (!GetDictionary(subdict_path
, &subdict
))
785 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
787 if (result
&& subdict
->empty())
788 RemoveWithoutPathExpansion(subdict_path
, NULL
);
793 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
794 Value
* copy
= CopyWithoutEmptyChildren(this);
795 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
798 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
799 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
800 const Value
* merge_value
= &it
.value();
801 // Check whether we have to merge dictionaries.
802 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
803 DictionaryValue
* sub_dict
;
804 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
805 sub_dict
->MergeDictionary(
806 static_cast<const DictionaryValue
*>(merge_value
));
810 // All other cases: Make a copy and hook it up.
811 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
815 void DictionaryValue::Swap(DictionaryValue
* other
) {
816 dictionary_
.swap(other
->dictionary_
);
819 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
821 it_(target
.dictionary_
.begin()) {}
823 DictionaryValue::Iterator::~Iterator() {}
825 DictionaryValue
* DictionaryValue::DeepCopy() const {
826 DictionaryValue
* result
= new DictionaryValue
;
828 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
829 current_entry
!= dictionary_
.end(); ++current_entry
) {
830 result
->SetWithoutPathExpansion(current_entry
->first
,
831 current_entry
->second
->DeepCopy());
837 bool DictionaryValue::Equals(const Value
* other
) const {
838 if (other
->GetType() != GetType())
841 const DictionaryValue
* other_dict
=
842 static_cast<const DictionaryValue
*>(other
);
843 Iterator
lhs_it(*this);
844 Iterator
rhs_it(*other_dict
);
845 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
846 if (lhs_it
.key() != rhs_it
.key() ||
847 !lhs_it
.value().Equals(&rhs_it
.value())) {
853 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
859 ///////////////////// ListValue ////////////////////
861 ListValue::ListValue() : Value(TYPE_LIST
) {
864 ListValue::~ListValue() {
868 void ListValue::Clear() {
869 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
874 bool ListValue::Set(size_t index
, Value
* in_value
) {
878 if (index
>= list_
.size()) {
879 // Pad out any intermediate indexes with null settings
880 while (index
> list_
.size())
881 Append(CreateNullValue());
884 DCHECK(list_
[index
] != in_value
);
886 list_
[index
] = in_value
;
891 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
892 if (index
>= list_
.size())
896 *out_value
= list_
[index
];
901 bool ListValue::Get(size_t index
, Value
** out_value
) {
902 return static_cast<const ListValue
&>(*this).Get(
904 const_cast<const Value
**>(out_value
));
907 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
909 if (!Get(index
, &value
))
912 return value
->GetAsBoolean(bool_value
);
915 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
917 if (!Get(index
, &value
))
920 return value
->GetAsInteger(out_value
);
923 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
925 if (!Get(index
, &value
))
928 return value
->GetAsDouble(out_value
);
931 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
933 if (!Get(index
, &value
))
936 return value
->GetAsString(out_value
);
939 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
941 if (!Get(index
, &value
))
944 return value
->GetAsString(out_value
);
947 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
949 bool result
= Get(index
, &value
);
950 if (!result
|| !value
->IsType(TYPE_BINARY
))
954 *out_value
= static_cast<const BinaryValue
*>(value
);
959 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
960 return static_cast<const ListValue
&>(*this).GetBinary(
962 const_cast<const BinaryValue
**>(out_value
));
965 bool ListValue::GetDictionary(size_t index
,
966 const DictionaryValue
** out_value
) const {
968 bool result
= Get(index
, &value
);
969 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
973 *out_value
= static_cast<const DictionaryValue
*>(value
);
978 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
979 return static_cast<const ListValue
&>(*this).GetDictionary(
981 const_cast<const DictionaryValue
**>(out_value
));
984 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
986 bool result
= Get(index
, &value
);
987 if (!result
|| !value
->IsType(TYPE_LIST
))
991 *out_value
= static_cast<const ListValue
*>(value
);
996 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
997 return static_cast<const ListValue
&>(*this).GetList(
999 const_cast<const ListValue
**>(out_value
));
1002 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
1003 if (index
>= list_
.size())
1007 out_value
->reset(list_
[index
]);
1009 delete list_
[index
];
1011 list_
.erase(list_
.begin() + index
);
1015 bool ListValue::Remove(const Value
& value
, size_t* index
) {
1016 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1017 if ((*i
)->Equals(&value
)) {
1018 size_t previous_index
= i
- list_
.begin();
1023 *index
= previous_index
;
1030 ListValue::iterator
ListValue::Erase(iterator iter
,
1031 scoped_ptr
<Value
>* out_value
) {
1033 out_value
->reset(*iter
);
1037 return list_
.erase(iter
);
1040 void ListValue::Append(Value
* in_value
) {
1042 list_
.push_back(in_value
);
1045 void ListValue::AppendBoolean(bool in_value
) {
1046 Append(new FundamentalValue(in_value
));
1049 void ListValue::AppendInteger(int in_value
) {
1050 Append(new FundamentalValue(in_value
));
1053 void ListValue::AppendDouble(double in_value
) {
1054 Append(new FundamentalValue(in_value
));
1057 void ListValue::AppendString(const std::string
& in_value
) {
1058 Append(new StringValue(in_value
));
1061 void ListValue::AppendString(const string16
& in_value
) {
1062 Append(new StringValue(in_value
));
1065 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1066 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1067 it
!= in_values
.end(); ++it
) {
1072 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1073 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1074 it
!= in_values
.end(); ++it
) {
1079 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1081 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1082 if ((*i
)->Equals(in_value
)) {
1087 list_
.push_back(in_value
);
1091 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1093 if (index
> list_
.size())
1096 list_
.insert(list_
.begin() + index
, in_value
);
1100 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1101 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1104 void ListValue::Swap(ListValue
* other
) {
1105 list_
.swap(other
->list_
);
1108 bool ListValue::GetAsList(ListValue
** out_value
) {
1114 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1120 ListValue
* ListValue::DeepCopy() const {
1121 ListValue
* result
= new ListValue
;
1123 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1124 result
->Append((*i
)->DeepCopy());
1129 bool ListValue::Equals(const Value
* other
) const {
1130 if (other
->GetType() != GetType())
1133 const ListValue
* other_list
=
1134 static_cast<const ListValue
*>(other
);
1135 const_iterator lhs_it
, rhs_it
;
1136 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1137 lhs_it
!= end() && rhs_it
!= other_list
->end();
1138 ++lhs_it
, ++rhs_it
) {
1139 if (!(*lhs_it
)->Equals(*rhs_it
))
1142 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1148 ValueSerializer::~ValueSerializer() {
1151 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1153 JSONWriter::WriteWithOptions(&value
,
1154 JSONWriter::OPTIONS_PRETTY_PRINT
,