1 // Copyright (c) 2011 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"
7 #include "base/float_util.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
14 // Make a deep copy of |node|, but don't include empty lists or dictionaries
15 // in the copy. It's possible for this function to return NULL and it
16 // expects |node| to always be non-NULL.
17 Value
* CopyWithoutEmptyChildren(Value
* node
) {
19 switch (node
->GetType()) {
20 case Value::TYPE_LIST
: {
21 ListValue
* list
= static_cast<ListValue
*>(node
);
22 ListValue
* copy
= new ListValue
;
23 for (ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
25 Value
* child_copy
= CopyWithoutEmptyChildren(*it
);
27 copy
->Append(child_copy
);
36 case Value::TYPE_DICTIONARY
: {
37 DictionaryValue
* dict
= static_cast<DictionaryValue
*>(node
);
38 DictionaryValue
* copy
= new DictionaryValue
;
39 for (DictionaryValue::key_iterator it
= dict
->begin_keys();
40 it
!= dict
->end_keys(); ++it
) {
42 bool rv
= dict
->GetWithoutPathExpansion(*it
, &child
);
44 Value
* child_copy
= CopyWithoutEmptyChildren(child
);
46 copy
->SetWithoutPathExpansion(*it
, child_copy
);
56 // For everything else, just make a copy.
57 return node
->DeepCopy();
65 ///////////////////// Value ////////////////////
68 #if !defined(OS_CHROMEOS)
69 CHECK(!check_on_delete_
);
74 Value
* Value::CreateNullValue() {
75 return new Value(TYPE_NULL
);
79 FundamentalValue
* Value::CreateBooleanValue(bool in_value
) {
80 return new FundamentalValue(in_value
);
84 FundamentalValue
* Value::CreateIntegerValue(int in_value
) {
85 return new FundamentalValue(in_value
);
89 FundamentalValue
* Value::CreateDoubleValue(double in_value
) {
90 return new FundamentalValue(in_value
);
94 StringValue
* Value::CreateStringValue(const std::string
& in_value
) {
95 return new StringValue(in_value
);
99 StringValue
* Value::CreateStringValue(const string16
& in_value
) {
100 return new StringValue(in_value
);
103 bool Value::GetAsBoolean(bool* out_value
) const {
107 bool Value::GetAsInteger(int* out_value
) const {
111 bool Value::GetAsDouble(double* out_value
) const {
115 bool Value::GetAsString(std::string
* out_value
) const {
119 bool Value::GetAsString(string16
* out_value
) const {
123 bool Value::GetAsList(ListValue
** out_value
) {
127 bool Value::GetAsList(const ListValue
** out_value
) const {
131 Value
* Value::DeepCopy() const {
132 // This method should only be getting called for null Values--all subclasses
133 // need to provide their own implementation;.
134 DCHECK(IsType(TYPE_NULL
));
135 return CreateNullValue();
138 bool Value::Equals(const Value
* other
) const {
139 // This method should only be getting called for null Values--all subclasses
140 // need to provide their own implementation;.
141 DCHECK(IsType(TYPE_NULL
));
142 return other
->IsType(TYPE_NULL
);
146 bool Value::Equals(const Value
* a
, const Value
* b
) {
147 if ((a
== NULL
) && (b
== NULL
)) return true;
148 if ((a
== NULL
) ^ (b
== NULL
)) return false;
152 Value::Value(Type type
)
154 #if !defined(OS_CHROMEOS)
155 , check_on_delete_(false)
160 ///////////////////// FundamentalValue ////////////////////
162 FundamentalValue::FundamentalValue(bool in_value
)
163 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
166 FundamentalValue::FundamentalValue(int in_value
)
167 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
170 FundamentalValue::FundamentalValue(double in_value
)
171 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
172 if (!IsFinite(double_value_
)) {
173 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
174 << "values cannot be represented in JSON";
179 FundamentalValue::~FundamentalValue() {
182 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
183 if (out_value
&& IsType(TYPE_BOOLEAN
))
184 *out_value
= boolean_value_
;
185 return (IsType(TYPE_BOOLEAN
));
188 bool FundamentalValue::GetAsInteger(int* out_value
) const {
189 if (out_value
&& IsType(TYPE_INTEGER
))
190 *out_value
= integer_value_
;
191 return (IsType(TYPE_INTEGER
));
194 bool FundamentalValue::GetAsDouble(double* out_value
) const {
195 if (out_value
&& IsType(TYPE_DOUBLE
))
196 *out_value
= double_value_
;
197 else if (out_value
&& IsType(TYPE_INTEGER
))
198 *out_value
= integer_value_
;
199 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
202 FundamentalValue
* FundamentalValue::DeepCopy() const {
205 return CreateBooleanValue(boolean_value_
);
208 return CreateIntegerValue(integer_value_
);
211 return CreateDoubleValue(double_value_
);
219 bool FundamentalValue::Equals(const Value
* other
) const {
220 if (other
->GetType() != GetType())
226 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
230 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
234 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
242 ///////////////////// StringValue ////////////////////
244 StringValue::StringValue(const std::string
& in_value
)
245 : Value(TYPE_STRING
),
247 DCHECK(IsStringUTF8(in_value
));
250 StringValue::StringValue(const string16
& in_value
)
251 : Value(TYPE_STRING
),
252 value_(UTF16ToUTF8(in_value
)) {
255 StringValue::~StringValue() {
258 bool StringValue::GetAsString(std::string
* out_value
) const {
264 bool StringValue::GetAsString(string16
* out_value
) const {
266 *out_value
= UTF8ToUTF16(value_
);
270 StringValue
* StringValue::DeepCopy() const {
271 return CreateStringValue(value_
);
274 bool StringValue::Equals(const Value
* other
) const {
275 if (other
->GetType() != GetType())
277 std::string lhs
, rhs
;
278 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
281 ///////////////////// BinaryValue ////////////////////
283 BinaryValue::~BinaryValue() {
290 BinaryValue
* BinaryValue::Create(char* buffer
, size_t size
) {
294 return new BinaryValue(buffer
, size
);
298 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
303 char* buffer_copy
= new char[size
];
304 memcpy(buffer_copy
, buffer
, size
);
305 return new BinaryValue(buffer_copy
, size
);
308 BinaryValue
* BinaryValue::DeepCopy() const {
309 return CreateWithCopiedBuffer(buffer_
, size_
);
312 bool BinaryValue::Equals(const Value
* other
) const {
313 if (other
->GetType() != GetType())
315 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
316 if (other_binary
->size_
!= size_
)
318 return !memcmp(buffer_
, other_binary
->buffer_
, size_
);
321 BinaryValue::BinaryValue(char* buffer
, size_t size
)
322 : Value(TYPE_BINARY
),
328 ///////////////////// DictionaryValue ////////////////////
330 DictionaryValue::DictionaryValue()
331 : Value(TYPE_DICTIONARY
) {
334 DictionaryValue::~DictionaryValue() {
338 bool DictionaryValue::HasKey(const std::string
& key
) const {
339 DCHECK(IsStringUTF8(key
));
340 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
341 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
342 return current_entry
!= dictionary_
.end();
345 void DictionaryValue::Clear() {
346 ValueMap::iterator dict_iterator
= dictionary_
.begin();
347 while (dict_iterator
!= dictionary_
.end()) {
348 delete dict_iterator
->second
;
355 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
356 DCHECK(IsStringUTF8(path
));
359 std::string
current_path(path
);
360 DictionaryValue
* current_dictionary
= this;
361 for (size_t delimiter_position
= current_path
.find('.');
362 delimiter_position
!= std::string::npos
;
363 delimiter_position
= current_path
.find('.')) {
364 // Assume that we're indexing into a dictionary.
365 std::string
key(current_path
, 0, delimiter_position
);
366 DictionaryValue
* child_dictionary
= NULL
;
367 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
368 child_dictionary
= new DictionaryValue
;
369 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
372 current_dictionary
= child_dictionary
;
373 current_path
.erase(0, delimiter_position
+ 1);
376 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
379 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
380 Set(path
, CreateBooleanValue(in_value
));
383 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
384 Set(path
, CreateIntegerValue(in_value
));
387 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
388 Set(path
, CreateDoubleValue(in_value
));
391 void DictionaryValue::SetString(const std::string
& path
,
392 const std::string
& in_value
) {
393 Set(path
, CreateStringValue(in_value
));
396 void DictionaryValue::SetString(const std::string
& path
,
397 const string16
& in_value
) {
398 Set(path
, CreateStringValue(in_value
));
401 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
403 // If there's an existing value here, we need to delete it, because
404 // we own all our children.
406 DCHECK(dictionary_
[key
] != in_value
); // This would be bogus
407 delete dictionary_
[key
];
410 dictionary_
[key
] = in_value
;
413 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) const {
414 DCHECK(IsStringUTF8(path
));
415 std::string
current_path(path
);
416 const DictionaryValue
* current_dictionary
= this;
417 for (size_t delimiter_position
= current_path
.find('.');
418 delimiter_position
!= std::string::npos
;
419 delimiter_position
= current_path
.find('.')) {
420 DictionaryValue
* child_dictionary
= NULL
;
421 if (!current_dictionary
->GetDictionary(
422 current_path
.substr(0, delimiter_position
), &child_dictionary
))
425 current_dictionary
= child_dictionary
;
426 current_path
.erase(0, delimiter_position
+ 1);
429 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
432 bool DictionaryValue::GetBoolean(const std::string
& path
,
433 bool* bool_value
) const {
435 if (!Get(path
, &value
))
438 return value
->GetAsBoolean(bool_value
);
441 bool DictionaryValue::GetInteger(const std::string
& path
,
442 int* out_value
) const {
444 if (!Get(path
, &value
))
447 return value
->GetAsInteger(out_value
);
450 bool DictionaryValue::GetDouble(const std::string
& path
,
451 double* out_value
) const {
453 if (!Get(path
, &value
))
456 return value
->GetAsDouble(out_value
);
459 bool DictionaryValue::GetString(const std::string
& path
,
460 std::string
* out_value
) const {
462 if (!Get(path
, &value
))
465 return value
->GetAsString(out_value
);
468 bool DictionaryValue::GetString(const std::string
& path
,
469 string16
* out_value
) const {
471 if (!Get(path
, &value
))
474 return value
->GetAsString(out_value
);
477 bool DictionaryValue::GetStringASCII(const std::string
& path
,
478 std::string
* out_value
) const {
480 if (!GetString(path
, &out
))
483 if (!IsStringASCII(out
)) {
488 out_value
->assign(out
);
492 bool DictionaryValue::GetBinary(const std::string
& path
,
493 BinaryValue
** out_value
) const {
495 bool result
= Get(path
, &value
);
496 if (!result
|| !value
->IsType(TYPE_BINARY
))
500 *out_value
= static_cast<BinaryValue
*>(value
);
505 bool DictionaryValue::GetDictionary(const std::string
& path
,
506 DictionaryValue
** out_value
) const {
508 bool result
= Get(path
, &value
);
509 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
513 *out_value
= static_cast<DictionaryValue
*>(value
);
518 bool DictionaryValue::GetList(const std::string
& path
,
519 ListValue
** out_value
) const {
521 bool result
= Get(path
, &value
);
522 if (!result
|| !value
->IsType(TYPE_LIST
))
526 *out_value
= static_cast<ListValue
*>(value
);
531 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
532 Value
** out_value
) const {
533 DCHECK(IsStringUTF8(key
));
534 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
535 if (entry_iterator
== dictionary_
.end())
538 Value
* entry
= entry_iterator
->second
;
544 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
545 int* out_value
) const {
547 if (!GetWithoutPathExpansion(key
, &value
))
550 return value
->GetAsInteger(out_value
);
553 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
554 double* out_value
) const {
556 if (!GetWithoutPathExpansion(key
, &value
))
559 return value
->GetAsDouble(out_value
);
562 bool DictionaryValue::GetStringWithoutPathExpansion(
563 const std::string
& key
,
564 std::string
* out_value
) const {
566 if (!GetWithoutPathExpansion(key
, &value
))
569 return value
->GetAsString(out_value
);
572 bool DictionaryValue::GetStringWithoutPathExpansion(
573 const std::string
& key
,
574 string16
* out_value
) const {
576 if (!GetWithoutPathExpansion(key
, &value
))
579 return value
->GetAsString(out_value
);
582 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
583 const std::string
& key
,
584 DictionaryValue
** out_value
) const {
586 bool result
= GetWithoutPathExpansion(key
, &value
);
587 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
591 *out_value
= static_cast<DictionaryValue
*>(value
);
596 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
597 ListValue
** out_value
) const {
599 bool result
= GetWithoutPathExpansion(key
, &value
);
600 if (!result
|| !value
->IsType(TYPE_LIST
))
604 *out_value
= static_cast<ListValue
*>(value
);
609 bool DictionaryValue::Remove(const std::string
& path
, Value
** out_value
) {
610 DCHECK(IsStringUTF8(path
));
611 std::string
current_path(path
);
612 DictionaryValue
* current_dictionary
= this;
613 size_t delimiter_position
= current_path
.rfind('.');
614 if (delimiter_position
!= std::string::npos
) {
615 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
616 ¤t_dictionary
))
618 current_path
.erase(0, delimiter_position
+ 1);
621 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
625 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
627 DCHECK(IsStringUTF8(key
));
628 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
629 if (entry_iterator
== dictionary_
.end())
632 Value
* entry
= entry_iterator
->second
;
637 dictionary_
.erase(entry_iterator
);
641 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() {
642 Value
* copy
= CopyWithoutEmptyChildren(this);
643 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
646 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
647 for (DictionaryValue::key_iterator
key(dictionary
->begin_keys());
648 key
!= dictionary
->end_keys(); ++key
) {
650 if (dictionary
->GetWithoutPathExpansion(*key
, &merge_value
)) {
651 // Check whether we have to merge dictionaries.
652 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
653 DictionaryValue
* sub_dict
;
654 if (GetDictionaryWithoutPathExpansion(*key
, &sub_dict
)) {
655 sub_dict
->MergeDictionary(
656 static_cast<const DictionaryValue
*>(merge_value
));
660 // All other cases: Make a copy and hook it up.
661 SetWithoutPathExpansion(*key
, merge_value
->DeepCopy());
666 DictionaryValue
* DictionaryValue::DeepCopy() const {
667 DictionaryValue
* result
= new DictionaryValue
;
669 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
670 current_entry
!= dictionary_
.end(); ++current_entry
) {
671 result
->SetWithoutPathExpansion(current_entry
->first
,
672 current_entry
->second
->DeepCopy());
678 bool DictionaryValue::Equals(const Value
* other
) const {
679 if (other
->GetType() != GetType())
682 const DictionaryValue
* other_dict
=
683 static_cast<const DictionaryValue
*>(other
);
684 key_iterator
lhs_it(begin_keys());
685 key_iterator
rhs_it(other_dict
->begin_keys());
686 while (lhs_it
!= end_keys() && rhs_it
!= other_dict
->end_keys()) {
689 if (*lhs_it
!= *rhs_it
||
690 !GetWithoutPathExpansion(*lhs_it
, &lhs
) ||
691 !other_dict
->GetWithoutPathExpansion(*rhs_it
, &rhs
) ||
698 if (lhs_it
!= end_keys() || rhs_it
!= other_dict
->end_keys())
704 ///////////////////// ListValue ////////////////////
706 ListValue::ListValue() : Value(TYPE_LIST
) {
709 ListValue::~ListValue() {
713 void ListValue::Clear() {
714 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
719 bool ListValue::Set(size_t index
, Value
* in_value
) {
723 if (index
>= list_
.size()) {
724 // Pad out any intermediate indexes with null settings
725 while (index
> list_
.size())
726 Append(CreateNullValue());
729 DCHECK(list_
[index
] != in_value
);
731 list_
[index
] = in_value
;
736 bool ListValue::Get(size_t index
, Value
** out_value
) const {
737 if (index
>= list_
.size())
741 *out_value
= list_
[index
];
746 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
748 if (!Get(index
, &value
))
751 return value
->GetAsBoolean(bool_value
);
754 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
756 if (!Get(index
, &value
))
759 return value
->GetAsInteger(out_value
);
762 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
764 if (!Get(index
, &value
))
767 return value
->GetAsDouble(out_value
);
770 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
772 if (!Get(index
, &value
))
775 return value
->GetAsString(out_value
);
778 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
780 if (!Get(index
, &value
))
783 return value
->GetAsString(out_value
);
786 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) const {
788 bool result
= Get(index
, &value
);
789 if (!result
|| !value
->IsType(TYPE_BINARY
))
793 *out_value
= static_cast<BinaryValue
*>(value
);
798 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) const {
800 bool result
= Get(index
, &value
);
801 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
805 *out_value
= static_cast<DictionaryValue
*>(value
);
810 bool ListValue::GetList(size_t index
, ListValue
** out_value
) const {
812 bool result
= Get(index
, &value
);
813 if (!result
|| !value
->IsType(TYPE_LIST
))
817 *out_value
= static_cast<ListValue
*>(value
);
822 bool ListValue::Remove(size_t index
, Value
** out_value
) {
823 if (index
>= list_
.size())
827 *out_value
= list_
[index
];
831 list_
.erase(list_
.begin() + index
);
835 bool ListValue::Remove(const Value
& value
, size_t* index
) {
836 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
837 if ((*i
)->Equals(&value
)) {
838 size_t previous_index
= i
- list_
.begin();
843 *index
= previous_index
;
850 void ListValue::Append(Value
* in_value
) {
852 list_
.push_back(in_value
);
855 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
857 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
858 if ((*i
)->Equals(in_value
)) {
863 list_
.push_back(in_value
);
867 bool ListValue::Insert(size_t index
, Value
* in_value
) {
869 if (index
> list_
.size())
872 list_
.insert(list_
.begin() + index
, in_value
);
876 bool ListValue::GetAsList(ListValue
** out_value
) {
882 bool ListValue::GetAsList(const ListValue
** out_value
) const {
888 ListValue
* ListValue::DeepCopy() const {
889 ListValue
* result
= new ListValue
;
891 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
892 result
->Append((*i
)->DeepCopy());
897 bool ListValue::Equals(const Value
* other
) const {
898 if (other
->GetType() != GetType())
901 const ListValue
* other_list
=
902 static_cast<const ListValue
*>(other
);
903 const_iterator lhs_it
, rhs_it
;
904 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
905 lhs_it
!= end() && rhs_it
!= other_list
->end();
906 ++lhs_it
, ++rhs_it
) {
907 if (!(*lhs_it
)->Equals(*rhs_it
))
910 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
916 ValueSerializer::~ValueSerializer() {