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 scoped_ptr
<Value
> CopyWithoutEmptyChildren(const Value
& node
);
25 // Make a deep copy of |node|, but don't include empty lists or dictionaries
26 // in the copy. It's possible for this function to return NULL and it
27 // expects |node| to always be non-NULL.
28 scoped_ptr
<ListValue
> CopyListWithoutEmptyChildren(const ListValue
& list
) {
29 scoped_ptr
<ListValue
> copy
;
30 for (ListValue::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
31 scoped_ptr
<Value
> child_copy
= CopyWithoutEmptyChildren(**it
);
34 copy
.reset(new ListValue
);
35 copy
->Append(child_copy
.Pass());
41 scoped_ptr
<DictionaryValue
> CopyDictionaryWithoutEmptyChildren(
42 const DictionaryValue
& dict
) {
43 scoped_ptr
<DictionaryValue
> copy
;
44 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
45 scoped_ptr
<Value
> child_copy
= CopyWithoutEmptyChildren(it
.value());
48 copy
.reset(new DictionaryValue
);
49 copy
->SetWithoutPathExpansion(it
.key(), child_copy
.Pass());
55 scoped_ptr
<Value
> CopyWithoutEmptyChildren(const Value
& node
) {
56 switch (node
.GetType()) {
57 case Value::TYPE_LIST
:
58 return CopyListWithoutEmptyChildren(static_cast<const ListValue
&>(node
));
60 case Value::TYPE_DICTIONARY
:
61 return CopyDictionaryWithoutEmptyChildren(
62 static_cast<const DictionaryValue
&>(node
));
65 return node
.CreateDeepCopy();
69 // A small functor for comparing Values for std::find_if and similar.
72 // Pass the value against which all consecutive calls of the () operator will
73 // compare their argument to. This Value object must not be destroyed while
74 // the ValueEquals is in use.
75 explicit ValueEquals(const Value
* first
) : first_(first
) { }
77 bool operator ()(const Value
* second
) const {
78 return first_
->Equals(second
);
91 scoped_ptr
<Value
> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL
));
95 bool Value::GetAsBinary(const BinaryValue
** out_value
) const {
99 bool Value::GetAsBoolean(bool* out_value
) const {
103 bool Value::GetAsInteger(int* out_value
) const {
107 bool Value::GetAsDouble(double* out_value
) const {
111 bool Value::GetAsString(std::string
* out_value
) const {
115 bool Value::GetAsString(string16
* out_value
) const {
119 bool Value::GetAsString(const StringValue
** out_value
) const {
123 bool Value::GetAsList(ListValue
** out_value
) {
127 bool Value::GetAsList(const ListValue
** out_value
) const {
131 bool Value::GetAsDictionary(DictionaryValue
** out_value
) {
135 bool Value::GetAsDictionary(const DictionaryValue
** out_value
) const {
139 Value
* Value::DeepCopy() 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 CreateNullValue().release();
146 scoped_ptr
<Value
> Value::CreateDeepCopy() const {
147 return make_scoped_ptr(DeepCopy());
150 bool Value::Equals(const Value
* other
) const {
151 // This method should only be getting called for null Values--all subclasses
152 // need to provide their own implementation;.
153 DCHECK(IsType(TYPE_NULL
));
154 return other
->IsType(TYPE_NULL
);
158 bool Value::Equals(const Value
* a
, const Value
* b
) {
159 if ((a
== NULL
) && (b
== NULL
)) return true;
160 if ((a
== NULL
) ^ (b
== NULL
)) return false;
164 Value::Value(Type type
) : type_(type
) {}
166 Value::Value(const Value
& that
) : type_(that
.type_
) {}
168 Value
& Value::operator=(const Value
& that
) {
173 ///////////////////// FundamentalValue ////////////////////
175 FundamentalValue::FundamentalValue(bool in_value
)
176 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
179 FundamentalValue::FundamentalValue(int in_value
)
180 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
183 FundamentalValue::FundamentalValue(double in_value
)
184 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
185 if (!std::isfinite(double_value_
)) {
186 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
187 << "values cannot be represented in JSON";
192 FundamentalValue::~FundamentalValue() {
195 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
196 if (out_value
&& IsType(TYPE_BOOLEAN
))
197 *out_value
= boolean_value_
;
198 return (IsType(TYPE_BOOLEAN
));
201 bool FundamentalValue::GetAsInteger(int* out_value
) const {
202 if (out_value
&& IsType(TYPE_INTEGER
))
203 *out_value
= integer_value_
;
204 return (IsType(TYPE_INTEGER
));
207 bool FundamentalValue::GetAsDouble(double* out_value
) const {
208 if (out_value
&& IsType(TYPE_DOUBLE
))
209 *out_value
= double_value_
;
210 else if (out_value
&& IsType(TYPE_INTEGER
))
211 *out_value
= integer_value_
;
212 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
215 FundamentalValue
* FundamentalValue::DeepCopy() const {
218 return new FundamentalValue(boolean_value_
);
221 return new FundamentalValue(integer_value_
);
224 return new FundamentalValue(double_value_
);
232 bool FundamentalValue::Equals(const Value
* other
) const {
233 if (other
->GetType() != GetType())
239 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
243 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
247 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
255 ///////////////////// StringValue ////////////////////
257 StringValue::StringValue(const std::string
& in_value
)
258 : Value(TYPE_STRING
),
260 DCHECK(IsStringUTF8(in_value
));
263 StringValue::StringValue(const string16
& in_value
)
264 : Value(TYPE_STRING
),
265 value_(UTF16ToUTF8(in_value
)) {
268 StringValue::~StringValue() {
271 std::string
* StringValue::GetString() {
275 const std::string
& StringValue::GetString() const {
279 bool StringValue::GetAsString(std::string
* out_value
) const {
285 bool StringValue::GetAsString(string16
* out_value
) const {
287 *out_value
= UTF8ToUTF16(value_
);
291 bool StringValue::GetAsString(const StringValue
** out_value
) const {
297 StringValue
* StringValue::DeepCopy() const {
298 return new StringValue(value_
);
301 bool StringValue::Equals(const Value
* other
) const {
302 if (other
->GetType() != GetType())
304 std::string lhs
, rhs
;
305 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
308 ///////////////////// BinaryValue ////////////////////
310 BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY
),
315 BinaryValue::BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
)
316 : Value(TYPE_BINARY
),
317 buffer_(buffer
.Pass()),
321 BinaryValue::~BinaryValue() {
325 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
327 char* buffer_copy
= new char[size
];
328 memcpy(buffer_copy
, buffer
, size
);
329 scoped_ptr
<char[]> scoped_buffer_copy(buffer_copy
);
330 return new BinaryValue(scoped_buffer_copy
.Pass(), size
);
333 bool BinaryValue::GetAsBinary(const BinaryValue
** out_value
) const {
339 BinaryValue
* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_
.get(), size_
);
343 bool BinaryValue::Equals(const Value
* other
) const {
344 if (other
->GetType() != GetType())
346 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
347 if (other_binary
->size_
!= size_
)
349 return !memcmp(GetBuffer(), other_binary
->GetBuffer(), size_
);
352 ///////////////////// DictionaryValue ////////////////////
354 DictionaryValue::DictionaryValue()
355 : Value(TYPE_DICTIONARY
) {
358 DictionaryValue::~DictionaryValue() {
362 bool DictionaryValue::GetAsDictionary(DictionaryValue
** out_value
) {
368 bool DictionaryValue::GetAsDictionary(const DictionaryValue
** out_value
) const {
374 bool DictionaryValue::HasKey(const std::string
& key
) const {
375 DCHECK(IsStringUTF8(key
));
376 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
377 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
378 return current_entry
!= dictionary_
.end();
381 void DictionaryValue::Clear() {
382 ValueMap::iterator dict_iterator
= dictionary_
.begin();
383 while (dict_iterator
!= dictionary_
.end()) {
384 delete dict_iterator
->second
;
391 void DictionaryValue::Set(const std::string
& path
, scoped_ptr
<Value
> in_value
) {
392 DCHECK(IsStringUTF8(path
));
395 std::string
current_path(path
);
396 DictionaryValue
* current_dictionary
= this;
397 for (size_t delimiter_position
= current_path
.find('.');
398 delimiter_position
!= std::string::npos
;
399 delimiter_position
= current_path
.find('.')) {
400 // Assume that we're indexing into a dictionary.
401 std::string
key(current_path
, 0, delimiter_position
);
402 DictionaryValue
* child_dictionary
= NULL
;
403 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
404 child_dictionary
= new DictionaryValue
;
405 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
408 current_dictionary
= child_dictionary
;
409 current_path
.erase(0, delimiter_position
+ 1);
412 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
.Pass());
415 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
416 Set(path
, make_scoped_ptr(in_value
));
419 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
420 Set(path
, new FundamentalValue(in_value
));
423 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
424 Set(path
, new FundamentalValue(in_value
));
427 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
428 Set(path
, new FundamentalValue(in_value
));
431 void DictionaryValue::SetString(const std::string
& path
,
432 const std::string
& in_value
) {
433 Set(path
, new StringValue(in_value
));
436 void DictionaryValue::SetString(const std::string
& path
,
437 const string16
& in_value
) {
438 Set(path
, new StringValue(in_value
));
441 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
442 scoped_ptr
<Value
> in_value
) {
443 Value
* bare_ptr
= in_value
.release();
444 // If there's an existing value here, we need to delete it, because
445 // we own all our children.
446 std::pair
<ValueMap::iterator
, bool> ins_res
=
447 dictionary_
.insert(std::make_pair(key
, bare_ptr
));
448 if (!ins_res
.second
) {
449 DCHECK_NE(ins_res
.first
->second
, bare_ptr
); // This would be bogus
450 delete ins_res
.first
->second
;
451 ins_res
.first
->second
= bare_ptr
;
455 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
457 SetWithoutPathExpansion(key
, make_scoped_ptr(in_value
));
460 void DictionaryValue::SetBooleanWithoutPathExpansion(
461 const std::string
& path
, bool in_value
) {
462 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
465 void DictionaryValue::SetIntegerWithoutPathExpansion(
466 const std::string
& path
, int in_value
) {
467 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
470 void DictionaryValue::SetDoubleWithoutPathExpansion(
471 const std::string
& path
, double in_value
) {
472 SetWithoutPathExpansion(path
, new FundamentalValue(in_value
));
475 void DictionaryValue::SetStringWithoutPathExpansion(
476 const std::string
& path
, const std::string
& in_value
) {
477 SetWithoutPathExpansion(path
, new StringValue(in_value
));
480 void DictionaryValue::SetStringWithoutPathExpansion(
481 const std::string
& path
, const string16
& in_value
) {
482 SetWithoutPathExpansion(path
, new StringValue(in_value
));
485 bool DictionaryValue::Get(const std::string
& path
,
486 const Value
** out_value
) const {
487 DCHECK(IsStringUTF8(path
));
488 std::string
current_path(path
);
489 const DictionaryValue
* current_dictionary
= this;
490 for (size_t delimiter_position
= current_path
.find('.');
491 delimiter_position
!= std::string::npos
;
492 delimiter_position
= current_path
.find('.')) {
493 const DictionaryValue
* child_dictionary
= NULL
;
494 if (!current_dictionary
->GetDictionary(
495 current_path
.substr(0, delimiter_position
), &child_dictionary
))
498 current_dictionary
= child_dictionary
;
499 current_path
.erase(0, delimiter_position
+ 1);
502 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
505 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) {
506 return static_cast<const DictionaryValue
&>(*this).Get(
508 const_cast<const Value
**>(out_value
));
511 bool DictionaryValue::GetBoolean(const std::string
& path
,
512 bool* bool_value
) const {
514 if (!Get(path
, &value
))
517 return value
->GetAsBoolean(bool_value
);
520 bool DictionaryValue::GetInteger(const std::string
& path
,
521 int* out_value
) const {
523 if (!Get(path
, &value
))
526 return value
->GetAsInteger(out_value
);
529 bool DictionaryValue::GetDouble(const std::string
& path
,
530 double* out_value
) const {
532 if (!Get(path
, &value
))
535 return value
->GetAsDouble(out_value
);
538 bool DictionaryValue::GetString(const std::string
& path
,
539 std::string
* out_value
) const {
541 if (!Get(path
, &value
))
544 return value
->GetAsString(out_value
);
547 bool DictionaryValue::GetString(const std::string
& path
,
548 string16
* out_value
) const {
550 if (!Get(path
, &value
))
553 return value
->GetAsString(out_value
);
556 bool DictionaryValue::GetStringASCII(const std::string
& path
,
557 std::string
* out_value
) const {
559 if (!GetString(path
, &out
))
562 if (!IsStringASCII(out
)) {
567 out_value
->assign(out
);
571 bool DictionaryValue::GetBinary(const std::string
& path
,
572 const BinaryValue
** out_value
) const {
574 bool result
= Get(path
, &value
);
575 if (!result
|| !value
->IsType(TYPE_BINARY
))
579 *out_value
= static_cast<const BinaryValue
*>(value
);
584 bool DictionaryValue::GetBinary(const std::string
& path
,
585 BinaryValue
** out_value
) {
586 return static_cast<const DictionaryValue
&>(*this).GetBinary(
588 const_cast<const BinaryValue
**>(out_value
));
591 bool DictionaryValue::GetDictionary(const std::string
& path
,
592 const DictionaryValue
** out_value
) const {
594 bool result
= Get(path
, &value
);
595 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
599 *out_value
= static_cast<const DictionaryValue
*>(value
);
604 bool DictionaryValue::GetDictionary(const std::string
& path
,
605 DictionaryValue
** out_value
) {
606 return static_cast<const DictionaryValue
&>(*this).GetDictionary(
608 const_cast<const DictionaryValue
**>(out_value
));
611 bool DictionaryValue::GetList(const std::string
& path
,
612 const ListValue
** out_value
) const {
614 bool result
= Get(path
, &value
);
615 if (!result
|| !value
->IsType(TYPE_LIST
))
619 *out_value
= static_cast<const ListValue
*>(value
);
624 bool DictionaryValue::GetList(const std::string
& path
, ListValue
** out_value
) {
625 return static_cast<const DictionaryValue
&>(*this).GetList(
627 const_cast<const ListValue
**>(out_value
));
630 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
631 const Value
** out_value
) const {
632 DCHECK(IsStringUTF8(key
));
633 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
634 if (entry_iterator
== dictionary_
.end())
637 const Value
* entry
= entry_iterator
->second
;
643 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
645 return static_cast<const DictionaryValue
&>(*this).GetWithoutPathExpansion(
647 const_cast<const Value
**>(out_value
));
650 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string
& key
,
651 bool* out_value
) const {
653 if (!GetWithoutPathExpansion(key
, &value
))
656 return value
->GetAsBoolean(out_value
);
659 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
660 int* out_value
) const {
662 if (!GetWithoutPathExpansion(key
, &value
))
665 return value
->GetAsInteger(out_value
);
668 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
669 double* out_value
) const {
671 if (!GetWithoutPathExpansion(key
, &value
))
674 return value
->GetAsDouble(out_value
);
677 bool DictionaryValue::GetStringWithoutPathExpansion(
678 const std::string
& key
,
679 std::string
* out_value
) const {
681 if (!GetWithoutPathExpansion(key
, &value
))
684 return value
->GetAsString(out_value
);
687 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string
& key
,
688 string16
* out_value
) const {
690 if (!GetWithoutPathExpansion(key
, &value
))
693 return value
->GetAsString(out_value
);
696 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
697 const std::string
& key
,
698 const DictionaryValue
** out_value
) const {
700 bool result
= GetWithoutPathExpansion(key
, &value
);
701 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
705 *out_value
= static_cast<const DictionaryValue
*>(value
);
710 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
711 const std::string
& key
,
712 DictionaryValue
** out_value
) {
713 const DictionaryValue
& const_this
=
714 static_cast<const DictionaryValue
&>(*this);
715 return const_this
.GetDictionaryWithoutPathExpansion(
717 const_cast<const DictionaryValue
**>(out_value
));
720 bool DictionaryValue::GetListWithoutPathExpansion(
721 const std::string
& key
,
722 const ListValue
** out_value
) const {
724 bool result
= GetWithoutPathExpansion(key
, &value
);
725 if (!result
|| !value
->IsType(TYPE_LIST
))
729 *out_value
= static_cast<const ListValue
*>(value
);
734 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
735 ListValue
** out_value
) {
737 static_cast<const DictionaryValue
&>(*this).GetListWithoutPathExpansion(
739 const_cast<const ListValue
**>(out_value
));
742 bool DictionaryValue::Remove(const std::string
& path
,
743 scoped_ptr
<Value
>* out_value
) {
744 DCHECK(IsStringUTF8(path
));
745 std::string
current_path(path
);
746 DictionaryValue
* current_dictionary
= this;
747 size_t delimiter_position
= current_path
.rfind('.');
748 if (delimiter_position
!= std::string::npos
) {
749 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
750 ¤t_dictionary
))
752 current_path
.erase(0, delimiter_position
+ 1);
755 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
759 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
760 scoped_ptr
<Value
>* out_value
) {
761 DCHECK(IsStringUTF8(key
));
762 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
763 if (entry_iterator
== dictionary_
.end())
766 Value
* entry
= entry_iterator
->second
;
768 out_value
->reset(entry
);
771 dictionary_
.erase(entry_iterator
);
775 bool DictionaryValue::RemovePath(const std::string
& path
,
776 scoped_ptr
<Value
>* out_value
) {
778 size_t delimiter_position
= path
.find('.');
780 if (delimiter_position
== std::string::npos
)
781 return RemoveWithoutPathExpansion(path
, out_value
);
783 const std::string subdict_path
= path
.substr(0, delimiter_position
);
784 DictionaryValue
* subdict
= NULL
;
785 if (!GetDictionary(subdict_path
, &subdict
))
787 result
= subdict
->RemovePath(path
.substr(delimiter_position
+ 1),
789 if (result
&& subdict
->empty())
790 RemoveWithoutPathExpansion(subdict_path
, NULL
);
795 scoped_ptr
<DictionaryValue
> DictionaryValue::DeepCopyWithoutEmptyChildren()
797 scoped_ptr
<DictionaryValue
> copy
= CopyDictionaryWithoutEmptyChildren(*this);
799 copy
.reset(new DictionaryValue
);
803 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
804 for (DictionaryValue::Iterator
it(*dictionary
); !it
.IsAtEnd(); it
.Advance()) {
805 const Value
* merge_value
= &it
.value();
806 // Check whether we have to merge dictionaries.
807 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
808 DictionaryValue
* sub_dict
;
809 if (GetDictionaryWithoutPathExpansion(it
.key(), &sub_dict
)) {
810 sub_dict
->MergeDictionary(
811 static_cast<const DictionaryValue
*>(merge_value
));
815 // All other cases: Make a copy and hook it up.
816 SetWithoutPathExpansion(it
.key(), merge_value
->DeepCopy());
820 void DictionaryValue::Swap(DictionaryValue
* other
) {
821 dictionary_
.swap(other
->dictionary_
);
824 DictionaryValue::Iterator::Iterator(const DictionaryValue
& target
)
826 it_(target
.dictionary_
.begin()) {}
828 DictionaryValue::Iterator::~Iterator() {}
830 DictionaryValue
* DictionaryValue::DeepCopy() const {
831 DictionaryValue
* result
= new DictionaryValue
;
833 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
834 current_entry
!= dictionary_
.end(); ++current_entry
) {
835 result
->SetWithoutPathExpansion(current_entry
->first
,
836 current_entry
->second
->DeepCopy());
842 scoped_ptr
<DictionaryValue
> DictionaryValue::CreateDeepCopy() const {
843 return make_scoped_ptr(DeepCopy());
846 bool DictionaryValue::Equals(const Value
* other
) const {
847 if (other
->GetType() != GetType())
850 const DictionaryValue
* other_dict
=
851 static_cast<const DictionaryValue
*>(other
);
852 Iterator
lhs_it(*this);
853 Iterator
rhs_it(*other_dict
);
854 while (!lhs_it
.IsAtEnd() && !rhs_it
.IsAtEnd()) {
855 if (lhs_it
.key() != rhs_it
.key() ||
856 !lhs_it
.value().Equals(&rhs_it
.value())) {
862 if (!lhs_it
.IsAtEnd() || !rhs_it
.IsAtEnd())
868 ///////////////////// ListValue ////////////////////
870 ListValue::ListValue() : Value(TYPE_LIST
) {
873 ListValue::~ListValue() {
877 void ListValue::Clear() {
878 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
883 bool ListValue::Set(size_t index
, Value
* in_value
) {
887 if (index
>= list_
.size()) {
888 // Pad out any intermediate indexes with null settings
889 while (index
> list_
.size())
890 Append(CreateNullValue());
893 DCHECK(list_
[index
] != in_value
);
895 list_
[index
] = in_value
;
900 bool ListValue::Set(size_t index
, scoped_ptr
<Value
> in_value
) {
901 return Set(index
, in_value
.release());
904 bool ListValue::Get(size_t index
, const Value
** out_value
) const {
905 if (index
>= list_
.size())
909 *out_value
= list_
[index
];
914 bool ListValue::Get(size_t index
, Value
** out_value
) {
915 return static_cast<const ListValue
&>(*this).Get(
917 const_cast<const Value
**>(out_value
));
920 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
922 if (!Get(index
, &value
))
925 return value
->GetAsBoolean(bool_value
);
928 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
930 if (!Get(index
, &value
))
933 return value
->GetAsInteger(out_value
);
936 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
938 if (!Get(index
, &value
))
941 return value
->GetAsDouble(out_value
);
944 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
946 if (!Get(index
, &value
))
949 return value
->GetAsString(out_value
);
952 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
954 if (!Get(index
, &value
))
957 return value
->GetAsString(out_value
);
960 bool ListValue::GetBinary(size_t index
, const BinaryValue
** out_value
) const {
962 bool result
= Get(index
, &value
);
963 if (!result
|| !value
->IsType(TYPE_BINARY
))
967 *out_value
= static_cast<const BinaryValue
*>(value
);
972 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) {
973 return static_cast<const ListValue
&>(*this).GetBinary(
975 const_cast<const BinaryValue
**>(out_value
));
978 bool ListValue::GetDictionary(size_t index
,
979 const DictionaryValue
** out_value
) const {
981 bool result
= Get(index
, &value
);
982 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
986 *out_value
= static_cast<const DictionaryValue
*>(value
);
991 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) {
992 return static_cast<const ListValue
&>(*this).GetDictionary(
994 const_cast<const DictionaryValue
**>(out_value
));
997 bool ListValue::GetList(size_t index
, const ListValue
** out_value
) const {
999 bool result
= Get(index
, &value
);
1000 if (!result
|| !value
->IsType(TYPE_LIST
))
1004 *out_value
= static_cast<const ListValue
*>(value
);
1009 bool ListValue::GetList(size_t index
, ListValue
** out_value
) {
1010 return static_cast<const ListValue
&>(*this).GetList(
1012 const_cast<const ListValue
**>(out_value
));
1015 bool ListValue::Remove(size_t index
, scoped_ptr
<Value
>* out_value
) {
1016 if (index
>= list_
.size())
1020 out_value
->reset(list_
[index
]);
1022 delete list_
[index
];
1024 list_
.erase(list_
.begin() + index
);
1028 bool ListValue::Remove(const Value
& value
, size_t* index
) {
1029 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1030 if ((*i
)->Equals(&value
)) {
1031 size_t previous_index
= i
- list_
.begin();
1036 *index
= previous_index
;
1043 ListValue::iterator
ListValue::Erase(iterator iter
,
1044 scoped_ptr
<Value
>* out_value
) {
1046 out_value
->reset(*iter
);
1050 return list_
.erase(iter
);
1053 void ListValue::Append(scoped_ptr
<Value
> in_value
) {
1054 Append(in_value
.release());
1057 void ListValue::Append(Value
* in_value
) {
1059 list_
.push_back(in_value
);
1062 void ListValue::AppendBoolean(bool in_value
) {
1063 Append(new FundamentalValue(in_value
));
1066 void ListValue::AppendInteger(int in_value
) {
1067 Append(new FundamentalValue(in_value
));
1070 void ListValue::AppendDouble(double in_value
) {
1071 Append(new FundamentalValue(in_value
));
1074 void ListValue::AppendString(const std::string
& in_value
) {
1075 Append(new StringValue(in_value
));
1078 void ListValue::AppendString(const string16
& in_value
) {
1079 Append(new StringValue(in_value
));
1082 void ListValue::AppendStrings(const std::vector
<std::string
>& in_values
) {
1083 for (std::vector
<std::string
>::const_iterator it
= in_values
.begin();
1084 it
!= in_values
.end(); ++it
) {
1089 void ListValue::AppendStrings(const std::vector
<string16
>& in_values
) {
1090 for (std::vector
<string16
>::const_iterator it
= in_values
.begin();
1091 it
!= in_values
.end(); ++it
) {
1096 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
1098 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
1099 if ((*i
)->Equals(in_value
)) {
1104 list_
.push_back(in_value
);
1108 bool ListValue::Insert(size_t index
, Value
* in_value
) {
1110 if (index
> list_
.size())
1113 list_
.insert(list_
.begin() + index
, in_value
);
1117 ListValue::const_iterator
ListValue::Find(const Value
& value
) const {
1118 return std::find_if(list_
.begin(), list_
.end(), ValueEquals(&value
));
1121 void ListValue::Swap(ListValue
* other
) {
1122 list_
.swap(other
->list_
);
1125 bool ListValue::GetAsList(ListValue
** out_value
) {
1131 bool ListValue::GetAsList(const ListValue
** out_value
) const {
1137 ListValue
* ListValue::DeepCopy() const {
1138 ListValue
* result
= new ListValue
;
1140 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
1141 result
->Append((*i
)->DeepCopy());
1146 scoped_ptr
<ListValue
> ListValue::CreateDeepCopy() const {
1147 return make_scoped_ptr(DeepCopy());
1150 bool ListValue::Equals(const Value
* other
) const {
1151 if (other
->GetType() != GetType())
1154 const ListValue
* other_list
=
1155 static_cast<const ListValue
*>(other
);
1156 const_iterator lhs_it
, rhs_it
;
1157 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
1158 lhs_it
!= end() && rhs_it
!= other_list
->end();
1159 ++lhs_it
, ++rhs_it
) {
1160 if (!(*lhs_it
)->Equals(*rhs_it
))
1163 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
1169 ValueSerializer::~ValueSerializer() {
1172 ValueDeserializer::~ValueDeserializer() {
1175 std::ostream
& operator<<(std::ostream
& out
, const Value
& value
) {
1177 JSONWriter::WriteWithOptions(value
, JSONWriter::OPTIONS_PRETTY_PRINT
, &json
);