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
);
92 bool Value::GetAsBoolean(bool* out_value
) const {
96 bool Value::GetAsInteger(int* out_value
) const {
100 bool Value::GetAsDouble(double* out_value
) const {
104 bool Value::GetAsString(std::string
* out_value
) const {
108 bool Value::GetAsString(string16
* out_value
) const {
112 bool Value::GetAsString(const StringValue
** out_value
) const {
116 bool Value::GetAsList(ListValue
** out_value
) {
120 bool Value::GetAsList(const ListValue
** out_value
) const {
124 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
128 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
132 Value
* Value::DeepCopy() const {
133 // This method should only be getting called for null Values--all subclasses
134 // need to provide their own implementation;.
135 DCHECK(IsType(TYPE_NULL
));
136 return CreateNullValue();
139 bool Value::Equals(const Value
* other
) const {
140 // This method should only be getting called for null Values--all subclasses
141 // need to provide their own implementation;.
142 DCHECK(IsType(TYPE_NULL
));
143 return other
->IsType(TYPE_NULL
);
147 bool Value::Equals(const Value
* a
, const Value
* b
) {
148 if ((a
== NULL
) && (b
== NULL
)) return true;
149 if ((a
== NULL
) ^ (b
== NULL
)) return false;
153 Value::Value(Type type
) : type_(type
) {}
155 Value::Value(const Value
& that
) : type_(that
.type_
) {}
157 Value
& Value::operator=(const Value
& that
) {
162 ///////////////////// FundamentalValue ////////////////////
164 FundamentalValue::FundamentalValue(bool in_value
)
165 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
168 FundamentalValue::FundamentalValue(int in_value
)
169 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
172 FundamentalValue::FundamentalValue(double in_value
)
173 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
174 if (!IsFinite(double_value_
)) {
175 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
176 << "values cannot be represented in JSON";
181 FundamentalValue::~FundamentalValue() {
184 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
185 if (out_value
&& IsType(TYPE_BOOLEAN
))
186 *out_value
= boolean_value_
;
187 return (IsType(TYPE_BOOLEAN
));
190 bool FundamentalValue::GetAsInteger(int* out_value
) const {
191 if (out_value
&& IsType(TYPE_INTEGER
))
192 *out_value
= integer_value_
;
193 return (IsType(TYPE_INTEGER
));
196 bool FundamentalValue::GetAsDouble(double* out_value
) const {
197 if (out_value
&& IsType(TYPE_DOUBLE
))
198 *out_value
= double_value_
;
199 else if (out_value
&& IsType(TYPE_INTEGER
))
200 *out_value
= integer_value_
;
201 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
204 FundamentalValue
* FundamentalValue::DeepCopy() const {
207 return new FundamentalValue(boolean_value_
);
210 return new FundamentalValue(integer_value_
);
213 return new FundamentalValue(double_value_
);
221 bool FundamentalValue::Equals(const Value
* other
) const {
222 if (other
->GetType() != GetType())
228 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
232 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
236 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
244 ///////////////////// StringValue ////////////////////
246 StringValue::StringValue(const std::string
& in_value
)
247 : Value(TYPE_STRING
),
249 DCHECK(IsStringUTF8(in_value
));
252 StringValue::StringValue(const string16
& in_value
)
253 : Value(TYPE_STRING
),
254 value_(UTF16ToUTF8(in_value
)) {
257 StringValue::~StringValue() {
260 std::string
* StringValue::GetString() {
264 const std::string
& StringValue::GetString() const {
268 bool StringValue::GetAsString(std::string
* out_value
) const {
274 bool StringValue::GetAsString(string16
* out_value
) const {
276 *out_value
= UTF8ToUTF16(value_
);
280 bool StringValue::GetAsString(const StringValue
** out_value
) const {
286 StringValue
* StringValue::DeepCopy() const {
287 return new StringValue(value_
);
290 bool StringValue::Equals(const Value
* other
) const {
291 if (other
->GetType() != GetType())
293 std::string lhs
, rhs
;
294 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
297 ///////////////////// BinaryValue ////////////////////
299 BinaryValue::BinaryValue()
300 : Value(TYPE_BINARY
),
304 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
305 : Value(TYPE_BINARY
),
306 buffer_(buffer
.Pass()),
310 BinaryValue::~BinaryValue() {
314 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
316 char* buffer_copy
= new char[size
];
317 memcpy(buffer_copy
, buffer
, size
);
318 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
319 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
322 BinaryValue
* BinaryValue::DeepCopy() const {
323 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
326 bool BinaryValue::Equals(const Value
* other
) const {
327 if (other
->GetType() != GetType())
329 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
330 if (other_binary
->size_
!= size_
)
332 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
335 ///////////////////// DictionaryValue ////////////////////
337 DictionaryValue::DictionaryValue()
338 : Value(TYPE_DICTIONARY
) {
341 DictionaryValue::~DictionaryValue() {
345 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
351 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
357 bool DictionaryValue::HasKey(const std::string
& key
) const {
358 DCHECK(IsStringUTF8(key
));
359 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
360 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
361 return current_entry
!= dictionary_
.end();
364 void DictionaryValue::Clear() {
365 ValueMap::iterator dict_iterator
= dictionary_
.begin();
366 while (dict_iterator
!= dictionary_
.end()) {
367 delete dict_iterator
->second
;
374 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
375 DCHECK(IsStringUTF8(path
));
378 std::string
current_path(path
);
379 DictionaryValue
* current_dictionary
= this;
380 for (size_t delimiter_position
= current_path
.find('.');
381 delimiter_position
!= std::string::npos
;
382 delimiter_position
= current_path
.find('.')) {
383 // Assume that we're indexing into a dictionary.
384 std::string
key(current_path
, 0, delimiter_position
);
385 DictionaryValue
* child_dictionary
= NULL
;
386 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
387 child_dictionary
= new DictionaryValue
;
388 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
391 current_dictionary
= child_dictionary
;
392 current_path
.erase(0, delimiter_position
+ 1);
395 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
398 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
399 Set(path
, new FundamentalValue(in_value
));
402 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
403 Set(path
, new FundamentalValue(in_value
));
406 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
407 Set(path
, new FundamentalValue(in_value
));
410 void DictionaryValue::SetString(const std::string
& path
,
411 const std::string
& in_value
) {
412 Set(path
, new StringValue(in_value
));
415 void DictionaryValue::SetString(const std::string
& path
,
416 const string16
& in_value
) {
417 Set(path
, new StringValue(in_value
));
420 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
422 // If there's an existing value here, we need to delete it, because
423 // we own all our children.
424 std::pair
<ValueMap::iterator
, bool> ins_res
=
425 dictionary_
.insert(std::make_pair(key
, in_value
));
426 if (!ins_res
.second
) {
427 DCHECK_NE(ins_res
.first
->second
, in_value
); // This would be bogus
428 delete ins_res
.first
->second
;
429 ins_res
.first
->second
= in_value
;
433 void DictionaryValue::SetBooleanWithoutPathExpansion(
434 const std::string
& path
, bool in_value
) {
435 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
438 void DictionaryValue::SetIntegerWithoutPathExpansion(
439 const std::string
& path
, int in_value
) {
440 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
443 void DictionaryValue::SetDoubleWithoutPathExpansion(
444 const std::string
& path
, double in_value
) {
445 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
448 void DictionaryValue::SetStringWithoutPathExpansion(
449 const std::string
& path
, const std::string
& in_value
) {
450 SetWithoutPathExpansion(path
, new StringValue(in_value
));
453 void DictionaryValue::SetStringWithoutPathExpansion(
454 const std::string
& path
, const string16
& in_value
) {
455 SetWithoutPathExpansion(path
, new StringValue(in_value
));
458 bool DictionaryValue::Get(const std::string
& path
,
459 const Value
** out_value
) const {
460 DCHECK(IsStringUTF8(path
));
461 std::string
current_path(path
);
462 const DictionaryValue
* current_dictionary
= this;
463 for (size_t delimiter_position
= current_path
.find('.');
464 delimiter_position
!= std::string::npos
;
465 delimiter_position
= current_path
.find('.')) {
466 const DictionaryValue
* child_dictionary
= NULL
;
467 if (!current_dictionary
->GetDictionary(
468 current_path
.substr(0, delimiter_position
), &child_dictionary
))
471 current_dictionary
= child_dictionary
;
472 current_path
.erase(0, delimiter_position
+ 1);
475 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
478 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
479 return static_cast<const DictionaryValue
&>(*this).Get(
481 const_cast<const Value
**>(out_value
));
484 bool DictionaryValue::GetBoolean(const std::string
& path
,
485 bool* bool_value
) const {
487 if (!Get(path
, &value
))
490 return value
->GetAsBoolean(bool_value
);
493 bool DictionaryValue::GetInteger(const std::string
& path
,
494 int* out_value
) const {
496 if (!Get(path
, &value
))
499 return value
->GetAsInteger(out_value
);
502 bool DictionaryValue::GetDouble(const std::string
& path
,
503 double* out_value
) const {
505 if (!Get(path
, &value
))
508 return value
->GetAsDouble(out_value
);
511 bool DictionaryValue::GetString(const std::string
& path
,
512 std::string
* out_value
) const {
514 if (!Get(path
, &value
))
517 return value
->GetAsString(out_value
);
520 bool DictionaryValue::GetString(const std::string
& path
,
521 string16
* out_value
) const {
523 if (!Get(path
, &value
))
526 return value
->GetAsString(out_value
);
529 bool DictionaryValue::GetStringASCII(const std::string
& path
,
530 std::string
* out_value
) const {
532 if (!GetString(path
, &out
))
535 if (!IsStringASCII(out
)) {
540 out_value
->assign(out
);
544 bool DictionaryValue::GetBinary(const std::string
& path
,
545 const BinaryValue
** out_value
) const {
547 bool result
= Get(path
, &value
);
548 if (!result
|| !value
->IsType(TYPE_BINARY
))
552 *out_value
= static_cast<const BinaryValue
*>(value
);
557 bool DictionaryValue::GetBinary(const std::string
& path
,
558 BinaryValue
** out_value
) {
559 return static_cast<const DictionaryValue
&>(*this).GetBinary(
561 const_cast<const BinaryValue
**>(out_value
));
564 bool DictionaryValue::GetDictionary(const std::string
& path
,
565 const DictionaryValue
** out_value
) const {
567 bool result
= Get(path
, &value
);
568 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
572 *out_value
= static_cast<const DictionaryValue
*>(value
);
577 bool DictionaryValue::GetDictionary(const std::string
& path
,
578 DictionaryValue
** out_value
) {
579 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
581 const_cast<const DictionaryValue
**>(out_value
));
584 bool DictionaryValue::GetList(const std::string
& path
,
585 const ListValue
** out_value
) const {
587 bool result
= Get(path
, &value
);
588 if (!result
|| !value
->IsType(TYPE_LIST
))
592 *out_value
= static_cast<const ListValue
*>(value
);
597 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
598 return static_cast<const DictionaryValue
&>(*this).GetList(
600 const_cast<const ListValue
**>(out_value
));
603 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
604 const Value
** out_value
) const {
605 DCHECK(IsStringUTF8(key
));
606 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
607 if (entry_iterator
== dictionary_
.end())
610 const Value
* entry
= entry_iterator
->second
;
616 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
618 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
620 const_cast<const Value
**>(out_value
));
623 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
624 bool* out_value
) const {
626 if (!GetWithoutPathExpansion(key
, &value
))
629 return value
->GetAsBoolean(out_value
);
632 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
633 int* out_value
) const {
635 if (!GetWithoutPathExpansion(key
, &value
))
638 return value
->GetAsInteger(out_value
);
641 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
642 double* out_value
) const {
644 if (!GetWithoutPathExpansion(key
, &value
))
647 return value
->GetAsDouble(out_value
);
650 bool DictionaryValue::GetStringWithoutPathExpansion(
651 const std::string
& key
,
652 std::string
* out_value
) const {
654 if (!GetWithoutPathExpansion(key
, &value
))
657 return value
->GetAsString(out_value
);
660 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
661 string16
* out_value
) const {
663 if (!GetWithoutPathExpansion(key
, &value
))
666 return value
->GetAsString(out_value
);
669 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
670 const std::string
& key
,
671 const DictionaryValue
** out_value
) const {
673 bool result
= GetWithoutPathExpansion(key
, &value
);
674 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
678 *out_value
= static_cast<const DictionaryValue
*>(value
);
683 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
684 const std::string
& key
,
685 DictionaryValue
** out_value
) {
686 const DictionaryValue
& const_this
=
687 static_cast<const DictionaryValue
&>(*this);
688 return const_this
.GetDictionaryWithoutPathExpansion(
690 const_cast<const DictionaryValue
**>(out_value
));
693 bool DictionaryValue::GetListWithoutPathExpansion(
694 const std::string
& key
,
695 const ListValue
** out_value
) const {
697 bool result
= GetWithoutPathExpansion(key
, &value
);
698 if (!result
|| !value
->IsType(TYPE_LIST
))
702 *out_value
= static_cast<const ListValue
*>(value
);
707 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
708 ListValue
** out_value
) {
710 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
712 const_cast<const ListValue
**>(out_value
));
715 bool DictionaryValue::Remove(const std::string
& path
,
716 scoped_ptr
<Value
>* out_value
) {
717 DCHECK(IsStringUTF8(path
));
718 std::string
current_path(path
);
719 DictionaryValue
* current_dictionary
= this;
720 size_t delimiter_position
= current_path
.rfind('.');
721 if (delimiter_position
!= std::string::npos
) {
722 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
723 ¤t_dictionary
))
725 current_path
.erase(0, delimiter_position
+ 1);
728 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
732 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
733 scoped_ptr
<Value
>* out_value
) {
734 DCHECK(IsStringUTF8(key
));
735 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
736 if (entry_iterator
== dictionary_
.end())
739 Value
* entry
= entry_iterator
->second
;
741 out_value
->reset(entry
);
744 dictionary_
.erase(entry_iterator
);
748 bool DictionaryValue::RemovePath(const std::string
& path
,
749 scoped_ptr
<Value
>* out_value
) {
751 size_t delimiter_position
= path
.find('.');
753 if (delimiter_position
== std::string::npos
)
754 return RemoveWithoutPathExpansion(path
, out_value
);
756 const std::string subdict_path
= path
.substr(0, delimiter_position
);
757 DictionaryValue
* subdict
= NULL
;
758 if (!GetDictionary(subdict_path
, &subdict
))
760 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
762 if (result
&& subdict
->empty())
763 RemoveWithoutPathExpansion(subdict_path
, NULL
);
768 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
769 Value
* copy
= CopyWithoutEmptyChildren(this);
770 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
773 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
774 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
775 const Value
* merge_value
= &it
.value();
776 // Check whether we have to merge dictionaries.
777 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
778 DictionaryValue
* sub_dict
;
779 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
780 sub_dict
->MergeDictionary(
781 static_cast<const DictionaryValue
*>(merge_value
));
785 // All other cases: Make a copy and hook it up.
786 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
790 void DictionaryValue::Swap(DictionaryValue
* other
) {
791 dictionary_
.swap(other
->dictionary_
);
794 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
796 it_(target
.dictionary_
.begin()) {}
798 DictionaryValue::Iterator::~Iterator() {}
800 DictionaryValue
* DictionaryValue::DeepCopy() const {
801 DictionaryValue
* result
= new DictionaryValue
;
803 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
804 current_entry
!= dictionary_
.end(); ++current_entry
) {
805 result
->SetWithoutPathExpansion(current_entry
->first
,
806 current_entry
->second
->DeepCopy());
812 bool DictionaryValue::Equals(const Value
* other
) const {
813 if (other
->GetType() != GetType())
816 const DictionaryValue
* other_dict
=
817 static_cast<const DictionaryValue
*>(other
);
818 Iterator
lhs_it(*this);
819 Iterator
rhs_it(*other_dict
);
820 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
821 if (lhs_it
.key() != rhs_it
.key() ||
822 !lhs_it
.value().Equals(&rhs_it
.value())) {
828 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
834 ///////////////////// ListValue ////////////////////
836 ListValue::ListValue() : Value(TYPE_LIST
) {
839 ListValue::~ListValue() {
843 void ListValue::Clear() {
844 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
849 bool ListValue::Set(size_t index
, Value
* in_value
) {
853 if (index
>= list_
.size()) {
854 // Pad out any intermediate indexes with null settings
855 while (index
> list_
.size())
856 Append(CreateNullValue());
859 DCHECK(list_
[index
] != in_value
);
861 list_
[index
] = in_value
;
866 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
867 if (index
>= list_
.size())
871 *out_value
= list_
[index
];
876 bool ListValue::Get(size_t index
, Value
** out_value
) {
877 return static_cast<const ListValue
&>(*this).Get(
879 const_cast<const Value
**>(out_value
));
882 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
884 if (!Get(index
, &value
))
887 return value
->GetAsBoolean(bool_value
);
890 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
892 if (!Get(index
, &value
))
895 return value
->GetAsInteger(out_value
);
898 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
900 if (!Get(index
, &value
))
903 return value
->GetAsDouble(out_value
);
906 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
908 if (!Get(index
, &value
))
911 return value
->GetAsString(out_value
);
914 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
916 if (!Get(index
, &value
))
919 return value
->GetAsString(out_value
);
922 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
924 bool result
= Get(index
, &value
);
925 if (!result
|| !value
->IsType(TYPE_BINARY
))
929 *out_value
= static_cast<const BinaryValue
*>(value
);
934 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
935 return static_cast<const ListValue
&>(*this).GetBinary(
937 const_cast<const BinaryValue
**>(out_value
));
940 bool ListValue::GetDictionary(size_t index
,
941 const DictionaryValue
** out_value
) const {
943 bool result
= Get(index
, &value
);
944 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
948 *out_value
= static_cast<const DictionaryValue
*>(value
);
953 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
954 return static_cast<const ListValue
&>(*this).GetDictionary(
956 const_cast<const DictionaryValue
**>(out_value
));
959 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
961 bool result
= Get(index
, &value
);
962 if (!result
|| !value
->IsType(TYPE_LIST
))
966 *out_value
= static_cast<const ListValue
*>(value
);
971 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
972 return static_cast<const ListValue
&>(*this).GetList(
974 const_cast<const ListValue
**>(out_value
));
977 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
978 if (index
>= list_
.size())
982 out_value
->reset(list_
[index
]);
986 list_
.erase(list_
.begin() + index
);
990 bool ListValue::Remove(const Value
& value
, size_t* index
) {
991 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
992 if ((*i
)->Equals(&value
)) {
993 size_t previous_index
= i
- list_
.begin();
998 *index
= previous_index
;
1005 ListValue::iterator
ListValue::Erase(iterator iter
,
1006 scoped_ptr
<Value
>* out_value
) {
1008 out_value
->reset(*iter
);
1012 return list_
.erase(iter
);
1015 void ListValue::Append(Value
* in_value
) {
1017 list_
.push_back(in_value
);
1020 void ListValue::AppendBoolean(bool in_value
) {
1021 Append(new FundamentalValue(in_value
));
1024 void ListValue::AppendInteger(int in_value
) {
1025 Append(new FundamentalValue(in_value
));
1028 void ListValue::AppendDouble(double in_value
) {
1029 Append(new FundamentalValue(in_value
));
1032 void ListValue::AppendString(const std::string
& in_value
) {
1033 Append(new StringValue(in_value
));
1036 void ListValue::AppendString(const string16
& in_value
) {
1037 Append(new StringValue(in_value
));
1040 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1041 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1042 it
!= in_values
.end(); ++it
) {
1047 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1048 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1049 it
!= in_values
.end(); ++it
) {
1054 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1056 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1057 if ((*i
)->Equals(in_value
)) {
1062 list_
.push_back(in_value
);
1066 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1068 if (index
> list_
.size())
1071 list_
.insert(list_
.begin() + index
, in_value
);
1075 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1076 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1079 void ListValue::Swap(ListValue
* other
) {
1080 list_
.swap(other
->list_
);
1083 bool ListValue::GetAsList(ListValue
** out_value
) {
1089 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1095 ListValue
* ListValue::DeepCopy() const {
1096 ListValue
* result
= new ListValue
;
1098 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1099 result
->Append((*i
)->DeepCopy());
1104 bool ListValue::Equals(const Value
* other
) const {
1105 if (other
->GetType() != GetType())
1108 const ListValue
* other_list
=
1109 static_cast<const ListValue
*>(other
);
1110 const_iterator lhs_it
, rhs_it
;
1111 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1112 lhs_it
!= end() && rhs_it
!= other_list
->end();
1113 ++lhs_it
, ++rhs_it
) {
1114 if (!(*lhs_it
)->Equals(*rhs_it
))
1117 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1123 ValueSerializer::~ValueSerializer() {
1126 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1128 JSONWriter::WriteWithOptions(&value
,
1129 JSONWriter::OPTIONS_PRETTY_PRINT
,