dict_util.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/mac/dict_util.h"
  5. #include "base/json/json_writer.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #include "base/values.h"
  8. namespace atom {
  9. NSArray* ListValueToNSArray(const base::ListValue& value) {
  10. std::string json;
  11. if (!base::JSONWriter::Write(value, &json))
  12. return nil;
  13. NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
  14. id obj =
  15. [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
  16. if (![obj isKindOfClass:[NSArray class]])
  17. return nil;
  18. return obj;
  19. }
  20. std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
  21. if (!arr)
  22. return nullptr;
  23. std::unique_ptr<base::ListValue> result(new base::ListValue);
  24. for (id value in arr) {
  25. if ([value isKindOfClass:[NSString class]]) {
  26. result->AppendString(base::SysNSStringToUTF8(value));
  27. } else if ([value isKindOfClass:[NSNumber class]]) {
  28. const char* objc_type = [value objCType];
  29. if (strcmp(objc_type, @encode(BOOL)) == 0 ||
  30. strcmp(objc_type, @encode(char)) == 0)
  31. result->AppendBoolean([value boolValue]);
  32. else if (strcmp(objc_type, @encode(double)) == 0 ||
  33. strcmp(objc_type, @encode(float)) == 0)
  34. result->AppendDouble([value doubleValue]);
  35. else
  36. result->AppendInteger([value intValue]);
  37. } else if ([value isKindOfClass:[NSArray class]]) {
  38. std::unique_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
  39. if (sub_arr)
  40. result->Append(std::move(sub_arr));
  41. else
  42. result->Append(std::make_unique<base::Value>());
  43. } else if ([value isKindOfClass:[NSDictionary class]]) {
  44. std::unique_ptr<base::DictionaryValue> sub_dict =
  45. NSDictionaryToDictionaryValue(value);
  46. if (sub_dict)
  47. result->Append(std::move(sub_dict));
  48. else
  49. result->Append(std::make_unique<base::Value>());
  50. } else {
  51. result->AppendString(base::SysNSStringToUTF8([value description]));
  52. }
  53. }
  54. return result;
  55. }
  56. NSDictionary* DictionaryValueToNSDictionary(
  57. const base::DictionaryValue& value) {
  58. std::string json;
  59. if (!base::JSONWriter::Write(value, &json))
  60. return nil;
  61. NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
  62. id obj =
  63. [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
  64. if (![obj isKindOfClass:[NSDictionary class]])
  65. return nil;
  66. return obj;
  67. }
  68. std::unique_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
  69. NSDictionary* dict) {
  70. if (!dict)
  71. return nullptr;
  72. std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
  73. for (id key in dict) {
  74. std::string str_key = base::SysNSStringToUTF8(
  75. [key isKindOfClass:[NSString class]] ? key : [key description]);
  76. id value = [dict objectForKey:key];
  77. if ([value isKindOfClass:[NSString class]]) {
  78. result->SetKey(str_key, base::Value(base::SysNSStringToUTF8(value)));
  79. } else if ([value isKindOfClass:[NSNumber class]]) {
  80. const char* objc_type = [value objCType];
  81. if (strcmp(objc_type, @encode(BOOL)) == 0 ||
  82. strcmp(objc_type, @encode(char)) == 0)
  83. result->SetKey(str_key, base::Value([value boolValue]));
  84. else if (strcmp(objc_type, @encode(double)) == 0 ||
  85. strcmp(objc_type, @encode(float)) == 0)
  86. result->SetKey(str_key, base::Value([value doubleValue]));
  87. else
  88. result->SetKey(str_key, base::Value([value intValue]));
  89. } else if ([value isKindOfClass:[NSArray class]]) {
  90. std::unique_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
  91. if (sub_arr)
  92. result->SetWithoutPathExpansion(str_key, std::move(sub_arr));
  93. else
  94. result->SetWithoutPathExpansion(str_key,
  95. std::make_unique<base::Value>());
  96. } else if ([value isKindOfClass:[NSDictionary class]]) {
  97. std::unique_ptr<base::DictionaryValue> sub_dict =
  98. NSDictionaryToDictionaryValue(value);
  99. if (sub_dict)
  100. result->SetWithoutPathExpansion(str_key, std::move(sub_dict));
  101. else
  102. result->SetWithoutPathExpansion(str_key,
  103. std::make_unique<base::Value>());
  104. } else {
  105. result->SetKey(str_key,
  106. base::Value(base::SysNSStringToUTF8([value description])));
  107. }
  108. }
  109. return result;
  110. }
  111. } // namespace atom