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