blob: 4093eba67aaf39be6ef8f648e49dfebacce1c39c [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>
James Robinson0fae0002015-05-05 16:31:51 -070010#include <cmath>
James Robinson646469d2014-10-03 15:33:28 -070011#include <ostream>
12
James Robinson646469d2014-10-03 15:33:28 -070013#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
James Robinsonc8f302a2015-05-14 16:38:33 -070088scoped_ptr<Value> Value::CreateNullValue() {
89 return make_scoped_ptr(new Value(TYPE_NULL));
James Robinson646469d2014-10-03 15:33:28 -070090}
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));
James Robinsonc8f302a2015-05-14 16:38:33 -0700140 return CreateNullValue().release();
141}
142
143scoped_ptr<Value> Value::CreateDeepCopy() const {
144 return make_scoped_ptr(DeepCopy());
James Robinson646469d2014-10-03 15:33:28 -0700145}
146
147bool 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);
152}
153
154// static
155bool Value::Equals(const Value* a, const Value* b) {
156 if ((a == NULL) && (b == NULL)) return true;
157 if ((a == NULL) ^ (b == NULL)) return false;
158 return a->Equals(b);
159}
160
161Value::Value(Type type) : type_(type) {}
162
163Value::Value(const Value& that) : type_(that.type_) {}
164
165Value& Value::operator=(const Value& that) {
166 type_ = that.type_;
167 return *this;
168}
169
170///////////////////// FundamentalValue ////////////////////
171
172FundamentalValue::FundamentalValue(bool in_value)
173 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
174}
175
176FundamentalValue::FundamentalValue(int in_value)
177 : Value(TYPE_INTEGER), integer_value_(in_value) {
178}
179
180FundamentalValue::FundamentalValue(double in_value)
181 : Value(TYPE_DOUBLE), double_value_(in_value) {
James Robinson0fae0002015-05-05 16:31:51 -0700182 if (!std::isfinite(double_value_)) {
James Robinson646469d2014-10-03 15:33:28 -0700183 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
184 << "values cannot be represented in JSON";
185 double_value_ = 0.0;
186 }
187}
188
189FundamentalValue::~FundamentalValue() {
190}
191
192bool FundamentalValue::GetAsBoolean(bool* out_value) const {
193 if (out_value && IsType(TYPE_BOOLEAN))
194 *out_value = boolean_value_;
195 return (IsType(TYPE_BOOLEAN));
196}
197
198bool FundamentalValue::GetAsInteger(int* out_value) const {
199 if (out_value && IsType(TYPE_INTEGER))
200 *out_value = integer_value_;
201 return (IsType(TYPE_INTEGER));
202}
203
204bool 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));
210}
211
212FundamentalValue* FundamentalValue::DeepCopy() const {
213 switch (GetType()) {
214 case TYPE_BOOLEAN:
215 return new FundamentalValue(boolean_value_);
216
217 case TYPE_INTEGER:
218 return new FundamentalValue(integer_value_);
219
220 case TYPE_DOUBLE:
221 return new FundamentalValue(double_value_);
222
223 default:
224 NOTREACHED();
225 return NULL;
226 }
227}
228
229bool FundamentalValue::Equals(const Value* other) const {
230 if (other->GetType() != GetType())
231 return false;
232
233 switch (GetType()) {
234 case TYPE_BOOLEAN: {
235 bool lhs, rhs;
236 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
237 }
238 case TYPE_INTEGER: {
239 int lhs, rhs;
240 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
241 }
242 case TYPE_DOUBLE: {
243 double lhs, rhs;
244 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
245 }
246 default:
247 NOTREACHED();
248 return false;
249 }
250}
251
252///////////////////// StringValue ////////////////////
253
254StringValue::StringValue(const std::string& in_value)
255 : Value(TYPE_STRING),
256 value_(in_value) {
257 DCHECK(IsStringUTF8(in_value));
258}
259
260StringValue::StringValue(const string16& in_value)
261 : Value(TYPE_STRING),
262 value_(UTF16ToUTF8(in_value)) {
263}
264
265StringValue::~StringValue() {
266}
267
268std::string* StringValue::GetString() {
269 return &value_;
270}
271
272const std::string& StringValue::GetString() const {
273 return value_;
274}
275
276bool StringValue::GetAsString(std::string* out_value) const {
277 if (out_value)
278 *out_value = value_;
279 return true;
280}
281
282bool StringValue::GetAsString(string16* out_value) const {
283 if (out_value)
284 *out_value = UTF8ToUTF16(value_);
285 return true;
286}
287
288bool StringValue::GetAsString(const StringValue** out_value) const {
289 if (out_value)
290 *out_value = this;
291 return true;
292}
293
294StringValue* StringValue::DeepCopy() const {
295 return new StringValue(value_);
296}
297
298bool StringValue::Equals(const Value* other) const {
299 if (other->GetType() != GetType())
300 return false;
301 std::string lhs, rhs;
302 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
303}
304
305///////////////////// BinaryValue ////////////////////
306
307BinaryValue::BinaryValue()
308 : Value(TYPE_BINARY),
309 size_(0) {
310}
311
312BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
313 : Value(TYPE_BINARY),
314 buffer_(buffer.Pass()),
315 size_(size) {
316}
317
318BinaryValue::~BinaryValue() {
319}
320
321// static
322BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
323 size_t size) {
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);
328}
329
James Robinson5e66a792015-01-21 17:02:08 -0800330bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
331 if (out_value)
332 *out_value = this;
333 return true;
334}
335
James Robinson646469d2014-10-03 15:33:28 -0700336BinaryValue* BinaryValue::DeepCopy() const {
337 return CreateWithCopiedBuffer(buffer_.get(), size_);
338}
339
340bool BinaryValue::Equals(const Value* other) const {
341 if (other->GetType() != GetType())
342 return false;
343 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
344 if (other_binary->size_ != size_)
345 return false;
346 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
347}
348
349///////////////////// DictionaryValue ////////////////////
350
351DictionaryValue::DictionaryValue()
352 : Value(TYPE_DICTIONARY) {
353}
354
355DictionaryValue::~DictionaryValue() {
356 Clear();
357}
358
359bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
360 if (out_value)
361 *out_value = this;
362 return true;
363}
364
365bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
366 if (out_value)
367 *out_value = this;
368 return true;
369}
370
371bool 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();
376}
377
378void DictionaryValue::Clear() {
379 ValueMap::iterator dict_iterator = dictionary_.begin();
380 while (dict_iterator != dictionary_.end()) {
381 delete dict_iterator->second;
382 ++dict_iterator;
383 }
384
385 dictionary_.clear();
386}
387
Nick Bray27a3f6e2015-01-08 16:39:35 -0800388void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
James Robinson646469d2014-10-03 15:33:28 -0700389 DCHECK(IsStringUTF8(path));
390 DCHECK(in_value);
391
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);
403 }
404
405 current_dictionary = child_dictionary;
406 current_path.erase(0, delimiter_position + 1);
407 }
408
Nick Bray27a3f6e2015-01-08 16:39:35 -0800409 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
410}
411
412void DictionaryValue::Set(const std::string& path, Value* in_value) {
413 Set(path, make_scoped_ptr(in_value));
James Robinson646469d2014-10-03 15:33:28 -0700414}
415
416void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
417 Set(path, new FundamentalValue(in_value));
418}
419
420void DictionaryValue::SetInteger(const std::string& path, int in_value) {
421 Set(path, new FundamentalValue(in_value));
422}
423
424void DictionaryValue::SetDouble(const std::string& path, double in_value) {
425 Set(path, new FundamentalValue(in_value));
426}
427
428void DictionaryValue::SetString(const std::string& path,
429 const std::string& in_value) {
430 Set(path, new StringValue(in_value));
431}
432
433void DictionaryValue::SetString(const std::string& path,
434 const string16& in_value) {
435 Set(path, new StringValue(in_value));
436}
437
438void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
Nick Bray27a3f6e2015-01-08 16:39:35 -0800439 scoped_ptr<Value> in_value) {
440 Value* bare_ptr = in_value.release();
James Robinson646469d2014-10-03 15:33:28 -0700441 // 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 =
Nick Bray27a3f6e2015-01-08 16:39:35 -0800444 dictionary_.insert(std::make_pair(key, bare_ptr));
James Robinson646469d2014-10-03 15:33:28 -0700445 if (!ins_res.second) {
Nick Bray27a3f6e2015-01-08 16:39:35 -0800446 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
James Robinson646469d2014-10-03 15:33:28 -0700447 delete ins_res.first->second;
Nick Bray27a3f6e2015-01-08 16:39:35 -0800448 ins_res.first->second = bare_ptr;
James Robinson646469d2014-10-03 15:33:28 -0700449 }
450}
451
Nick Bray27a3f6e2015-01-08 16:39:35 -0800452void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
453 Value* in_value) {
454 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
455}
456
James Robinson646469d2014-10-03 15:33:28 -0700457void DictionaryValue::SetBooleanWithoutPathExpansion(
458 const std::string& path, bool in_value) {
459 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
460}
461
462void DictionaryValue::SetIntegerWithoutPathExpansion(
463 const std::string& path, int in_value) {
464 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
465}
466
467void DictionaryValue::SetDoubleWithoutPathExpansion(
468 const std::string& path, double in_value) {
469 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
470}
471
472void DictionaryValue::SetStringWithoutPathExpansion(
473 const std::string& path, const std::string& in_value) {
474 SetWithoutPathExpansion(path, new StringValue(in_value));
475}
476
477void DictionaryValue::SetStringWithoutPathExpansion(
478 const std::string& path, const string16& in_value) {
479 SetWithoutPathExpansion(path, new StringValue(in_value));
480}
481
482bool 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))
493 return false;
494
495 current_dictionary = child_dictionary;
496 current_path.erase(0, delimiter_position + 1);
497 }
498
499 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
500}
501
502bool DictionaryValue::Get(const std::string& path, Value** out_value) {
503 return static_cast<const DictionaryValue&>(*this).Get(
504 path,
505 const_cast<const Value**>(out_value));
506}
507
508bool DictionaryValue::GetBoolean(const std::string& path,
509 bool* bool_value) const {
510 const Value* value;
511 if (!Get(path, &value))
512 return false;
513
514 return value->GetAsBoolean(bool_value);
515}
516
517bool DictionaryValue::GetInteger(const std::string& path,
518 int* out_value) const {
519 const Value* value;
520 if (!Get(path, &value))
521 return false;
522
523 return value->GetAsInteger(out_value);
524}
525
526bool DictionaryValue::GetDouble(const std::string& path,
527 double* out_value) const {
528 const Value* value;
529 if (!Get(path, &value))
530 return false;
531
532 return value->GetAsDouble(out_value);
533}
534
535bool DictionaryValue::GetString(const std::string& path,
536 std::string* out_value) const {
537 const Value* value;
538 if (!Get(path, &value))
539 return false;
540
541 return value->GetAsString(out_value);
542}
543
544bool DictionaryValue::GetString(const std::string& path,
545 string16* out_value) const {
546 const Value* value;
547 if (!Get(path, &value))
548 return false;
549
550 return value->GetAsString(out_value);
551}
552
553bool DictionaryValue::GetStringASCII(const std::string& path,
554 std::string* out_value) const {
555 std::string out;
556 if (!GetString(path, &out))
557 return false;
558
559 if (!IsStringASCII(out)) {
560 NOTREACHED();
561 return false;
562 }
563
564 out_value->assign(out);
565 return true;
566}
567
568bool DictionaryValue::GetBinary(const std::string& path,
569 const BinaryValue** out_value) const {
570 const Value* value;
571 bool result = Get(path, &value);
572 if (!result || !value->IsType(TYPE_BINARY))
573 return false;
574
575 if (out_value)
576 *out_value = static_cast<const BinaryValue*>(value);
577
578 return true;
579}
580
581bool DictionaryValue::GetBinary(const std::string& path,
582 BinaryValue** out_value) {
583 return static_cast<const DictionaryValue&>(*this).GetBinary(
584 path,
585 const_cast<const BinaryValue**>(out_value));
586}
587
588bool DictionaryValue::GetDictionary(const std::string& path,
589 const DictionaryValue** out_value) const {
590 const Value* value;
591 bool result = Get(path, &value);
592 if (!result || !value->IsType(TYPE_DICTIONARY))
593 return false;
594
595 if (out_value)
596 *out_value = static_cast<const DictionaryValue*>(value);
597
598 return true;
599}
600
601bool DictionaryValue::GetDictionary(const std::string& path,
602 DictionaryValue** out_value) {
603 return static_cast<const DictionaryValue&>(*this).GetDictionary(
604 path,
605 const_cast<const DictionaryValue**>(out_value));
606}
607
608bool DictionaryValue::GetList(const std::string& path,
609 const ListValue** out_value) const {
610 const Value* value;
611 bool result = Get(path, &value);
612 if (!result || !value->IsType(TYPE_LIST))
613 return false;
614
615 if (out_value)
616 *out_value = static_cast<const ListValue*>(value);
617
618 return true;
619}
620
621bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
622 return static_cast<const DictionaryValue&>(*this).GetList(
623 path,
624 const_cast<const ListValue**>(out_value));
625}
626
627bool 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())
632 return false;
633
634 const Value* entry = entry_iterator->second;
635 if (out_value)
636 *out_value = entry;
637 return true;
638}
639
640bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
641 Value** out_value) {
642 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
643 key,
644 const_cast<const Value**>(out_value));
645}
646
647bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
648 bool* out_value) const {
649 const Value* value;
650 if (!GetWithoutPathExpansion(key, &value))
651 return false;
652
653 return value->GetAsBoolean(out_value);
654}
655
656bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
657 int* out_value) const {
658 const Value* value;
659 if (!GetWithoutPathExpansion(key, &value))
660 return false;
661
662 return value->GetAsInteger(out_value);
663}
664
665bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
666 double* out_value) const {
667 const Value* value;
668 if (!GetWithoutPathExpansion(key, &value))
669 return false;
670
671 return value->GetAsDouble(out_value);
672}
673
674bool DictionaryValue::GetStringWithoutPathExpansion(
675 const std::string& key,
676 std::string* out_value) const {
677 const Value* value;
678 if (!GetWithoutPathExpansion(key, &value))
679 return false;
680
681 return value->GetAsString(out_value);
682}
683
684bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
685 string16* out_value) const {
686 const Value* value;
687 if (!GetWithoutPathExpansion(key, &value))
688 return false;
689
690 return value->GetAsString(out_value);
691}
692
693bool DictionaryValue::GetDictionaryWithoutPathExpansion(
694 const std::string& key,
695 const DictionaryValue** out_value) const {
696 const Value* value;
697 bool result = GetWithoutPathExpansion(key, &value);
698 if (!result || !value->IsType(TYPE_DICTIONARY))
699 return false;
700
701 if (out_value)
702 *out_value = static_cast<const DictionaryValue*>(value);
703
704 return true;
705}
706
707bool 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(
713 key,
714 const_cast<const DictionaryValue**>(out_value));
715}
716
717bool DictionaryValue::GetListWithoutPathExpansion(
718 const std::string& key,
719 const ListValue** out_value) const {
720 const Value* value;
721 bool result = GetWithoutPathExpansion(key, &value);
722 if (!result || !value->IsType(TYPE_LIST))
723 return false;
724
725 if (out_value)
726 *out_value = static_cast<const ListValue*>(value);
727
728 return true;
729}
730
731bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
732 ListValue** out_value) {
733 return
734 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
735 key,
736 const_cast<const ListValue**>(out_value));
737}
738
739bool 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 &current_dictionary))
748 return false;
749 current_path.erase(0, delimiter_position + 1);
750 }
751
752 return current_dictionary->RemoveWithoutPathExpansion(current_path,
753 out_value);
754}
755
756bool 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())
761 return false;
762
763 Value* entry = entry_iterator->second;
764 if (out_value)
765 out_value->reset(entry);
766 else
767 delete entry;
768 dictionary_.erase(entry_iterator);
769 return true;
770}
771
772bool DictionaryValue::RemovePath(const std::string& path,
773 scoped_ptr<Value>* out_value) {
774 bool result = false;
775 size_t delimiter_position = path.find('.');
776
777 if (delimiter_position == std::string::npos)
778 return RemoveWithoutPathExpansion(path, out_value);
779
780 const std::string subdict_path = path.substr(0, delimiter_position);
781 DictionaryValue* subdict = NULL;
782 if (!GetDictionary(subdict_path, &subdict))
783 return false;
784 result = subdict->RemovePath(path.substr(delimiter_position + 1),
785 out_value);
786 if (result && subdict->empty())
787 RemoveWithoutPathExpansion(subdict_path, NULL);
788
789 return result;
790}
791
792DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
793 Value* copy = CopyWithoutEmptyChildren(this);
794 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
795}
796
797void 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));
806 continue;
807 }
808 }
809 // All other cases: Make a copy and hook it up.
810 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
811 }
812}
813
814void DictionaryValue::Swap(DictionaryValue* other) {
815 dictionary_.swap(other->dictionary_);
816}
817
818DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
819 : target_(target),
820 it_(target.dictionary_.begin()) {}
821
822DictionaryValue::Iterator::~Iterator() {}
823
824DictionaryValue* DictionaryValue::DeepCopy() const {
825 DictionaryValue* result = new DictionaryValue;
826
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());
831 }
832
833 return result;
834}
835
James Robinsonc8f302a2015-05-14 16:38:33 -0700836scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
837 return make_scoped_ptr(DeepCopy());
838}
839
James Robinson646469d2014-10-03 15:33:28 -0700840bool DictionaryValue::Equals(const Value* other) const {
841 if (other->GetType() != GetType())
842 return false;
843
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())) {
851 return false;
852 }
853 lhs_it.Advance();
854 rhs_it.Advance();
855 }
856 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
857 return false;
858
859 return true;
860}
861
862///////////////////// ListValue ////////////////////
863
864ListValue::ListValue() : Value(TYPE_LIST) {
865}
866
867ListValue::~ListValue() {
868 Clear();
869}
870
871void ListValue::Clear() {
872 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
873 delete *i;
874 list_.clear();
875}
876
877bool ListValue::Set(size_t index, Value* in_value) {
878 if (!in_value)
879 return false;
880
881 if (index >= list_.size()) {
882 // Pad out any intermediate indexes with null settings
883 while (index > list_.size())
884 Append(CreateNullValue());
885 Append(in_value);
886 } else {
887 DCHECK(list_[index] != in_value);
888 delete list_[index];
889 list_[index] = in_value;
890 }
891 return true;
892}
893
James Robinsonc8f302a2015-05-14 16:38:33 -0700894bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
895 return Set(index, in_value.release());
896}
897
James Robinson646469d2014-10-03 15:33:28 -0700898bool ListValue::Get(size_t index, const Value** out_value) const {
899 if (index >= list_.size())
900 return false;
901
902 if (out_value)
903 *out_value = list_[index];
904
905 return true;
906}
907
908bool ListValue::Get(size_t index, Value** out_value) {
909 return static_cast<const ListValue&>(*this).Get(
910 index,
911 const_cast<const Value**>(out_value));
912}
913
914bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
915 const Value* value;
916 if (!Get(index, &value))
917 return false;
918
919 return value->GetAsBoolean(bool_value);
920}
921
922bool ListValue::GetInteger(size_t index, int* out_value) const {
923 const Value* value;
924 if (!Get(index, &value))
925 return false;
926
927 return value->GetAsInteger(out_value);
928}
929
930bool ListValue::GetDouble(size_t index, double* out_value) const {
931 const Value* value;
932 if (!Get(index, &value))
933 return false;
934
935 return value->GetAsDouble(out_value);
936}
937
938bool ListValue::GetString(size_t index, std::string* out_value) const {
939 const Value* value;
940 if (!Get(index, &value))
941 return false;
942
943 return value->GetAsString(out_value);
944}
945
946bool ListValue::GetString(size_t index, string16* out_value) const {
947 const Value* value;
948 if (!Get(index, &value))
949 return false;
950
951 return value->GetAsString(out_value);
952}
953
954bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
955 const Value* value;
956 bool result = Get(index, &value);
957 if (!result || !value->IsType(TYPE_BINARY))
958 return false;
959
960 if (out_value)
961 *out_value = static_cast<const BinaryValue*>(value);
962
963 return true;
964}
965
966bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
967 return static_cast<const ListValue&>(*this).GetBinary(
968 index,
969 const_cast<const BinaryValue**>(out_value));
970}
971
972bool ListValue::GetDictionary(size_t index,
973 const DictionaryValue** out_value) const {
974 const Value* value;
975 bool result = Get(index, &value);
976 if (!result || !value->IsType(TYPE_DICTIONARY))
977 return false;
978
979 if (out_value)
980 *out_value = static_cast<const DictionaryValue*>(value);
981
982 return true;
983}
984
985bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
986 return static_cast<const ListValue&>(*this).GetDictionary(
987 index,
988 const_cast<const DictionaryValue**>(out_value));
989}
990
991bool ListValue::GetList(size_t index, const ListValue** out_value) const {
992 const Value* value;
993 bool result = Get(index, &value);
994 if (!result || !value->IsType(TYPE_LIST))
995 return false;
996
997 if (out_value)
998 *out_value = static_cast<const ListValue*>(value);
999
1000 return true;
1001}
1002
1003bool ListValue::GetList(size_t index, ListValue** out_value) {
1004 return static_cast<const ListValue&>(*this).GetList(
1005 index,
1006 const_cast<const ListValue**>(out_value));
1007}
1008
1009bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1010 if (index >= list_.size())
1011 return false;
1012
1013 if (out_value)
1014 out_value->reset(list_[index]);
1015 else
1016 delete list_[index];
1017
1018 list_.erase(list_.begin() + index);
1019 return true;
1020}
1021
1022bool 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();
1026 delete *i;
1027 list_.erase(i);
1028
1029 if (index)
1030 *index = previous_index;
1031 return true;
1032 }
1033 }
1034 return false;
1035}
1036
1037ListValue::iterator ListValue::Erase(iterator iter,
1038 scoped_ptr<Value>* out_value) {
1039 if (out_value)
1040 out_value->reset(*iter);
1041 else
1042 delete *iter;
1043
1044 return list_.erase(iter);
1045}
1046
James Robinsonc8f302a2015-05-14 16:38:33 -07001047void ListValue::Append(scoped_ptr<Value> in_value) {
1048 Append(in_value.release());
1049}
1050
James Robinson646469d2014-10-03 15:33:28 -07001051void ListValue::Append(Value* in_value) {
1052 DCHECK(in_value);
1053 list_.push_back(in_value);
1054}
1055
1056void ListValue::AppendBoolean(bool in_value) {
1057 Append(new FundamentalValue(in_value));
1058}
1059
1060void ListValue::AppendInteger(int in_value) {
1061 Append(new FundamentalValue(in_value));
1062}
1063
1064void ListValue::AppendDouble(double in_value) {
1065 Append(new FundamentalValue(in_value));
1066}
1067
1068void ListValue::AppendString(const std::string& in_value) {
1069 Append(new StringValue(in_value));
1070}
1071
1072void ListValue::AppendString(const string16& in_value) {
1073 Append(new StringValue(in_value));
1074}
1075
1076void 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) {
1079 AppendString(*it);
1080 }
1081}
1082
1083void 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) {
1086 AppendString(*it);
1087 }
1088}
1089
1090bool ListValue::AppendIfNotPresent(Value* in_value) {
1091 DCHECK(in_value);
1092 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1093 if ((*i)->Equals(in_value)) {
1094 delete in_value;
1095 return false;
1096 }
1097 }
1098 list_.push_back(in_value);
1099 return true;
1100}
1101
1102bool ListValue::Insert(size_t index, Value* in_value) {
1103 DCHECK(in_value);
1104 if (index > list_.size())
1105 return false;
1106
1107 list_.insert(list_.begin() + index, in_value);
1108 return true;
1109}
1110
1111ListValue::const_iterator ListValue::Find(const Value& value) const {
1112 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1113}
1114
1115void ListValue::Swap(ListValue* other) {
1116 list_.swap(other->list_);
1117}
1118
1119bool ListValue::GetAsList(ListValue** out_value) {
1120 if (out_value)
1121 *out_value = this;
1122 return true;
1123}
1124
1125bool ListValue::GetAsList(const ListValue** out_value) const {
1126 if (out_value)
1127 *out_value = this;
1128 return true;
1129}
1130
1131ListValue* ListValue::DeepCopy() const {
1132 ListValue* result = new ListValue;
1133
1134 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1135 result->Append((*i)->DeepCopy());
1136
1137 return result;
1138}
1139
James Robinsonc8f302a2015-05-14 16:38:33 -07001140scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1141 return make_scoped_ptr(DeepCopy());
1142}
1143
James Robinson646469d2014-10-03 15:33:28 -07001144bool ListValue::Equals(const Value* other) const {
1145 if (other->GetType() != GetType())
1146 return false;
1147
1148 const ListValue* other_list =
1149 static_cast<const ListValue*>(other);
1150 const_iterator lhs_it, rhs_it;
1151 for (lhs_it = begin(), rhs_it = other_list->begin();
1152 lhs_it != end() && rhs_it != other_list->end();
1153 ++lhs_it, ++rhs_it) {
1154 if (!(*lhs_it)->Equals(*rhs_it))
1155 return false;
1156 }
1157 if (lhs_it != end() || rhs_it != other_list->end())
1158 return false;
1159
1160 return true;
1161}
1162
1163ValueSerializer::~ValueSerializer() {
1164}
1165
Dave Moorecc0e4f92015-03-10 15:23:04 -07001166ValueDeserializer::~ValueDeserializer() {
1167}
1168
James Robinson646469d2014-10-03 15:33:28 -07001169std::ostream& operator<<(std::ostream& out, const Value& value) {
1170 std::string json;
1171 JSONWriter::WriteWithOptions(&value,
1172 JSONWriter::OPTIONS_PRETTY_PRINT,
1173 &json);
1174 return out << json;
1175}
1176
1177} // namespace base