value_util.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2018 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. #include "components/mirroring/service/value_util.h"
  5. namespace mirroring {
  6. bool GetInt(const base::Value& value, const std::string& key, int32_t* result) {
  7. auto* found = value.GetDict().Find(key);
  8. if (!found || found->is_none())
  9. return true;
  10. if (found->is_int()) {
  11. *result = found->GetInt();
  12. return true;
  13. }
  14. return false;
  15. }
  16. bool GetDouble(const base::Value& value,
  17. const std::string& key,
  18. double* result) {
  19. auto* found = value.GetDict().Find(key);
  20. if (!found || found->is_none())
  21. return true;
  22. if (found->is_double()) {
  23. *result = found->GetDouble();
  24. return true;
  25. }
  26. if (found->is_int()) {
  27. *result = found->GetInt();
  28. return true;
  29. }
  30. return false;
  31. }
  32. bool GetString(const base::Value& value,
  33. const std::string& key,
  34. std::string* result) {
  35. auto* found = value.GetDict().Find(key);
  36. if (!found || found->is_none())
  37. return true;
  38. if (found->is_string()) {
  39. *result = found->GetString();
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool GetBool(const base::Value& value, const std::string& key, bool* result) {
  45. auto* found = value.GetDict().Find(key);
  46. if (!found || found->is_none())
  47. return true;
  48. if (found->is_bool()) {
  49. *result = found->GetBool();
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool GetIntArray(const base::Value& value,
  55. const std::string& key,
  56. std::vector<int32_t>* result) {
  57. auto* found = value.GetDict().Find(key);
  58. if (!found || found->is_none())
  59. return true;
  60. if (!found->is_list())
  61. return false;
  62. for (const auto& number_value : found->GetList()) {
  63. if (number_value.is_int())
  64. result->emplace_back(number_value.GetInt());
  65. else
  66. return false;
  67. }
  68. return true;
  69. }
  70. bool GetStringArray(const base::Value& value,
  71. const std::string& key,
  72. std::vector<std::string>* result) {
  73. auto* found = value.GetDict().Find(key);
  74. if (!found || found->is_none())
  75. return true;
  76. if (!found->is_list())
  77. return false;
  78. for (const auto& string_value : found->GetList()) {
  79. if (string_value.is_string())
  80. result->emplace_back(string_value.GetString());
  81. else
  82. return false;
  83. }
  84. return true;
  85. }
  86. } // namespace mirroring