2016-05-05 07:26:44 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/mac/dict_util.h"
|
|
|
|
|
|
|
|
#include "base/json/json_writer.h"
|
2016-05-18 06:49:33 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2016-05-05 07:26:44 +00:00
|
|
|
#include "base/values.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2016-11-28 23:08:22 +00:00
|
|
|
NSArray* ListValueToNSArray(const base::ListValue& value) {
|
|
|
|
std::string json;
|
|
|
|
if (!base::JSONWriter::Write(value, &json))
|
|
|
|
return nil;
|
|
|
|
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
|
|
|
|
id obj =
|
|
|
|
[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
|
|
|
if (![obj isKindOfClass:[NSArray class]])
|
|
|
|
return nil;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
|
2016-05-18 06:49:33 +00:00
|
|
|
if (!arr)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-06-18 07:32:55 +00:00
|
|
|
auto result = std::make_unique<base::ListValue>();
|
2016-05-18 06:49:33 +00:00
|
|
|
for (id value in arr) {
|
|
|
|
if ([value isKindOfClass:[NSString class]]) {
|
|
|
|
result->AppendString(base::SysNSStringToUTF8(value));
|
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
const char* objc_type = [value objCType];
|
|
|
|
if (strcmp(objc_type, @encode(BOOL)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(char)) == 0)
|
|
|
|
result->AppendBoolean([value boolValue]);
|
|
|
|
else if (strcmp(objc_type, @encode(double)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(float)) == 0)
|
|
|
|
result->AppendDouble([value doubleValue]);
|
|
|
|
else
|
|
|
|
result->AppendInteger([value intValue]);
|
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
|
2016-05-18 06:49:33 +00:00
|
|
|
if (sub_arr)
|
|
|
|
result->Append(std::move(sub_arr));
|
|
|
|
else
|
2018-04-12 12:48:32 +00:00
|
|
|
result->Append(std::make_unique<base::Value>());
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> sub_dict =
|
2016-05-18 06:49:33 +00:00
|
|
|
NSDictionaryToDictionaryValue(value);
|
|
|
|
if (sub_dict)
|
|
|
|
result->Append(std::move(sub_dict));
|
|
|
|
else
|
2018-04-12 12:48:32 +00:00
|
|
|
result->Append(std::make_unique<base::Value>());
|
2016-05-18 06:49:33 +00:00
|
|
|
} else {
|
2016-05-18 07:01:08 +00:00
|
|
|
result->AppendString(base::SysNSStringToUTF8([value description]));
|
2016-05-18 06:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
NSDictionary* DictionaryValueToNSDictionary(
|
|
|
|
const base::DictionaryValue& value) {
|
2016-05-05 07:26:44 +00:00
|
|
|
std::string json;
|
|
|
|
if (!base::JSONWriter::Write(value, &json))
|
|
|
|
return nil;
|
|
|
|
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
|
2018-04-20 18:47:04 +00:00
|
|
|
id obj =
|
|
|
|
[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
2016-05-05 07:26:44 +00:00
|
|
|
if (![obj isKindOfClass:[NSDictionary class]])
|
|
|
|
return nil;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
|
2016-05-05 07:26:44 +00:00
|
|
|
NSDictionary* dict) {
|
2016-05-18 06:49:33 +00:00
|
|
|
if (!dict)
|
2016-05-18 05:40:19 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2018-06-18 07:32:55 +00:00
|
|
|
auto result = std::make_unique<base::DictionaryValue>();
|
2016-05-18 06:49:33 +00:00
|
|
|
for (id key in dict) {
|
2016-05-18 07:01:08 +00:00
|
|
|
std::string str_key = base::SysNSStringToUTF8(
|
|
|
|
[key isKindOfClass:[NSString class]] ? key : [key description]);
|
2016-05-18 06:49:33 +00:00
|
|
|
|
|
|
|
id value = [dict objectForKey:key];
|
|
|
|
if ([value isKindOfClass:[NSString class]]) {
|
2017-11-27 06:45:01 +00:00
|
|
|
result->SetKey(str_key, base::Value(base::SysNSStringToUTF8(value)));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
const char* objc_type = [value objCType];
|
|
|
|
if (strcmp(objc_type, @encode(BOOL)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(char)) == 0)
|
2017-11-27 06:45:01 +00:00
|
|
|
result->SetKey(str_key, base::Value([value boolValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
else if (strcmp(objc_type, @encode(double)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(float)) == 0)
|
2017-11-27 06:45:01 +00:00
|
|
|
result->SetKey(str_key, base::Value([value doubleValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
else
|
2017-11-27 06:45:01 +00:00
|
|
|
result->SetKey(str_key, base::Value([value intValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
|
2016-05-18 06:49:33 +00:00
|
|
|
if (sub_arr)
|
|
|
|
result->SetWithoutPathExpansion(str_key, std::move(sub_arr));
|
|
|
|
else
|
|
|
|
result->SetWithoutPathExpansion(str_key,
|
2018-04-12 12:48:32 +00:00
|
|
|
std::make_unique<base::Value>());
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> sub_dict =
|
2016-05-18 06:49:33 +00:00
|
|
|
NSDictionaryToDictionaryValue(value);
|
|
|
|
if (sub_dict)
|
|
|
|
result->SetWithoutPathExpansion(str_key, std::move(sub_dict));
|
|
|
|
else
|
|
|
|
result->SetWithoutPathExpansion(str_key,
|
2018-04-12 12:48:32 +00:00
|
|
|
std::make_unique<base::Value>());
|
2016-05-18 06:49:33 +00:00
|
|
|
} else {
|
2017-11-27 06:45:01 +00:00
|
|
|
result->SetKey(str_key,
|
|
|
|
base::Value(base::SysNSStringToUTF8([value description])));
|
2016-05-18 06:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 07:26:44 +00:00
|
|
|
|
2016-05-18 06:49:33 +00:00
|
|
|
return result;
|
2016-05-05 07:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|