blob: 52876cf2c4fffff5b932d6c427418fedd75046a2 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
5#include "base/values.h"
6
7#include <string.h>
8
9#include <algorithm>
10#include <ostream>
11
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"
18
19namespace base {
20
21namespace {
22
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.
26Value* CopyWithoutEmptyChildren(const Value* node) {
27 DCHECK(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();
33 ++it) {
34 Value* child_copy = CopyWithoutEmptyChildren(*it);
35 if (child_copy)
36 copy->Append(child_copy);
37 }
38 if (!copy->empty())
39 return copy;
40
41 delete copy;
42 return NULL;
43 }
44
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());
50 if (child_copy)
51 copy->SetWithoutPathExpansion(it.key(), child_copy);
52 }
53 if (!copy->empty())
54 return copy;
55
56 delete copy;
57 return NULL;
58 }
59
60 default:
61 // For everything else, just make a copy.
62 return node->DeepCopy();
63 }
64}
65
66// A small functor for comparing Values for std::find_if and similar.
67class ValueEquals {
68 public:
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) { }
73
74 bool operator ()(const Value* second) const {
75 return first_->Equals(second);
76 }
77
78 private:
79 const Value* first_;
80};
81
82} // namespace
83
84Value::~Value() {
85}
86
87// static
88Value* Value::CreateNullValue() {
89 return new Value(TYPE_NULL);
90}
91
James Robinson5e66a792015-01-21 17:02:08 -080092bool Value::GetAsBinary(const BinaryValue** out_value) const {
93 return false;
94}
95
James Robinson646469d2014-10-03 15:33:28 -070096bool Value::GetAsBoolean(bool* out_value) const {
97 return false;
98}
99
100bool Value::GetAsInteger(int* out_value) const {
101 return false;
102}
103
104bool Value::GetAsDouble(double* out_value) const {
105 return false;
106}
107
108bool Value::GetAsString(std::string* out_value) const {
109 return false;
110}
111
112bool Value::GetAsString(string16* out_value) const {
113 return false;
114}
115
116bool Value::GetAsString(const StringValue** out_value) const {
117 return false;
118}
119
120bool Value::GetAsList(ListValue** out_value) {
121 return false;
122}
123
124bool Value::GetAsList(const ListValue** out_value) const {
125 return false;
126}
127
128bool Value::GetAsDictionary(DictionaryValue** out_value) {
129 return false;
130}
131
132bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
133 return false;
134}
135
136Value* 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();
141}
142
143bool Value::Equals(const Value* other) const {
144 // This method should only be getting called for null Values--all subclasses
145 // need to provide their own implementation;.
146 DCHECK(IsType(TYPE_NULL));
147 return other->IsType(TYPE_NULL);
148}
149
150// static
151bool Value::Equals(const Value* a, const Value* b) {
152 if ((a == NULL) && (b == NULL)) return true;
153 if ((a == NULL) ^ (b == NULL)) return false;
154 return a->Equals(b);
155}
156
157Value::Value(Type type) : type_(type) {}
158
159Value::Value(const Value& that) : type_(that.type_) {}
160
161Value& Value::operator=(const Value& that) {
162 type_ = that.type_;
163 return *this;
164}
165
166///////////////////// FundamentalValue ////////////////////
167
168FundamentalValue::FundamentalValue(bool in_value)
169 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
170}
171
172FundamentalValue::FundamentalValue(int in_value)
173 : Value(TYPE_INTEGER), integer_value_(in_value) {
174}
175
176FundamentalValue::FundamentalValue(double in_value)
177 : Value(TYPE_DOUBLE), double_value_(in_value) {
178 if (!IsFinite(double_value_)) {
179 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
180 << "values cannot be represented in JSON";
181 double_value_ = 0.0;
182 }
183}
184
185FundamentalValue::~FundamentalValue() {
186}
187
188bool FundamentalValue::GetAsBoolean(bool* out_value) const {
189 if (out_value && IsType(TYPE_BOOLEAN))
190 *out_value = boolean_value_;
191 return (IsType(TYPE_BOOLEAN));
192}
193
194bool FundamentalValue::GetAsInteger(int* out_value) const {
195 if (out_value && IsType(TYPE_INTEGER))
196 *out_value = integer_value_;
197 return (IsType(TYPE_INTEGER));
198}
199
200bool FundamentalValue::GetAsDouble(double* out_value) const {
201 if (out_value && IsType(TYPE_DOUBLE))
202 *out_value = double_value_;
203 else if (out_value && IsType(TYPE_INTEGER))
204 *out_value = integer_value_;
205 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
206}
207
208FundamentalValue* FundamentalValue::DeepCopy() const {
209 switch (GetType()) {
210 case TYPE_BOOLEAN:
211 return new FundamentalValue(boolean_value_);
212
213 case TYPE_INTEGER:
214 return new FundamentalValue(integer_value_);
215
216 case TYPE_DOUBLE:
217 return new FundamentalValue(double_value_);
218
219 default:
220 NOTREACHED();
221 return NULL;
222 }
223}
224
225bool FundamentalValue::Equals(const Value* other) const {
226 if (other->GetType() != GetType())
227 return false;
228
229 switch (GetType()) {
230 case TYPE_BOOLEAN: {
231 bool lhs, rhs;
232 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
233 }
234 case TYPE_INTEGER: {
235 int lhs, rhs;
236 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
237 }
238 case TYPE_DOUBLE: {
239 double lhs, rhs;
240 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
241 }
242 default:
243 NOTREACHED();
244 return false;
245 }
246}
247
248///////////////////// StringValue ////////////////////
249
250StringValue::StringValue(const std::string& in_value)
251 : Value(TYPE_STRING),
252 value_(in_value) {
253 DCHECK(IsStringUTF8(in_value));
254}
255
256StringValue::StringValue(const string16& in_value)
257 : Value(TYPE_STRING),
258 value_(UTF16ToUTF8(in_value)) {
259}
260
261StringValue::~StringValue() {
262}
263
264std::string* StringValue::GetString() {
265 return &value_;
266}
267
268const std::string& StringValue::GetString() const {
269 return value_;
270}
271
272bool StringValue::GetAsString(std::string* out_value) const {
273 if (out_value)
274 *out_value = value_;
275 return true;
276}
277
278bool StringValue::GetAsString(string16* out_value) const {
279 if (out_value)
280 *out_value = UTF8ToUTF16(value_);
281 return true;
282}
283
284bool StringValue::GetAsString(const StringValue** out_value) const {
285 if (out_value)
286 *out_value = this;
287 return true;
288}
289
290StringValue* StringValue::DeepCopy() const {
291 return new StringValue(value_);
292}
293
294bool StringValue::Equals(const Value* other) const {
295 if (other->GetType() != GetType())
296 return false;
297 std::string lhs, rhs;
298 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
299}
300
301///////////////////// BinaryValue ////////////////////
302
303BinaryValue::BinaryValue()
304 : Value(TYPE_BINARY),
305 size_(0) {
306}
307
308BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
309 : Value(TYPE_BINARY),
310 buffer_(buffer.Pass()),
311 size_(size) {
312}
313
314BinaryValue::~BinaryValue() {
315}
316
317// static
318BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
319 size_t size) {
320 char* buffer_copy = new char[size];
321 memcpy(buffer_copy, buffer, size);
322 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
323 return new BinaryValue(scoped_buffer_copy.Pass(), size);
324}
325
James Robinson5e66a792015-01-21 17:02:08 -0800326bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
327 if (out_value)
328 *out_value = this;
329 return true;
330}
331
James Robinson646469d2014-10-03 15:33:28 -0700332BinaryValue* BinaryValue::DeepCopy() const {
333 return CreateWithCopiedBuffer(buffer_.get(), size_);
334}
335
336bool BinaryValue::Equals(const Value* other) const {
337 if (other->GetType() != GetType())
338 return false;
339 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
340 if (other_binary->size_ != size_)
341 return false;
342 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
343}
344
345///////////////////// DictionaryValue ////////////////////
346
347DictionaryValue::DictionaryValue()
348 : Value(TYPE_DICTIONARY) {
349}
350
351DictionaryValue::~DictionaryValue() {
352 Clear();
353}
354
355bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
356 if (out_value)
357 *out_value = this;
358 return true;
359}
360
361bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
362 if (out_value)
363 *out_value = this;
364 return true;
365}
366
367bool DictionaryValue::HasKey(const std::string& key) const {
368 DCHECK(IsStringUTF8(key));
369 ValueMap::const_iterator current_entry = dictionary_.find(key);
370 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
371 return current_entry != dictionary_.end();
372}
373
374void DictionaryValue::Clear() {
375 ValueMap::iterator dict_iterator = dictionary_.begin();
376 while (dict_iterator != dictionary_.end()) {
377 delete dict_iterator->second;
378 ++dict_iterator;
379 }
380
381 dictionary_.clear();
382}
383
Nick Bray27a3f6e2015-01-08 16:39:35 -0800384void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
James Robinson646469d2014-10-03 15:33:28 -0700385 DCHECK(IsStringUTF8(path));
386 DCHECK(in_value);
387
388 std::string current_path(path);
389 DictionaryValue* current_dictionary = this;
390 for (size_t delimiter_position = current_path.find('.');
391 delimiter_position != std::string::npos;
392 delimiter_position = current_path.find('.')) {
393 // Assume that we're indexing into a dictionary.
394 std::string key(current_path, 0, delimiter_position);
395 DictionaryValue* child_dictionary = NULL;
396 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
397 child_dictionary = new DictionaryValue;
398 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
399 }
400
401 current_dictionary = child_dictionary;
402 current_path.erase(0, delimiter_position + 1);
403 }
404
Nick Bray27a3f6e2015-01-08 16:39:35 -0800405 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
406}
407
408void DictionaryValue::Set(const std::string& path, Value* in_value) {
409 Set(path, make_scoped_ptr(in_value));
James Robinson646469d2014-10-03 15:33:28 -0700410}
411
412void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
413 Set(path, new FundamentalValue(in_value));
414}
415
416void DictionaryValue::SetInteger(const std::string& path, int in_value) {
417 Set(path, new FundamentalValue(in_value));
418}
419
420void DictionaryValue::SetDouble(const std::string& path, double in_value) {
421 Set(path, new FundamentalValue(in_value));
422}
423
424void DictionaryValue::SetString(const std::string& path,
425 const std::string& in_value) {
426 Set(path, new StringValue(in_value));
427}
428
429void DictionaryValue::SetString(const std::string& path,
430 const string16& in_value) {
431 Set(path, new StringValue(in_value));
432}
433
434void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
Nick Bray27a3f6e2015-01-08 16:39:35 -0800435 scoped_ptr<Value> in_value) {
436 Value* bare_ptr = in_value.release();
James Robinson646469d2014-10-03 15:33:28 -0700437 // If there's an existing value here, we need to delete it, because
438 // we own all our children.
439 std::pair<ValueMap::iterator, bool> ins_res =
Nick Bray27a3f6e2015-01-08 16:39:35 -0800440 dictionary_.insert(std::make_pair(key, bare_ptr));
James Robinson646469d2014-10-03 15:33:28 -0700441 if (!ins_res.second) {
Nick Bray27a3f6e2015-01-08 16:39:35 -0800442 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
James Robinson646469d2014-10-03 15:33:28 -0700443 delete ins_res.first->second;
Nick Bray27a3f6e2015-01-08 16:39:35 -0800444 ins_res.first->second = bare_ptr;
James Robinson646469d2014-10-03 15:33:28 -0700445 }
446}
447
Nick Bray27a3f6e2015-01-08 16:39:35 -0800448void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
449 Value* in_value) {
450 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
451}
452
James Robinson646469d2014-10-03 15:33:28 -0700453void DictionaryValue::SetBooleanWithoutPathExpansion(
454 const std::string& path, bool in_value) {
455 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
456}
457
458void DictionaryValue::SetIntegerWithoutPathExpansion(
459 const std::string& path, int in_value) {
460 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
461}
462
463void DictionaryValue::SetDoubleWithoutPathExpansion(
464 const std::string& path, double in_value) {
465 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
466}
467
468void DictionaryValue::SetStringWithoutPathExpansion(
469 const std::string& path, const std::string& in_value) {
470 SetWithoutPathExpansion(path, new StringValue(in_value));
471}
472
473void DictionaryValue::SetStringWithoutPathExpansion(
474 const std::string& path, const string16& in_value) {
475 SetWithoutPathExpansion(path, new StringValue(in_value));
476}
477
478bool DictionaryValue::Get(const std::string& path,
479 const Value** out_value) const {
480 DCHECK(IsStringUTF8(path));
481 std::string current_path(path);
482 const DictionaryValue* current_dictionary = this;
483 for (size_t delimiter_position = current_path.find('.');
484 delimiter_position != std::string::npos;
485 delimiter_position = current_path.find('.')) {
486 const DictionaryValue* child_dictionary = NULL;
487 if (!current_dictionary->GetDictionary(
488 current_path.substr(0, delimiter_position), &child_dictionary))
489 return false;
490
491 current_dictionary = child_dictionary;
492 current_path.erase(0, delimiter_position + 1);
493 }
494
495 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
496}
497
498bool DictionaryValue::Get(const std::string& path, Value** out_value) {
499 return static_cast<const DictionaryValue&>(*this).Get(
500 path,
501 const_cast<const Value**>(out_value));
502}
503
504bool DictionaryValue::GetBoolean(const std::string& path,
505 bool* bool_value) const {
506 const Value* value;
507 if (!Get(path, &value))
508 return false;
509
510 return value->GetAsBoolean(bool_value);
511}
512
513bool DictionaryValue::GetInteger(const std::string& path,
514 int* out_value) const {
515 const Value* value;
516 if (!Get(path, &value))
517 return false;
518
519 return value->GetAsInteger(out_value);
520}
521
522bool DictionaryValue::GetDouble(const std::string& path,
523 double* out_value) const {
524 const Value* value;
525 if (!Get(path, &value))
526 return false;
527
528 return value->GetAsDouble(out_value);
529}
530
531bool DictionaryValue::GetString(const std::string& path,
532 std::string* out_value) const {
533 const Value* value;
534 if (!Get(path, &value))
535 return false;
536
537 return value->GetAsString(out_value);
538}
539
540bool DictionaryValue::GetString(const std::string& path,
541 string16* out_value) const {
542 const Value* value;
543 if (!Get(path, &value))
544 return false;
545
546 return value->GetAsString(out_value);
547}
548
549bool DictionaryValue::GetStringASCII(const std::string& path,
550 std::string* out_value) const {
551 std::string out;
552 if (!GetString(path, &out))
553 return false;
554
555 if (!IsStringASCII(out)) {
556 NOTREACHED();
557 return false;
558 }
559
560 out_value->assign(out);
561 return true;
562}
563
564bool DictionaryValue::GetBinary(const std::string& path,
565 const BinaryValue** out_value) const {
566 const Value* value;
567 bool result = Get(path, &value);
568 if (!result || !value->IsType(TYPE_BINARY))
569 return false;
570
571 if (out_value)
572 *out_value = static_cast<const BinaryValue*>(value);
573
574 return true;
575}
576
577bool DictionaryValue::GetBinary(const std::string& path,
578 BinaryValue** out_value) {
579 return static_cast<const DictionaryValue&>(*this).GetBinary(
580 path,
581 const_cast<const BinaryValue**>(out_value));
582}
583
584bool DictionaryValue::GetDictionary(const std::string& path,
585 const DictionaryValue** out_value) const {
586 const Value* value;
587 bool result = Get(path, &value);
588 if (!result || !value->IsType(TYPE_DICTIONARY))
589 return false;
590
591 if (out_value)
592 *out_value = static_cast<const DictionaryValue*>(value);
593
594 return true;
595}
596
597bool DictionaryValue::GetDictionary(const std::string& path,
598 DictionaryValue** out_value) {
599 return static_cast<const DictionaryValue&>(*this).GetDictionary(
600 path,
601 const_cast<const DictionaryValue**>(out_value));
602}
603
604bool DictionaryValue::GetList(const std::string& path,
605 const ListValue** out_value) const {
606 const Value* value;
607 bool result = Get(path, &value);
608 if (!result || !value->IsType(TYPE_LIST))
609 return false;
610
611 if (out_value)
612 *out_value = static_cast<const ListValue*>(value);
613
614 return true;
615}
616
617bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
618 return static_cast<const DictionaryValue&>(*this).GetList(
619 path,
620 const_cast<const ListValue**>(out_value));
621}
622
623bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
624 const Value** out_value) const {
625 DCHECK(IsStringUTF8(key));
626 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
627 if (entry_iterator == dictionary_.end())
628 return false;
629
630 const Value* entry = entry_iterator->second;
631 if (out_value)
632 *out_value = entry;
633 return true;
634}
635
636bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
637 Value** out_value) {
638 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
639 key,
640 const_cast<const Value**>(out_value));
641}
642
643bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
644 bool* out_value) const {
645 const Value* value;
646 if (!GetWithoutPathExpansion(key, &value))
647 return false;
648
649 return value->GetAsBoolean(out_value);
650}
651
652bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
653 int* out_value) const {
654 const Value* value;
655 if (!GetWithoutPathExpansion(key, &value))
656 return false;
657
658 return value->GetAsInteger(out_value);
659}
660
661bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
662 double* out_value) const {
663 const Value* value;
664 if (!GetWithoutPathExpansion(key, &value))
665 return false;
666
667 return value->GetAsDouble(out_value);
668}
669
670bool DictionaryValue::GetStringWithoutPathExpansion(
671 const std::string& key,
672 std::string* out_value) const {
673 const Value* value;
674 if (!GetWithoutPathExpansion(key, &value))
675 return false;
676
677 return value->GetAsString(out_value);
678}
679
680bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
681 string16* out_value) const {
682 const Value* value;
683 if (!GetWithoutPathExpansion(key, &value))
684 return false;
685
686 return value->GetAsString(out_value);
687}
688
689bool DictionaryValue::GetDictionaryWithoutPathExpansion(
690 const std::string& key,
691 const DictionaryValue** out_value) const {
692 const Value* value;
693 bool result = GetWithoutPathExpansion(key, &value);
694 if (!result || !value->IsType(TYPE_DICTIONARY))
695 return false;
696
697 if (out_value)
698 *out_value = static_cast<const DictionaryValue*>(value);
699
700 return true;
701}
702
703bool DictionaryValue::GetDictionaryWithoutPathExpansion(
704 const std::string& key,
705 DictionaryValue** out_value) {
706 const DictionaryValue& const_this =
707 static_cast<const DictionaryValue&>(*this);
708 return const_this.GetDictionaryWithoutPathExpansion(
709 key,
710 const_cast<const DictionaryValue**>(out_value));
711}
712
713bool DictionaryValue::GetListWithoutPathExpansion(
714 const std::string& key,
715 const ListValue** out_value) const {
716 const Value* value;
717 bool result = GetWithoutPathExpansion(key, &value);
718 if (!result || !value->IsType(TYPE_LIST))
719 return false;
720
721 if (out_value)
722 *out_value = static_cast<const ListValue*>(value);
723
724 return true;
725}
726
727bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
728 ListValue** out_value) {
729 return
730 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
731 key,
732 const_cast<const ListValue**>(out_value));
733}
734
735bool DictionaryValue::Remove(const std::string& path,
736 scoped_ptr<Value>* out_value) {
737 DCHECK(IsStringUTF8(path));
738 std::string current_path(path);
739 DictionaryValue* current_dictionary = this;
740 size_t delimiter_position = current_path.rfind('.');
741 if (delimiter_position != std::string::npos) {
742 if (!GetDictionary(current_path.substr(0, delimiter_position),
743 &current_dictionary))
744 return false;
745 current_path.erase(0, delimiter_position + 1);
746 }
747
748 return current_dictionary->RemoveWithoutPathExpansion(current_path,
749 out_value);
750}
751
752bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
753 scoped_ptr<Value>* out_value) {
754 DCHECK(IsStringUTF8(key));
755 ValueMap::iterator entry_iterator = dictionary_.find(key);
756 if (entry_iterator == dictionary_.end())
757 return false;
758
759 Value* entry = entry_iterator->second;
760 if (out_value)
761 out_value->reset(entry);
762 else
763 delete entry;
764 dictionary_.erase(entry_iterator);
765 return true;
766}
767
768bool DictionaryValue::RemovePath(const std::string& path,
769 scoped_ptr<Value>* out_value) {
770 bool result = false;
771 size_t delimiter_position = path.find('.');
772
773 if (delimiter_position == std::string::npos)
774 return RemoveWithoutPathExpansion(path, out_value);
775
776 const std::string subdict_path = path.substr(0, delimiter_position);
777 DictionaryValue* subdict = NULL;
778 if (!GetDictionary(subdict_path, &subdict))
779 return false;
780 result = subdict->RemovePath(path.substr(delimiter_position + 1),
781 out_value);
782 if (result && subdict->empty())
783 RemoveWithoutPathExpansion(subdict_path, NULL);
784
785 return result;
786}
787
788DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
789 Value* copy = CopyWithoutEmptyChildren(this);
790 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
791}
792
793void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
794 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
795 const Value* merge_value = &it.value();
796 // Check whether we have to merge dictionaries.
797 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
798 DictionaryValue* sub_dict;
799 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
800 sub_dict->MergeDictionary(
801 static_cast<const DictionaryValue*>(merge_value));
802 continue;
803 }
804 }
805 // All other cases: Make a copy and hook it up.
806 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
807 }
808}
809
810void DictionaryValue::Swap(DictionaryValue* other) {
811 dictionary_.swap(other->dictionary_);
812}
813
814DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
815 : target_(target),
816 it_(target.dictionary_.begin()) {}
817
818DictionaryValue::Iterator::~Iterator() {}
819
820DictionaryValue* DictionaryValue::DeepCopy() const {
821 DictionaryValue* result = new DictionaryValue;
822
823 for (ValueMap::const_iterator current_entry(dictionary_.begin());
824 current_entry != dictionary_.end(); ++current_entry) {
825 result->SetWithoutPathExpansion(current_entry->first,
826 current_entry->second->DeepCopy());
827 }
828
829 return result;
830}
831
832bool DictionaryValue::Equals(const Value* other) const {
833 if (other->GetType() != GetType())
834 return false;
835
836 const DictionaryValue* other_dict =
837 static_cast<const DictionaryValue*>(other);
838 Iterator lhs_it(*this);
839 Iterator rhs_it(*other_dict);
840 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
841 if (lhs_it.key() != rhs_it.key() ||
842 !lhs_it.value().Equals(&rhs_it.value())) {
843 return false;
844 }
845 lhs_it.Advance();
846 rhs_it.Advance();
847 }
848 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
849 return false;
850
851 return true;
852}
853
854///////////////////// ListValue ////////////////////
855
856ListValue::ListValue() : Value(TYPE_LIST) {
857}
858
859ListValue::~ListValue() {
860 Clear();
861}
862
863void ListValue::Clear() {
864 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
865 delete *i;
866 list_.clear();
867}
868
869bool ListValue::Set(size_t index, Value* in_value) {
870 if (!in_value)
871 return false;
872
873 if (index >= list_.size()) {
874 // Pad out any intermediate indexes with null settings
875 while (index > list_.size())
876 Append(CreateNullValue());
877 Append(in_value);
878 } else {
879 DCHECK(list_[index] != in_value);
880 delete list_[index];
881 list_[index] = in_value;
882 }
883 return true;
884}
885
886bool ListValue::Get(size_t index, const Value** out_value) const {
887 if (index >= list_.size())
888 return false;
889
890 if (out_value)
891 *out_value = list_[index];
892
893 return true;
894}
895
896bool ListValue::Get(size_t index, Value** out_value) {
897 return static_cast<const ListValue&>(*this).Get(
898 index,
899 const_cast<const Value**>(out_value));
900}
901
902bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
903 const Value* value;
904 if (!Get(index, &value))
905 return false;
906
907 return value->GetAsBoolean(bool_value);
908}
909
910bool ListValue::GetInteger(size_t index, int* out_value) const {
911 const Value* value;
912 if (!Get(index, &value))
913 return false;
914
915 return value->GetAsInteger(out_value);
916}
917
918bool ListValue::GetDouble(size_t index, double* out_value) const {
919 const Value* value;
920 if (!Get(index, &value))
921 return false;
922
923 return value->GetAsDouble(out_value);
924}
925
926bool ListValue::GetString(size_t index, std::string* out_value) const {
927 const Value* value;
928 if (!Get(index, &value))
929 return false;
930
931 return value->GetAsString(out_value);
932}
933
934bool ListValue::GetString(size_t index, string16* out_value) const {
935 const Value* value;
936 if (!Get(index, &value))
937 return false;
938
939 return value->GetAsString(out_value);
940}
941
942bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
943 const Value* value;
944 bool result = Get(index, &value);
945 if (!result || !value->IsType(TYPE_BINARY))
946 return false;
947
948 if (out_value)
949 *out_value = static_cast<const BinaryValue*>(value);
950
951 return true;
952}
953
954bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
955 return static_cast<const ListValue&>(*this).GetBinary(
956 index,
957 const_cast<const BinaryValue**>(out_value));
958}
959
960bool ListValue::GetDictionary(size_t index,
961 const DictionaryValue** out_value) const {
962 const Value* value;
963 bool result = Get(index, &value);
964 if (!result || !value->IsType(TYPE_DICTIONARY))
965 return false;
966
967 if (out_value)
968 *out_value = static_cast<const DictionaryValue*>(value);
969
970 return true;
971}
972
973bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
974 return static_cast<const ListValue&>(*this).GetDictionary(
975 index,
976 const_cast<const DictionaryValue**>(out_value));
977}
978
979bool ListValue::GetList(size_t index, const ListValue** out_value) const {
980 const Value* value;
981 bool result = Get(index, &value);
982 if (!result || !value->IsType(TYPE_LIST))
983 return false;
984
985 if (out_value)
986 *out_value = static_cast<const ListValue*>(value);
987
988 return true;
989}
990
991bool ListValue::GetList(size_t index, ListValue** out_value) {
992 return static_cast<const ListValue&>(*this).GetList(
993 index,
994 const_cast<const ListValue**>(out_value));
995}
996
997bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
998 if (index >= list_.size())
999 return false;
1000
1001 if (out_value)
1002 out_value->reset(list_[index]);
1003 else
1004 delete list_[index];
1005
1006 list_.erase(list_.begin() + index);
1007 return true;
1008}
1009
1010bool ListValue::Remove(const Value& value, size_t* index) {
1011 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1012 if ((*i)->Equals(&value)) {
1013 size_t previous_index = i - list_.begin();
1014 delete *i;
1015 list_.erase(i);
1016
1017 if (index)
1018 *index = previous_index;
1019 return true;
1020 }
1021 }
1022 return false;
1023}
1024
1025ListValue::iterator ListValue::Erase(iterator iter,
1026 scoped_ptr<Value>* out_value) {
1027 if (out_value)
1028 out_value->reset(*iter);
1029 else
1030 delete *iter;
1031
1032 return list_.erase(iter);
1033}
1034
1035void ListValue::Append(Value* in_value) {
1036 DCHECK(in_value);
1037 list_.push_back(in_value);
1038}
1039
1040void ListValue::AppendBoolean(bool in_value) {
1041 Append(new FundamentalValue(in_value));
1042}
1043
1044void ListValue::AppendInteger(int in_value) {
1045 Append(new FundamentalValue(in_value));
1046}
1047
1048void ListValue::AppendDouble(double in_value) {
1049 Append(new FundamentalValue(in_value));
1050}
1051
1052void ListValue::AppendString(const std::string& in_value) {
1053 Append(new StringValue(in_value));
1054}
1055
1056void ListValue::AppendString(const string16& in_value) {
1057 Append(new StringValue(in_value));
1058}
1059
1060void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1061 for (std::vector<std::string>::const_iterator it = in_values.begin();
1062 it != in_values.end(); ++it) {
1063 AppendString(*it);
1064 }
1065}
1066
1067void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1068 for (std::vector<string16>::const_iterator it = in_values.begin();
1069 it != in_values.end(); ++it) {
1070 AppendString(*it);
1071 }
1072}
1073
1074bool ListValue::AppendIfNotPresent(Value* in_value) {
1075 DCHECK(in_value);
1076 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1077 if ((*i)->Equals(in_value)) {
1078 delete in_value;
1079 return false;
1080 }
1081 }
1082 list_.push_back(in_value);
1083 return true;
1084}
1085
1086bool ListValue::Insert(size_t index, Value* in_value) {
1087 DCHECK(in_value);
1088 if (index > list_.size())
1089 return false;
1090
1091 list_.insert(list_.begin() + index, in_value);
1092 return true;
1093}
1094
1095ListValue::const_iterator ListValue::Find(const Value& value) const {
1096 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1097}
1098
1099void ListValue::Swap(ListValue* other) {
1100 list_.swap(other->list_);
1101}
1102
1103bool ListValue::GetAsList(ListValue** out_value) {
1104 if (out_value)
1105 *out_value = this;
1106 return true;
1107}
1108
1109bool ListValue::GetAsList(const ListValue** out_value) const {
1110 if (out_value)
1111 *out_value = this;
1112 return true;
1113}
1114
1115ListValue* ListValue::DeepCopy() const {
1116 ListValue* result = new ListValue;
1117
1118 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1119 result->Append((*i)->DeepCopy());
1120
1121 return result;
1122}
1123
1124bool ListValue::Equals(const Value* other) const {
1125 if (other->GetType() != GetType())
1126 return false;
1127
1128 const ListValue* other_list =
1129 static_cast<const ListValue*>(other);
1130 const_iterator lhs_it, rhs_it;
1131 for (lhs_it = begin(), rhs_it = other_list->begin();
1132 lhs_it != end() && rhs_it != other_list->end();
1133 ++lhs_it, ++rhs_it) {
1134 if (!(*lhs_it)->Equals(*rhs_it))
1135 return false;
1136 }
1137 if (lhs_it != end() || rhs_it != other_list->end())
1138 return false;
1139
1140 return true;
1141}
1142
1143ValueSerializer::~ValueSerializer() {
1144}
1145
Dave Moorecc0e4f92015-03-10 15:23:04 -07001146ValueDeserializer::~ValueDeserializer() {
1147}
1148
James Robinson646469d2014-10-03 15:33:28 -07001149std::ostream& operator<<(std::ostream& out, const Value& value) {
1150 std::string json;
1151 JSONWriter::WriteWithOptions(&value,
1152 JSONWriter::OPTIONS_PRETTY_PRINT,
1153 &json);
1154 return out << json;
1155}
1156
1157} // namespace base