blob: 4534d27dff59a29ac2bf5f4028918a8659a7319b [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
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070023scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
24
James Robinson646469d2014-10-03 15:33:28 -070025// 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.
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070028scoped_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);
32 if (child_copy) {
33 if (!copy)
34 copy.reset(new ListValue);
35 copy->Append(child_copy.Pass());
James Robinson646469d2014-10-03 15:33:28 -070036 }
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070037 }
38 return copy;
39}
James Robinson646469d2014-10-03 15:33:28 -070040
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070041scoped_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());
46 if (child_copy) {
47 if (!copy)
48 copy.reset(new DictionaryValue);
49 copy->SetWithoutPathExpansion(it.key(), child_copy.Pass());
James Robinson646469d2014-10-03 15:33:28 -070050 }
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070051 }
52 return copy;
53}
54
55scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
56 switch (node.GetType()) {
57 case Value::TYPE_LIST:
58 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node));
59
60 case Value::TYPE_DICTIONARY:
61 return CopyDictionaryWithoutEmptyChildren(
62 static_cast<const DictionaryValue&>(node));
James Robinson646469d2014-10-03 15:33:28 -070063
64 default:
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -070065 return node.CreateDeepCopy();
James Robinson646469d2014-10-03 15:33:28 -070066 }
67}
68
69// A small functor for comparing Values for std::find_if and similar.
70class ValueEquals {
71 public:
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) { }
76
77 bool operator ()(const Value* second) const {
78 return first_->Equals(second);
79 }
80
81 private:
82 const Value* first_;
83};
84
85} // namespace
86
87Value::~Value() {
88}
89
90// static
James Robinsonc8f302a2015-05-14 16:38:33 -070091scoped_ptr<Value> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL));
James Robinson646469d2014-10-03 15:33:28 -070093}
94
James Robinson5e66a792015-01-21 17:02:08 -080095bool Value::GetAsBinary(const BinaryValue** out_value) const {
96 return false;
97}
98
James Robinson646469d2014-10-03 15:33:28 -070099bool Value::GetAsBoolean(bool* out_value) const {
100 return false;
101}
102
103bool Value::GetAsInteger(int* out_value) const {
104 return false;
105}
106
107bool Value::GetAsDouble(double* out_value) const {
108 return false;
109}
110
111bool Value::GetAsString(std::string* out_value) const {
112 return false;
113}
114
115bool Value::GetAsString(string16* out_value) const {
116 return false;
117}
118
119bool Value::GetAsString(const StringValue** out_value) const {
120 return false;
121}
122
123bool Value::GetAsList(ListValue** out_value) {
124 return false;
125}
126
127bool Value::GetAsList(const ListValue** out_value) const {
128 return false;
129}
130
131bool Value::GetAsDictionary(DictionaryValue** out_value) {
132 return false;
133}
134
135bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
136 return false;
137}
138
139Value* 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));
James Robinsonc8f302a2015-05-14 16:38:33 -0700143 return CreateNullValue().release();
144}
145
146scoped_ptr<Value> Value::CreateDeepCopy() const {
147 return make_scoped_ptr(DeepCopy());
James Robinson646469d2014-10-03 15:33:28 -0700148}
149
150bool 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);
155}
156
157// static
158bool Value::Equals(const Value* a, const Value* b) {
159 if ((a == NULL) && (b == NULL)) return true;
160 if ((a == NULL) ^ (b == NULL)) return false;
161 return a->Equals(b);
162}
163
164Value::Value(Type type) : type_(type) {}
165
166Value::Value(const Value& that) : type_(that.type_) {}
167
168Value& Value::operator=(const Value& that) {
169 type_ = that.type_;
170 return *this;
171}
172
173///////////////////// FundamentalValue ////////////////////
174
175FundamentalValue::FundamentalValue(bool in_value)
176 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
177}
178
179FundamentalValue::FundamentalValue(int in_value)
180 : Value(TYPE_INTEGER), integer_value_(in_value) {
181}
182
183FundamentalValue::FundamentalValue(double in_value)
184 : Value(TYPE_DOUBLE), double_value_(in_value) {
James Robinson0fae0002015-05-05 16:31:51 -0700185 if (!std::isfinite(double_value_)) {
James Robinson646469d2014-10-03 15:33:28 -0700186 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
187 << "values cannot be represented in JSON";
188 double_value_ = 0.0;
189 }
190}
191
192FundamentalValue::~FundamentalValue() {
193}
194
195bool FundamentalValue::GetAsBoolean(bool* out_value) const {
196 if (out_value && IsType(TYPE_BOOLEAN))
197 *out_value = boolean_value_;
198 return (IsType(TYPE_BOOLEAN));
199}
200
201bool FundamentalValue::GetAsInteger(int* out_value) const {
202 if (out_value && IsType(TYPE_INTEGER))
203 *out_value = integer_value_;
204 return (IsType(TYPE_INTEGER));
205}
206
207bool 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));
213}
214
215FundamentalValue* FundamentalValue::DeepCopy() const {
216 switch (GetType()) {
217 case TYPE_BOOLEAN:
218 return new FundamentalValue(boolean_value_);
219
220 case TYPE_INTEGER:
221 return new FundamentalValue(integer_value_);
222
223 case TYPE_DOUBLE:
224 return new FundamentalValue(double_value_);
225
226 default:
227 NOTREACHED();
228 return NULL;
229 }
230}
231
232bool FundamentalValue::Equals(const Value* other) const {
233 if (other->GetType() != GetType())
234 return false;
235
236 switch (GetType()) {
237 case TYPE_BOOLEAN: {
238 bool lhs, rhs;
239 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
240 }
241 case TYPE_INTEGER: {
242 int lhs, rhs;
243 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
244 }
245 case TYPE_DOUBLE: {
246 double lhs, rhs;
247 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
248 }
249 default:
250 NOTREACHED();
251 return false;
252 }
253}
254
255///////////////////// StringValue ////////////////////
256
257StringValue::StringValue(const std::string& in_value)
258 : Value(TYPE_STRING),
259 value_(in_value) {
260 DCHECK(IsStringUTF8(in_value));
261}
262
263StringValue::StringValue(const string16& in_value)
264 : Value(TYPE_STRING),
265 value_(UTF16ToUTF8(in_value)) {
266}
267
268StringValue::~StringValue() {
269}
270
271std::string* StringValue::GetString() {
272 return &value_;
273}
274
275const std::string& StringValue::GetString() const {
276 return value_;
277}
278
279bool StringValue::GetAsString(std::string* out_value) const {
280 if (out_value)
281 *out_value = value_;
282 return true;
283}
284
285bool StringValue::GetAsString(string16* out_value) const {
286 if (out_value)
287 *out_value = UTF8ToUTF16(value_);
288 return true;
289}
290
291bool StringValue::GetAsString(const StringValue** out_value) const {
292 if (out_value)
293 *out_value = this;
294 return true;
295}
296
297StringValue* StringValue::DeepCopy() const {
298 return new StringValue(value_);
299}
300
301bool StringValue::Equals(const Value* other) const {
302 if (other->GetType() != GetType())
303 return false;
304 std::string lhs, rhs;
305 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
306}
307
308///////////////////// BinaryValue ////////////////////
309
310BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY),
312 size_(0) {
313}
314
315BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
316 : Value(TYPE_BINARY),
317 buffer_(buffer.Pass()),
318 size_(size) {
319}
320
321BinaryValue::~BinaryValue() {
322}
323
324// static
325BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
326 size_t size) {
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);
331}
332
James Robinson5e66a792015-01-21 17:02:08 -0800333bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
334 if (out_value)
335 *out_value = this;
336 return true;
337}
338
James Robinson646469d2014-10-03 15:33:28 -0700339BinaryValue* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_.get(), size_);
341}
342
343bool BinaryValue::Equals(const Value* other) const {
344 if (other->GetType() != GetType())
345 return false;
346 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
347 if (other_binary->size_ != size_)
348 return false;
349 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
350}
351
352///////////////////// DictionaryValue ////////////////////
353
354DictionaryValue::DictionaryValue()
355 : Value(TYPE_DICTIONARY) {
356}
357
358DictionaryValue::~DictionaryValue() {
359 Clear();
360}
361
362bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
363 if (out_value)
364 *out_value = this;
365 return true;
366}
367
368bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
369 if (out_value)
370 *out_value = this;
371 return true;
372}
373
374bool 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();
379}
380
381void DictionaryValue::Clear() {
382 ValueMap::iterator dict_iterator = dictionary_.begin();
383 while (dict_iterator != dictionary_.end()) {
384 delete dict_iterator->second;
385 ++dict_iterator;
386 }
387
388 dictionary_.clear();
389}
390
Nick Bray27a3f6e2015-01-08 16:39:35 -0800391void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
James Robinson646469d2014-10-03 15:33:28 -0700392 DCHECK(IsStringUTF8(path));
393 DCHECK(in_value);
394
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);
406 }
407
408 current_dictionary = child_dictionary;
409 current_path.erase(0, delimiter_position + 1);
410 }
411
Nick Bray27a3f6e2015-01-08 16:39:35 -0800412 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
413}
414
415void DictionaryValue::Set(const std::string& path, Value* in_value) {
416 Set(path, make_scoped_ptr(in_value));
James Robinson646469d2014-10-03 15:33:28 -0700417}
418
419void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
420 Set(path, new FundamentalValue(in_value));
421}
422
423void DictionaryValue::SetInteger(const std::string& path, int in_value) {
424 Set(path, new FundamentalValue(in_value));
425}
426
427void DictionaryValue::SetDouble(const std::string& path, double in_value) {
428 Set(path, new FundamentalValue(in_value));
429}
430
431void DictionaryValue::SetString(const std::string& path,
432 const std::string& in_value) {
433 Set(path, new StringValue(in_value));
434}
435
436void DictionaryValue::SetString(const std::string& path,
437 const string16& in_value) {
438 Set(path, new StringValue(in_value));
439}
440
441void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
Nick Bray27a3f6e2015-01-08 16:39:35 -0800442 scoped_ptr<Value> in_value) {
443 Value* bare_ptr = in_value.release();
James Robinson646469d2014-10-03 15:33:28 -0700444 // 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 =
Nick Bray27a3f6e2015-01-08 16:39:35 -0800447 dictionary_.insert(std::make_pair(key, bare_ptr));
James Robinson646469d2014-10-03 15:33:28 -0700448 if (!ins_res.second) {
Nick Bray27a3f6e2015-01-08 16:39:35 -0800449 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
James Robinson646469d2014-10-03 15:33:28 -0700450 delete ins_res.first->second;
Nick Bray27a3f6e2015-01-08 16:39:35 -0800451 ins_res.first->second = bare_ptr;
James Robinson646469d2014-10-03 15:33:28 -0700452 }
453}
454
Nick Bray27a3f6e2015-01-08 16:39:35 -0800455void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
456 Value* in_value) {
457 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
458}
459
James Robinson646469d2014-10-03 15:33:28 -0700460void DictionaryValue::SetBooleanWithoutPathExpansion(
461 const std::string& path, bool in_value) {
462 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
463}
464
465void DictionaryValue::SetIntegerWithoutPathExpansion(
466 const std::string& path, int in_value) {
467 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
468}
469
470void DictionaryValue::SetDoubleWithoutPathExpansion(
471 const std::string& path, double in_value) {
472 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
473}
474
475void DictionaryValue::SetStringWithoutPathExpansion(
476 const std::string& path, const std::string& in_value) {
477 SetWithoutPathExpansion(path, new StringValue(in_value));
478}
479
480void DictionaryValue::SetStringWithoutPathExpansion(
481 const std::string& path, const string16& in_value) {
482 SetWithoutPathExpansion(path, new StringValue(in_value));
483}
484
485bool 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))
496 return false;
497
498 current_dictionary = child_dictionary;
499 current_path.erase(0, delimiter_position + 1);
500 }
501
502 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
503}
504
505bool DictionaryValue::Get(const std::string& path, Value** out_value) {
506 return static_cast<const DictionaryValue&>(*this).Get(
507 path,
508 const_cast<const Value**>(out_value));
509}
510
511bool DictionaryValue::GetBoolean(const std::string& path,
512 bool* bool_value) const {
513 const Value* value;
514 if (!Get(path, &value))
515 return false;
516
517 return value->GetAsBoolean(bool_value);
518}
519
520bool DictionaryValue::GetInteger(const std::string& path,
521 int* out_value) const {
522 const Value* value;
523 if (!Get(path, &value))
524 return false;
525
526 return value->GetAsInteger(out_value);
527}
528
529bool DictionaryValue::GetDouble(const std::string& path,
530 double* out_value) const {
531 const Value* value;
532 if (!Get(path, &value))
533 return false;
534
535 return value->GetAsDouble(out_value);
536}
537
538bool DictionaryValue::GetString(const std::string& path,
539 std::string* out_value) const {
540 const Value* value;
541 if (!Get(path, &value))
542 return false;
543
544 return value->GetAsString(out_value);
545}
546
547bool DictionaryValue::GetString(const std::string& path,
548 string16* out_value) const {
549 const Value* value;
550 if (!Get(path, &value))
551 return false;
552
553 return value->GetAsString(out_value);
554}
555
556bool DictionaryValue::GetStringASCII(const std::string& path,
557 std::string* out_value) const {
558 std::string out;
559 if (!GetString(path, &out))
560 return false;
561
562 if (!IsStringASCII(out)) {
563 NOTREACHED();
564 return false;
565 }
566
567 out_value->assign(out);
568 return true;
569}
570
571bool DictionaryValue::GetBinary(const std::string& path,
572 const BinaryValue** out_value) const {
573 const Value* value;
574 bool result = Get(path, &value);
575 if (!result || !value->IsType(TYPE_BINARY))
576 return false;
577
578 if (out_value)
579 *out_value = static_cast<const BinaryValue*>(value);
580
581 return true;
582}
583
584bool DictionaryValue::GetBinary(const std::string& path,
585 BinaryValue** out_value) {
586 return static_cast<const DictionaryValue&>(*this).GetBinary(
587 path,
588 const_cast<const BinaryValue**>(out_value));
589}
590
591bool DictionaryValue::GetDictionary(const std::string& path,
592 const DictionaryValue** out_value) const {
593 const Value* value;
594 bool result = Get(path, &value);
595 if (!result || !value->IsType(TYPE_DICTIONARY))
596 return false;
597
598 if (out_value)
599 *out_value = static_cast<const DictionaryValue*>(value);
600
601 return true;
602}
603
604bool DictionaryValue::GetDictionary(const std::string& path,
605 DictionaryValue** out_value) {
606 return static_cast<const DictionaryValue&>(*this).GetDictionary(
607 path,
608 const_cast<const DictionaryValue**>(out_value));
609}
610
611bool DictionaryValue::GetList(const std::string& path,
612 const ListValue** out_value) const {
613 const Value* value;
614 bool result = Get(path, &value);
615 if (!result || !value->IsType(TYPE_LIST))
616 return false;
617
618 if (out_value)
619 *out_value = static_cast<const ListValue*>(value);
620
621 return true;
622}
623
624bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
625 return static_cast<const DictionaryValue&>(*this).GetList(
626 path,
627 const_cast<const ListValue**>(out_value));
628}
629
630bool 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())
635 return false;
636
637 const Value* entry = entry_iterator->second;
638 if (out_value)
639 *out_value = entry;
640 return true;
641}
642
643bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
644 Value** out_value) {
645 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
646 key,
647 const_cast<const Value**>(out_value));
648}
649
650bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
651 bool* out_value) const {
652 const Value* value;
653 if (!GetWithoutPathExpansion(key, &value))
654 return false;
655
656 return value->GetAsBoolean(out_value);
657}
658
659bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
660 int* out_value) const {
661 const Value* value;
662 if (!GetWithoutPathExpansion(key, &value))
663 return false;
664
665 return value->GetAsInteger(out_value);
666}
667
668bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
669 double* out_value) const {
670 const Value* value;
671 if (!GetWithoutPathExpansion(key, &value))
672 return false;
673
674 return value->GetAsDouble(out_value);
675}
676
677bool DictionaryValue::GetStringWithoutPathExpansion(
678 const std::string& key,
679 std::string* out_value) const {
680 const Value* value;
681 if (!GetWithoutPathExpansion(key, &value))
682 return false;
683
684 return value->GetAsString(out_value);
685}
686
687bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
688 string16* out_value) const {
689 const Value* value;
690 if (!GetWithoutPathExpansion(key, &value))
691 return false;
692
693 return value->GetAsString(out_value);
694}
695
696bool DictionaryValue::GetDictionaryWithoutPathExpansion(
697 const std::string& key,
698 const DictionaryValue** out_value) const {
699 const Value* value;
700 bool result = GetWithoutPathExpansion(key, &value);
701 if (!result || !value->IsType(TYPE_DICTIONARY))
702 return false;
703
704 if (out_value)
705 *out_value = static_cast<const DictionaryValue*>(value);
706
707 return true;
708}
709
710bool 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(
716 key,
717 const_cast<const DictionaryValue**>(out_value));
718}
719
720bool DictionaryValue::GetListWithoutPathExpansion(
721 const std::string& key,
722 const ListValue** out_value) const {
723 const Value* value;
724 bool result = GetWithoutPathExpansion(key, &value);
725 if (!result || !value->IsType(TYPE_LIST))
726 return false;
727
728 if (out_value)
729 *out_value = static_cast<const ListValue*>(value);
730
731 return true;
732}
733
734bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
735 ListValue** out_value) {
736 return
737 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
738 key,
739 const_cast<const ListValue**>(out_value));
740}
741
742bool 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 &current_dictionary))
751 return false;
752 current_path.erase(0, delimiter_position + 1);
753 }
754
755 return current_dictionary->RemoveWithoutPathExpansion(current_path,
756 out_value);
757}
758
759bool 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())
764 return false;
765
766 Value* entry = entry_iterator->second;
767 if (out_value)
768 out_value->reset(entry);
769 else
770 delete entry;
771 dictionary_.erase(entry_iterator);
772 return true;
773}
774
775bool DictionaryValue::RemovePath(const std::string& path,
776 scoped_ptr<Value>* out_value) {
777 bool result = false;
778 size_t delimiter_position = path.find('.');
779
780 if (delimiter_position == std::string::npos)
781 return RemoveWithoutPathExpansion(path, out_value);
782
783 const std::string subdict_path = path.substr(0, delimiter_position);
784 DictionaryValue* subdict = NULL;
785 if (!GetDictionary(subdict_path, &subdict))
786 return false;
787 result = subdict->RemovePath(path.substr(delimiter_position + 1),
788 out_value);
789 if (result && subdict->empty())
790 RemoveWithoutPathExpansion(subdict_path, NULL);
791
792 return result;
793}
794
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -0700795scoped_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
796 const {
797 scoped_ptr<DictionaryValue> copy = CopyDictionaryWithoutEmptyChildren(*this);
798 if (!copy)
799 copy.reset(new DictionaryValue);
800 return copy;
James Robinson646469d2014-10-03 15:33:28 -0700801}
802
803void 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));
812 continue;
813 }
814 }
815 // All other cases: Make a copy and hook it up.
816 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
817 }
818}
819
820void DictionaryValue::Swap(DictionaryValue* other) {
821 dictionary_.swap(other->dictionary_);
822}
823
824DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
825 : target_(target),
826 it_(target.dictionary_.begin()) {}
827
828DictionaryValue::Iterator::~Iterator() {}
829
830DictionaryValue* DictionaryValue::DeepCopy() const {
831 DictionaryValue* result = new DictionaryValue;
832
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());
837 }
838
839 return result;
840}
841
James Robinsonc8f302a2015-05-14 16:38:33 -0700842scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
843 return make_scoped_ptr(DeepCopy());
844}
845
James Robinson646469d2014-10-03 15:33:28 -0700846bool DictionaryValue::Equals(const Value* other) const {
847 if (other->GetType() != GetType())
848 return false;
849
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())) {
857 return false;
858 }
859 lhs_it.Advance();
860 rhs_it.Advance();
861 }
862 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
863 return false;
864
865 return true;
866}
867
868///////////////////// ListValue ////////////////////
869
870ListValue::ListValue() : Value(TYPE_LIST) {
871}
872
873ListValue::~ListValue() {
874 Clear();
875}
876
877void ListValue::Clear() {
878 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
879 delete *i;
880 list_.clear();
881}
882
883bool ListValue::Set(size_t index, Value* in_value) {
884 if (!in_value)
885 return false;
886
887 if (index >= list_.size()) {
888 // Pad out any intermediate indexes with null settings
889 while (index > list_.size())
890 Append(CreateNullValue());
891 Append(in_value);
892 } else {
893 DCHECK(list_[index] != in_value);
894 delete list_[index];
895 list_[index] = in_value;
896 }
897 return true;
898}
899
James Robinsonc8f302a2015-05-14 16:38:33 -0700900bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
901 return Set(index, in_value.release());
902}
903
James Robinson646469d2014-10-03 15:33:28 -0700904bool ListValue::Get(size_t index, const Value** out_value) const {
905 if (index >= list_.size())
906 return false;
907
908 if (out_value)
909 *out_value = list_[index];
910
911 return true;
912}
913
914bool ListValue::Get(size_t index, Value** out_value) {
915 return static_cast<const ListValue&>(*this).Get(
916 index,
917 const_cast<const Value**>(out_value));
918}
919
920bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
921 const Value* value;
922 if (!Get(index, &value))
923 return false;
924
925 return value->GetAsBoolean(bool_value);
926}
927
928bool ListValue::GetInteger(size_t index, int* out_value) const {
929 const Value* value;
930 if (!Get(index, &value))
931 return false;
932
933 return value->GetAsInteger(out_value);
934}
935
936bool ListValue::GetDouble(size_t index, double* out_value) const {
937 const Value* value;
938 if (!Get(index, &value))
939 return false;
940
941 return value->GetAsDouble(out_value);
942}
943
944bool ListValue::GetString(size_t index, std::string* out_value) const {
945 const Value* value;
946 if (!Get(index, &value))
947 return false;
948
949 return value->GetAsString(out_value);
950}
951
952bool ListValue::GetString(size_t index, string16* out_value) const {
953 const Value* value;
954 if (!Get(index, &value))
955 return false;
956
957 return value->GetAsString(out_value);
958}
959
960bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
961 const Value* value;
962 bool result = Get(index, &value);
963 if (!result || !value->IsType(TYPE_BINARY))
964 return false;
965
966 if (out_value)
967 *out_value = static_cast<const BinaryValue*>(value);
968
969 return true;
970}
971
972bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
973 return static_cast<const ListValue&>(*this).GetBinary(
974 index,
975 const_cast<const BinaryValue**>(out_value));
976}
977
978bool ListValue::GetDictionary(size_t index,
979 const DictionaryValue** out_value) const {
980 const Value* value;
981 bool result = Get(index, &value);
982 if (!result || !value->IsType(TYPE_DICTIONARY))
983 return false;
984
985 if (out_value)
986 *out_value = static_cast<const DictionaryValue*>(value);
987
988 return true;
989}
990
991bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
992 return static_cast<const ListValue&>(*this).GetDictionary(
993 index,
994 const_cast<const DictionaryValue**>(out_value));
995}
996
997bool ListValue::GetList(size_t index, const ListValue** out_value) const {
998 const Value* value;
999 bool result = Get(index, &value);
1000 if (!result || !value->IsType(TYPE_LIST))
1001 return false;
1002
1003 if (out_value)
1004 *out_value = static_cast<const ListValue*>(value);
1005
1006 return true;
1007}
1008
1009bool ListValue::GetList(size_t index, ListValue** out_value) {
1010 return static_cast<const ListValue&>(*this).GetList(
1011 index,
1012 const_cast<const ListValue**>(out_value));
1013}
1014
1015bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1016 if (index >= list_.size())
1017 return false;
1018
1019 if (out_value)
1020 out_value->reset(list_[index]);
1021 else
1022 delete list_[index];
1023
1024 list_.erase(list_.begin() + index);
1025 return true;
1026}
1027
1028bool 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();
1032 delete *i;
1033 list_.erase(i);
1034
1035 if (index)
1036 *index = previous_index;
1037 return true;
1038 }
1039 }
1040 return false;
1041}
1042
1043ListValue::iterator ListValue::Erase(iterator iter,
1044 scoped_ptr<Value>* out_value) {
1045 if (out_value)
1046 out_value->reset(*iter);
1047 else
1048 delete *iter;
1049
1050 return list_.erase(iter);
1051}
1052
James Robinsonc8f302a2015-05-14 16:38:33 -07001053void ListValue::Append(scoped_ptr<Value> in_value) {
1054 Append(in_value.release());
1055}
1056
James Robinson646469d2014-10-03 15:33:28 -07001057void ListValue::Append(Value* in_value) {
1058 DCHECK(in_value);
1059 list_.push_back(in_value);
1060}
1061
1062void ListValue::AppendBoolean(bool in_value) {
1063 Append(new FundamentalValue(in_value));
1064}
1065
1066void ListValue::AppendInteger(int in_value) {
1067 Append(new FundamentalValue(in_value));
1068}
1069
1070void ListValue::AppendDouble(double in_value) {
1071 Append(new FundamentalValue(in_value));
1072}
1073
1074void ListValue::AppendString(const std::string& in_value) {
1075 Append(new StringValue(in_value));
1076}
1077
1078void ListValue::AppendString(const string16& in_value) {
1079 Append(new StringValue(in_value));
1080}
1081
1082void 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) {
1085 AppendString(*it);
1086 }
1087}
1088
1089void 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) {
1092 AppendString(*it);
1093 }
1094}
1095
1096bool ListValue::AppendIfNotPresent(Value* in_value) {
1097 DCHECK(in_value);
1098 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1099 if ((*i)->Equals(in_value)) {
1100 delete in_value;
1101 return false;
1102 }
1103 }
1104 list_.push_back(in_value);
1105 return true;
1106}
1107
1108bool ListValue::Insert(size_t index, Value* in_value) {
1109 DCHECK(in_value);
1110 if (index > list_.size())
1111 return false;
1112
1113 list_.insert(list_.begin() + index, in_value);
1114 return true;
1115}
1116
1117ListValue::const_iterator ListValue::Find(const Value& value) const {
1118 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1119}
1120
1121void ListValue::Swap(ListValue* other) {
1122 list_.swap(other->list_);
1123}
1124
1125bool ListValue::GetAsList(ListValue** out_value) {
1126 if (out_value)
1127 *out_value = this;
1128 return true;
1129}
1130
1131bool ListValue::GetAsList(const ListValue** out_value) const {
1132 if (out_value)
1133 *out_value = this;
1134 return true;
1135}
1136
1137ListValue* ListValue::DeepCopy() const {
1138 ListValue* result = new ListValue;
1139
1140 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1141 result->Append((*i)->DeepCopy());
1142
1143 return result;
1144}
1145
James Robinsonc8f302a2015-05-14 16:38:33 -07001146scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1147 return make_scoped_ptr(DeepCopy());
1148}
1149
James Robinson646469d2014-10-03 15:33:28 -07001150bool ListValue::Equals(const Value* other) const {
1151 if (other->GetType() != GetType())
1152 return false;
1153
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))
1161 return false;
1162 }
1163 if (lhs_it != end() || rhs_it != other_list->end())
1164 return false;
1165
1166 return true;
1167}
1168
1169ValueSerializer::~ValueSerializer() {
1170}
1171
Dave Moorecc0e4f92015-03-10 15:23:04 -07001172ValueDeserializer::~ValueDeserializer() {
1173}
1174
James Robinson646469d2014-10-03 15:33:28 -07001175std::ostream& operator<<(std::ostream& out, const Value& value) {
1176 std::string json;
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -07001177 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
James Robinson646469d2014-10-03 15:33:28 -07001178 return out << json;
1179}
1180
1181} // namespace base