electron/shell/browser/mac/dict_util.mm

108 lines
3.7 KiB
Text
Raw Normal View History

// 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 "shell/browser/mac/dict_util.h"
#include <string>
#include "base/json/json_writer.h"
2016-05-18 06:49:33 +00:00
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
namespace electron {
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;
}
base::ListValue NSArrayToListValue(NSArray* arr) {
base::ListValue result;
2016-05-18 06:49:33 +00:00
if (!arr)
return result;
2016-05-18 06:49:33 +00:00
for (id value in arr) {
if ([value isKindOfClass:[NSString class]]) {
result.AppendString(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)
result.AppendBoolean([value boolValue]);
2016-05-18 06:49:33 +00:00
else if (strcmp(objc_type, @encode(double)) == 0 ||
strcmp(objc_type, @encode(float)) == 0)
result.AppendDouble([value doubleValue]);
2016-05-18 06:49:33 +00:00
else
result.AppendInteger([value intValue]);
2016-05-18 06:49:33 +00:00
} else if ([value isKindOfClass:[NSArray class]]) {
result.Append(NSArrayToListValue(value));
2016-05-18 06:49:33 +00:00
} else if ([value isKindOfClass:[NSDictionary class]]) {
result.Append(NSDictionaryToDictionaryValue(value));
2016-05-18 06:49:33 +00:00
} else {
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) {
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:[NSDictionary class]])
return nil;
return obj;
}
base::DictionaryValue NSDictionaryToDictionaryValue(NSDictionary* dict) {
base::DictionaryValue result;
2016-05-18 06:49:33 +00:00
if (!dict)
return result;
2016-05-18 05:40:19 +00:00
2016-05-18 06:49:33 +00:00
for (id key in dict) {
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]]) {
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)
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)
result.SetKey(str_key, base::Value([value doubleValue]));
2016-05-18 06:49:33 +00:00
else
result.SetKey(str_key, base::Value([value intValue]));
2016-05-18 06:49:33 +00:00
} else if ([value isKindOfClass:[NSArray class]]) {
result.SetKey(str_key, NSArrayToListValue(value));
2016-05-18 06:49:33 +00:00
} else if ([value isKindOfClass:[NSDictionary class]]) {
result.SetKey(str_key, NSDictionaryToDictionaryValue(value));
2016-05-18 06:49:33 +00:00
} else {
result.SetKey(str_key,
base::Value(base::SysNSStringToUTF8([value description])));
2016-05-18 06:49:33 +00:00
}
}
2016-05-18 06:49:33 +00:00
return result;
}
} // namespace electron