Do not convert NSDictionary to JSON
This commit is contained in:
parent
240c346d02
commit
68cf267b1d
1 changed files with 87 additions and 13 deletions
|
@ -4,13 +4,55 @@
|
||||||
|
|
||||||
#include "atom/browser/mac/dict_util.h"
|
#include "atom/browser/mac/dict_util.h"
|
||||||
|
|
||||||
#include "base/mac/scoped_nsobject.h"
|
|
||||||
#include "base/json/json_reader.h"
|
|
||||||
#include "base/json/json_writer.h"
|
#include "base/json/json_writer.h"
|
||||||
|
#include "base/strings/sys_string_conversions.h"
|
||||||
#include "base/values.h"
|
#include "base/values.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
scoped_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
|
||||||
|
if (!arr)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
scoped_ptr<base::ListValue> result(new base::ListValue);
|
||||||
|
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]]) {
|
||||||
|
scoped_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
|
||||||
|
if (sub_arr)
|
||||||
|
result->Append(std::move(sub_arr));
|
||||||
|
else
|
||||||
|
result->Append(base::Value::CreateNullValue());
|
||||||
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
||||||
|
scoped_ptr<base::DictionaryValue> sub_dict =
|
||||||
|
NSDictionaryToDictionaryValue(value);
|
||||||
|
if (sub_dict)
|
||||||
|
result->Append(std::move(sub_dict));
|
||||||
|
else
|
||||||
|
result->Append(base::Value::CreateNullValue());
|
||||||
|
} else {
|
||||||
|
result->Append(base::Value::CreateNullValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) {
|
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) {
|
||||||
std::string json;
|
std::string json;
|
||||||
if (!base::JSONWriter::Write(value, &json))
|
if (!base::JSONWriter::Write(value, &json))
|
||||||
|
@ -26,20 +68,52 @@ NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value)
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
|
scoped_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
|
||||||
NSDictionary* dict) {
|
NSDictionary* dict) {
|
||||||
if (!dict || ![NSJSONSerialization isValidJSONObject:dict])
|
if (!dict)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
NSData* data = [NSJSONSerialization dataWithJSONObject:dict
|
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
|
||||||
options:0
|
for (id key in dict) {
|
||||||
error:nil];
|
std::string str_key;
|
||||||
if (!data)
|
if ([key isKindOfClass:[NSString class]])
|
||||||
return nullptr;
|
str_key = base::SysNSStringToUTF8(key);
|
||||||
|
else
|
||||||
|
str_key = base::SysNSStringToUTF8([NSString stringWithFormat:@"%p", key]);
|
||||||
|
|
||||||
base::scoped_nsobject<NSString> json =
|
id value = [dict objectForKey:key];
|
||||||
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
if ([value isKindOfClass:[NSString class]]) {
|
||||||
scoped_ptr<base::Value> value =
|
result->SetStringWithoutPathExpansion(
|
||||||
base::JSONReader::Read([json UTF8String]);
|
str_key, base::SysNSStringToUTF8(value));
|
||||||
return base::DictionaryValue::From(std::move(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->SetBooleanWithoutPathExpansion(str_key, [value boolValue]);
|
||||||
|
else if (strcmp(objc_type, @encode(double)) == 0 ||
|
||||||
|
strcmp(objc_type, @encode(float)) == 0)
|
||||||
|
result->SetDoubleWithoutPathExpansion(str_key, [value doubleValue]);
|
||||||
|
else
|
||||||
|
result->SetIntegerWithoutPathExpansion(str_key, [value intValue]);
|
||||||
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
||||||
|
scoped_ptr<base::ListValue> sub_arr = NSArrayToListValue(value);
|
||||||
|
if (sub_arr)
|
||||||
|
result->SetWithoutPathExpansion(str_key, std::move(sub_arr));
|
||||||
|
else
|
||||||
|
result->SetWithoutPathExpansion(str_key,
|
||||||
|
base::Value::CreateNullValue());
|
||||||
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
||||||
|
scoped_ptr<base::DictionaryValue> sub_dict =
|
||||||
|
NSDictionaryToDictionaryValue(value);
|
||||||
|
if (sub_dict)
|
||||||
|
result->SetWithoutPathExpansion(str_key, std::move(sub_dict));
|
||||||
|
else
|
||||||
|
result->SetWithoutPathExpansion(str_key,
|
||||||
|
base::Value::CreateNullValue());
|
||||||
|
} else {
|
||||||
|
result->SetWithoutPathExpansion(str_key, base::Value::CreateNullValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
Loading…
Reference in a new issue