-
Notifications
You must be signed in to change notification settings - Fork 0
/
v.m
102 lines (88 loc) · 3.45 KB
/
v.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
- (instancetype)init {
[NSException raise:NSInternalInconsistencyException format:@"Cannot instantiate helper class"];
return nil;
}
+ (NSDictionary *)dictionaryFrom:(NSObject<A0DictionarySerialization> *)object {
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([object class], &count);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
NSArray *propertiesToSkip = @[
NSStringFromSelector(@selector(debugDescription)),
NSStringFromSelector(@selector(description)),
NSStringFromSelector(@selector(hash)),
NSStringFromSelector(@selector(superclass)),
];
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
if ([propertiesToSkip containsObject:key]) {
continue;
}
id value = [object valueForKey:key];
id entryValue = [self entryValueFromObject:value];
if (entryValue) {
[dictionary setObject:entryValue forKey:key];
}
}
free(properties);
return dictionary;
}
+ (id)entryValueFromObject:(id)object {
id value;
if ([object isKindOfClass:NSURL.class]) {
value = [self stringFromURL:object];
}
if ([object isKindOfClass:NSDate.class]) {
value = [self timestampFromDate:object];
}
if ([object conformsToProtocol:@protocol(A0DictionarySerialization)]) {
value = [object asDictionary];
}
if ([object isKindOfClass:NSArray.class]) {
value = [self arrayFromArray:object];
}
if ([object isKindOfClass:NSDictionary.class]) {
value = [self dictionaryFromDictionary:object];
}
if ([object isKindOfClass:NSString.class] ||
[object isKindOfClass:NSNumber.class]) {
value = object;
}
return value;
}
+ (NSString *)stringFromURL:(NSURL *)url {
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
return url.absoluteString;
}
+ (NSNumber *)timestampFromDate:(NSDate *)date {
return @(date.timeIntervalSince1970);
}
+ (NSArray *)arrayFromArray:(NSArray *)array {
NSMutableArray *tranformed = [@[] mutableCopy];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id value = [self entryValueFromObject:obj];
if (value) {
[tranformed addObject:value];
}
}];
return [NSArray arrayWithArray:tranformed];
}
+ (NSDictionary *)dictionaryFromDictionary:(NSDictionary *)dictionary {
NSMutableDictionary *transformed = [@{} mutableCopy];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id value;
if ([key isKindOfClass:NSString.class]) {
value = [self entryValueFromObject:obj];
}
if (value) {
transformed[key] = value;
}
}];
return [NSDictionary dictionaryWithDictionary:transformed];
}
@end