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 scoped_ptr
<Value
> Value::CreateDeepCopy() const {
144 return make_scoped_ptr(DeepCopy());
147 bool Value::Equals(const Value
* other
) const {
148 // This method should only be getting called for null Values--all subclasses
149 // need to provide their own implementation;.
150 DCHECK(IsType(TYPE_NULL
));
151 return other
->IsType(TYPE_NULL
);
155 bool Value::Equals(const Value
* a
, const Value
* b
) {
156 if ((a
== NULL
) && (b
== NULL
)) return true;
157 if ((a
== NULL
) ^ (b
== NULL
)) return false;
161 Value::Value(Type type
) : type_(type
) {}
163 Value::Value(const Value
& that
) : type_(that
.type_
) {}
165 Value
& Value::operator=(const Value
& that
) {
170 ///////////////////// FundamentalValue ////////////////////
172 FundamentalValue::FundamentalValue(bool in_value
)
173 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
176 FundamentalValue::FundamentalValue(int in_value
)
177 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
180 FundamentalValue::FundamentalValue(double in_value
)
181 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
182 if (!std::isfinite(double_value_
)) {
183 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
184 << "values cannot be represented in JSON";
189 FundamentalValue::~FundamentalValue() {
192 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
193 if (out_value
&& IsType(TYPE_BOOLEAN
))
194 *out_value
= boolean_value_
;
195 return (IsType(TYPE_BOOLEAN
));
198 bool FundamentalValue::GetAsInteger(int* out_value
) const {
199 if (out_value
&& IsType(TYPE_INTEGER
))
200 *out_value
= integer_value_
;
201 return (IsType(TYPE_INTEGER
));
204 bool FundamentalValue::GetAsDouble(double* out_value
) const {
205 if (out_value
&& IsType(TYPE_DOUBLE
))
206 *out_value
= double_value_
;
207 else if (out_value
&& IsType(TYPE_INTEGER
))
208 *out_value
= integer_value_
;
209 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
212 FundamentalValue
* FundamentalValue::DeepCopy() const {
215 return new FundamentalValue(boolean_value_
);
218 return new FundamentalValue(integer_value_
);
221 return new FundamentalValue(double_value_
);
229 bool FundamentalValue::Equals(const Value
* other
) const {
230 if (other
->GetType() != GetType())
236 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
240 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
244 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
252 ///////////////////// StringValue ////////////////////
254 StringValue::StringValue(const std::string
& in_value
)
255 : Value(TYPE_STRING
),
257 DCHECK(IsStringUTF8(in_value
));
260 StringValue::StringValue(const string16
& in_value
)
261 : Value(TYPE_STRING
),
262 value_(UTF16ToUTF8(in_value
)) {
265 StringValue::~StringValue() {
268 std::string
* StringValue::GetString() {
272 const std::string
& StringValue::GetString() const {
276 bool StringValue::GetAsString(std::string
* out_value
) const {
282 bool StringValue::GetAsString(string16
* out_value
) const {
284 *out_value
= UTF8ToUTF16(value_
);
288 bool StringValue::GetAsString(const StringValue
** out_value
) const {
294 StringValue
* StringValue::DeepCopy() const {
295 return new StringValue(value_
);
298 bool StringValue::Equals(const Value
* other
) const {
299 if (other
->GetType() != GetType())
301 std::string lhs
, rhs
;
302 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
305 ///////////////////// BinaryValue ////////////////////
307 BinaryValue::BinaryValue()
308 : Value(TYPE_BINARY
),
312 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
313 : Value(TYPE_BINARY
),
314 buffer_(buffer
.Pass()),
318 BinaryValue::~BinaryValue() {
322 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
324 char* buffer_copy
= new char[size
];
325 memcpy(buffer_copy
, buffer
, size
);
326 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
327 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
330 bool BinaryValue::GetAsBinary(const BinaryValue
** out_value
) const {
336 BinaryValue
* BinaryValue::DeepCopy() const {
337 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
340 bool BinaryValue::Equals(const Value
* other
) const {
341 if (other
->GetType() != GetType())
343 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
344 if (other_binary
->size_
!= size_
)
346 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
349 ///////////////////// DictionaryValue ////////////////////
351 DictionaryValue::DictionaryValue()
352 : Value(TYPE_DICTIONARY
) {
355 DictionaryValue::~DictionaryValue() {
359 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
365 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
371 bool DictionaryValue::HasKey(const std::string
& key
) const {
372 DCHECK(IsStringUTF8(key
));
373 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
374 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
375 return current_entry
!= dictionary_
.end();
378 void DictionaryValue::Clear() {
379 ValueMap::iterator dict_iterator
= dictionary_
.begin();
380 while (dict_iterator
!= dictionary_
.end()) {
381 delete dict_iterator
->second
;
388 void DictionaryValue::Set(const std::string
& path
, scoped_ptr
<Value
> in_value
) {
389 DCHECK(IsStringUTF8(path
));
392 std::string
current_path(path
);
393 DictionaryValue
* current_dictionary
= this;
394 for (size_t delimiter_position
= current_path
.find('.');
395 delimiter_position
!= std::string::npos
;
396 delimiter_position
= current_path
.find('.')) {
397 // Assume that we're indexing into a dictionary.
398 std::string
key(current_path
, 0, delimiter_position
);
399 DictionaryValue
* child_dictionary
= NULL
;
400 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
401 child_dictionary
= new DictionaryValue
;
402 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
405 current_dictionary
= child_dictionary
;
406 current_path
.erase(0, delimiter_position
+ 1);
409 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
.Pass());
412 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
413 Set(path
, make_scoped_ptr(in_value
));
416 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
417 Set(path
, new FundamentalValue(in_value
));
420 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
421 Set(path
, new FundamentalValue(in_value
));
424 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
425 Set(path
, new FundamentalValue(in_value
));
428 void DictionaryValue::SetString(const std::string
& path
,
429 const std::string
& in_value
) {
430 Set(path
, new StringValue(in_value
));
433 void DictionaryValue::SetString(const std::string
& path
,
434 const string16
& in_value
) {
435 Set(path
, new StringValue(in_value
));
438 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
439 scoped_ptr
<Value
> in_value
) {
440 Value
* bare_ptr
= in_value
.release();
441 // If there's an existing value here, we need to delete it, because
442 // we own all our children.
443 std::pair
<ValueMap::iterator
, bool> ins_res
=
444 dictionary_
.insert(std::make_pair(key
, bare_ptr
));
445 if (!ins_res
.second
) {
446 DCHECK_NE(ins_res
.first
->second
, bare_ptr
); // This would be bogus
447 delete ins_res
.first
->second
;
448 ins_res
.first
->second
= bare_ptr
;
452 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
454 SetWithoutPathExpansion(key
, make_scoped_ptr(in_value
));
457 void DictionaryValue::SetBooleanWithoutPathExpansion(
458 const std::string
& path
, bool in_value
) {
459 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
462 void DictionaryValue::SetIntegerWithoutPathExpansion(
463 const std::string
& path
, int in_value
) {
464 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
467 void DictionaryValue::SetDoubleWithoutPathExpansion(
468 const std::string
& path
, double in_value
) {
469 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
472 void DictionaryValue::SetStringWithoutPathExpansion(
473 const std::string
& path
, const std::string
& in_value
) {
474 SetWithoutPathExpansion(path
, new StringValue(in_value
));
477 void DictionaryValue::SetStringWithoutPathExpansion(
478 const std::string
& path
, const string16
& in_value
) {
479 SetWithoutPathExpansion(path
, new StringValue(in_value
));
482 bool DictionaryValue::Get(const std::string
& path
,
483 const Value
** out_value
) const {
484 DCHECK(IsStringUTF8(path
));
485 std::string
current_path(path
);
486 const DictionaryValue
* current_dictionary
= this;
487 for (size_t delimiter_position
= current_path
.find('.');
488 delimiter_position
!= std::string::npos
;
489 delimiter_position
= current_path
.find('.')) {
490 const DictionaryValue
* child_dictionary
= NULL
;
491 if (!current_dictionary
->GetDictionary(
492 current_path
.substr(0, delimiter_position
), &child_dictionary
))
495 current_dictionary
= child_dictionary
;
496 current_path
.erase(0, delimiter_position
+ 1);
499 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
502 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
503 return static_cast<const DictionaryValue
&>(*this).Get(
505 const_cast<const Value
**>(out_value
));
508 bool DictionaryValue::GetBoolean(const std::string
& path
,
509 bool* bool_value
) const {
511 if (!Get(path
, &value
))
514 return value
->GetAsBoolean(bool_value
);
517 bool DictionaryValue::GetInteger(const std::string
& path
,
518 int* out_value
) const {
520 if (!Get(path
, &value
))
523 return value
->GetAsInteger(out_value
);
526 bool DictionaryValue::GetDouble(const std::string
& path
,
527 double* out_value
) const {
529 if (!Get(path
, &value
))
532 return value
->GetAsDouble(out_value
);
535 bool DictionaryValue::GetString(const std::string
& path
,
536 std::string
* out_value
) const {
538 if (!Get(path
, &value
))
541 return value
->GetAsString(out_value
);
544 bool DictionaryValue::GetString(const std::string
& path
,
545 string16
* out_value
) const {
547 if (!Get(path
, &value
))
550 return value
->GetAsString(out_value
);
553 bool DictionaryValue::GetStringASCII(const std::string
& path
,
554 std::string
* out_value
) const {
556 if (!GetString(path
, &out
))
559 if (!IsStringASCII(out
)) {
564 out_value
->assign(out
);
568 bool DictionaryValue::GetBinary(const std::string
& path
,
569 const BinaryValue
** out_value
) const {
571 bool result
= Get(path
, &value
);
572 if (!result
|| !value
->IsType(TYPE_BINARY
))
576 *out_value
= static_cast<const BinaryValue
*>(value
);
581 bool DictionaryValue::GetBinary(const std::string
& path
,
582 BinaryValue
** out_value
) {
583 return static_cast<const DictionaryValue
&>(*this).GetBinary(
585 const_cast<const BinaryValue
**>(out_value
));
588 bool DictionaryValue::GetDictionary(const std::string
& path
,
589 const DictionaryValue
** out_value
) const {
591 bool result
= Get(path
, &value
);
592 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
596 *out_value
= static_cast<const DictionaryValue
*>(value
);
601 bool DictionaryValue::GetDictionary(const std::string
& path
,
602 DictionaryValue
** out_value
) {
603 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
605 const_cast<const DictionaryValue
**>(out_value
));
608 bool DictionaryValue::GetList(const std::string
& path
,
609 const ListValue
** out_value
) const {
611 bool result
= Get(path
, &value
);
612 if (!result
|| !value
->IsType(TYPE_LIST
))
616 *out_value
= static_cast<const ListValue
*>(value
);
621 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
622 return static_cast<const DictionaryValue
&>(*this).GetList(
624 const_cast<const ListValue
**>(out_value
));
627 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
628 const Value
** out_value
) const {
629 DCHECK(IsStringUTF8(key
));
630 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
631 if (entry_iterator
== dictionary_
.end())
634 const Value
* entry
= entry_iterator
->second
;
640 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
642 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
644 const_cast<const Value
**>(out_value
));
647 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
648 bool* out_value
) const {
650 if (!GetWithoutPathExpansion(key
, &value
))
653 return value
->GetAsBoolean(out_value
);
656 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
657 int* out_value
) const {
659 if (!GetWithoutPathExpansion(key
, &value
))
662 return value
->GetAsInteger(out_value
);
665 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
666 double* out_value
) const {
668 if (!GetWithoutPathExpansion(key
, &value
))
671 return value
->GetAsDouble(out_value
);
674 bool DictionaryValue::GetStringWithoutPathExpansion(
675 const std::string
& key
,
676 std::string
* out_value
) const {
678 if (!GetWithoutPathExpansion(key
, &value
))
681 return value
->GetAsString(out_value
);
684 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
685 string16
* out_value
) const {
687 if (!GetWithoutPathExpansion(key
, &value
))
690 return value
->GetAsString(out_value
);
693 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
694 const std::string
& key
,
695 const DictionaryValue
** out_value
) const {
697 bool result
= GetWithoutPathExpansion(key
, &value
);
698 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
702 *out_value
= static_cast<const DictionaryValue
*>(value
);
707 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
708 const std::string
& key
,
709 DictionaryValue
** out_value
) {
710 const DictionaryValue
& const_this
=
711 static_cast<const DictionaryValue
&>(*this);
712 return const_this
.GetDictionaryWithoutPathExpansion(
714 const_cast<const DictionaryValue
**>(out_value
));
717 bool DictionaryValue::GetListWithoutPathExpansion(
718 const std::string
& key
,
719 const ListValue
** out_value
) const {
721 bool result
= GetWithoutPathExpansion(key
, &value
);
722 if (!result
|| !value
->IsType(TYPE_LIST
))
726 *out_value
= static_cast<const ListValue
*>(value
);
731 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
732 ListValue
** out_value
) {
734 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
736 const_cast<const ListValue
**>(out_value
));
739 bool DictionaryValue::Remove(const std::string
& path
,
740 scoped_ptr
<Value
>* out_value
) {
741 DCHECK(IsStringUTF8(path
));
742 std::string
current_path(path
);
743 DictionaryValue
* current_dictionary
= this;
744 size_t delimiter_position
= current_path
.rfind('.');
745 if (delimiter_position
!= std::string::npos
) {
746 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
747 ¤t_dictionary
))
749 current_path
.erase(0, delimiter_position
+ 1);
752 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
756 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
757 scoped_ptr
<Value
>* out_value
) {
758 DCHECK(IsStringUTF8(key
));
759 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
760 if (entry_iterator
== dictionary_
.end())
763 Value
* entry
= entry_iterator
->second
;
765 out_value
->reset(entry
);
768 dictionary_
.erase(entry_iterator
);
772 bool DictionaryValue::RemovePath(const std::string
& path
,
773 scoped_ptr
<Value
>* out_value
) {
775 size_t delimiter_position
= path
.find('.');
777 if (delimiter_position
== std::string::npos
)
778 return RemoveWithoutPathExpansion(path
, out_value
);
780 const std::string subdict_path
= path
.substr(0, delimiter_position
);
781 DictionaryValue
* subdict
= NULL
;
782 if (!GetDictionary(subdict_path
, &subdict
))
784 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
786 if (result
&& subdict
->empty())
787 RemoveWithoutPathExpansion(subdict_path
, NULL
);
792 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
793 Value
* copy
= CopyWithoutEmptyChildren(this);
794 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
797 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
798 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
799 const Value
* merge_value
= &it
.value();
800 // Check whether we have to merge dictionaries.
801 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
802 DictionaryValue
* sub_dict
;
803 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
804 sub_dict
->MergeDictionary(
805 static_cast<const DictionaryValue
*>(merge_value
));
809 // All other cases: Make a copy and hook it up.
810 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
814 void DictionaryValue::Swap(DictionaryValue
* other
) {
815 dictionary_
.swap(other
->dictionary_
);
818 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
820 it_(target
.dictionary_
.begin()) {}
822 DictionaryValue::Iterator::~Iterator() {}
824 DictionaryValue
* DictionaryValue::DeepCopy() const {
825 DictionaryValue
* result
= new DictionaryValue
;
827 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
828 current_entry
!= dictionary_
.end(); ++current_entry
) {
829 result
->SetWithoutPathExpansion(current_entry
->first
,
830 current_entry
->second
->DeepCopy());
836 scoped_ptr
<DictionaryValue
> DictionaryValue::CreateDeepCopy() const {
837 return make_scoped_ptr(DeepCopy());
840 bool DictionaryValue::Equals(const Value
* other
) const {
841 if (other
->GetType() != GetType())
844 const DictionaryValue
* other_dict
=
845 static_cast<const DictionaryValue
*>(other
);
846 Iterator
lhs_it(*this);
847 Iterator
rhs_it(*other_dict
);
848 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
849 if (lhs_it
.key() != rhs_it
.key() ||
850 !lhs_it
.value().Equals(&rhs_it
.value())) {
856 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
862 ///////////////////// ListValue ////////////////////
864 ListValue::ListValue() : Value(TYPE_LIST
) {
867 ListValue::~ListValue() {
871 void ListValue::Clear() {
872 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
877 bool ListValue::Set(size_t index
, Value
* in_value
) {
881 if (index
>= list_
.size()) {
882 // Pad out any intermediate indexes with null settings
883 while (index
> list_
.size())
884 Append(CreateNullValue());
887 DCHECK(list_
[index
] != in_value
);
889 list_
[index
] = in_value
;
894 bool ListValue::Set(size_t index
, scoped_ptr
<Value
> in_value
) {
895 return Set(index
, in_value
.release());
898 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
899 if (index
>= list_
.size())
903 *out_value
= list_
[index
];
908 bool ListValue::Get(size_t index
, Value
** out_value
) {
909 return static_cast<const ListValue
&>(*this).Get(
911 const_cast<const Value
**>(out_value
));
914 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
916 if (!Get(index
, &value
))
919 return value
->GetAsBoolean(bool_value
);
922 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
924 if (!Get(index
, &value
))
927 return value
->GetAsInteger(out_value
);
930 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
932 if (!Get(index
, &value
))
935 return value
->GetAsDouble(out_value
);
938 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
940 if (!Get(index
, &value
))
943 return value
->GetAsString(out_value
);
946 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
948 if (!Get(index
, &value
))
951 return value
->GetAsString(out_value
);
954 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
956 bool result
= Get(index
, &value
);
957 if (!result
|| !value
->IsType(TYPE_BINARY
))
961 *out_value
= static_cast<const BinaryValue
*>(value
);
966 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
967 return static_cast<const ListValue
&>(*this).GetBinary(
969 const_cast<const BinaryValue
**>(out_value
));
972 bool ListValue::GetDictionary(size_t index
,
973 const DictionaryValue
** out_value
) const {
975 bool result
= Get(index
, &value
);
976 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
980 *out_value
= static_cast<const DictionaryValue
*>(value
);
985 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
986 return static_cast<const ListValue
&>(*this).GetDictionary(
988 const_cast<const DictionaryValue
**>(out_value
));
991 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
993 bool result
= Get(index
, &value
);
994 if (!result
|| !value
->IsType(TYPE_LIST
))
998 *out_value
= static_cast<const ListValue
*>(value
);
1003 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
1004 return static_cast<const ListValue
&>(*this).GetList(
1006 const_cast<const ListValue
**>(out_value
));
1009 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
1010 if (index
>= list_
.size())
1014 out_value
->reset(list_
[index
]);
1016 delete list_
[index
];
1018 list_
.erase(list_
.begin() + index
);
1022 bool ListValue::Remove(const Value
& value
, size_t* index
) {
1023 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1024 if ((*i
)->Equals(&value
)) {
1025 size_t previous_index
= i
- list_
.begin();
1030 *index
= previous_index
;
1037 ListValue::iterator
ListValue::Erase(iterator iter
,
1038 scoped_ptr
<Value
>* out_value
) {
1040 out_value
->reset(*iter
);
1044 return list_
.erase(iter
);
1047 void ListValue::Append(scoped_ptr
<Value
> in_value
) {
1048 Append(in_value
.release());
1051 void ListValue::Append(Value
* in_value
) {
1053 list_
.push_back(in_value
);
1056 void ListValue::AppendBoolean(bool in_value
) {
1057 Append(new FundamentalValue(in_value
));
1060 void ListValue::AppendInteger(int in_value
) {
1061 Append(new FundamentalValue(in_value
));
1064 void ListValue::AppendDouble(double in_value
) {
1065 Append(new FundamentalValue(in_value
));
1068 void ListValue::AppendString(const std::string
& in_value
) {
1069 Append(new StringValue(in_value
));
1072 void ListValue::AppendString(const string16
& in_value
) {
1073 Append(new StringValue(in_value
));
1076 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1077 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1078 it
!= in_values
.end(); ++it
) {
1083 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1084 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1085 it
!= in_values
.end(); ++it
) {
1090 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1092 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1093 if ((*i
)->Equals(in_value
)) {
1098 list_
.push_back(in_value
);
1102 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1104 if (index
> list_
.size())
1107 list_
.insert(list_
.begin() + index
, in_value
);
1111 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1112 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1115 void ListValue::Swap(ListValue
* other
) {
1116 list_
.swap(other
->list_
);
1119 bool ListValue::GetAsList(ListValue
** out_value
) {
1125 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1131 ListValue
* ListValue::DeepCopy() const {
1132 ListValue
* result
= new ListValue
;
1134 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1135 result
->Append((*i
)->DeepCopy());
1140 bool ListValue::Equals(const Value
* other
) const {
1141 if (other
->GetType() != GetType())
1144 const ListValue
* other_list
=
1145 static_cast<const ListValue
*>(other
);
1146 const_iterator lhs_it
, rhs_it
;
1147 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1148 lhs_it
!= end() && rhs_it
!= other_list
->end();
1149 ++lhs_it
, ++rhs_it
) {
1150 if (!(*lhs_it
)->Equals(*rhs_it
))
1153 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1159 ValueSerializer::~ValueSerializer() {
1162 ValueDeserializer::~ValueDeserializer() {
1165 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1167 JSONWriter::WriteWithOptions(&value
,
1168 JSONWriter::OPTIONS_PRETTY_PRINT
,