root / Classes / NSObject+NSCoding.m @ e06c24cf
History | View | Annotate | Download (9.1 kB)
1 |
// |
---|---|
2 |
// NSObject+NSCoding.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Michael Mayo on 3/4/11. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "NSObject+NSCoding.h" |
10 |
#import <objc/runtime.h> |
11 |
//#import <objc/objc-class.h> |
12 |
|
13 |
|
14 |
@implementation NSObject (NSCoding) |
15 |
|
16 |
- (NSMutableDictionary *)propertiesForClass:(Class)klass { |
17 |
|
18 |
NSMutableDictionary *results = [[[NSMutableDictionary alloc] init] autorelease]; |
19 |
|
20 |
unsigned int outCount, i; |
21 |
objc_property_t *properties = class_copyPropertyList(klass, &outCount); |
22 |
for(i = 0; i < outCount; i++) { |
23 |
objc_property_t property = properties[i]; |
24 |
|
25 |
NSString *pname = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; |
26 |
NSString *pattrs = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; |
27 |
|
28 |
pattrs = [[pattrs componentsSeparatedByString:@","] objectAtIndex:0]; |
29 |
pattrs = [pattrs substringFromIndex:1]; |
30 |
|
31 |
[results setObject:pattrs forKey:pname]; |
32 |
} |
33 |
free(properties); |
34 |
|
35 |
if ([klass superclass] != [NSObject class]) { |
36 |
[results addEntriesFromDictionary:[self propertiesForClass:[klass superclass]]]; |
37 |
} |
38 |
|
39 |
return results; |
40 |
} |
41 |
|
42 |
- (NSDictionary *)properties { |
43 |
return [self propertiesForClass:[self class]]; |
44 |
} |
45 |
|
46 |
- (void)autoEncodeWithCoder:(NSCoder *)coder { |
47 |
NSDictionary *properties = [self properties]; |
48 |
for (NSString *key in properties) { |
49 |
NSString *type = [properties objectForKey:key]; |
50 |
id value; |
51 |
unsigned long long ullValue; |
52 |
BOOL boolValue; |
53 |
float floatValue; |
54 |
double doubleValue; |
55 |
NSInteger intValue; |
56 |
unsigned long ulValue; |
57 |
NSString *className; |
58 |
|
59 |
NSMethodSignature *signature = [self methodSignatureForSelector:NSSelectorFromString(key)]; |
60 |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; |
61 |
[invocation setSelector:NSSelectorFromString(key)]; |
62 |
[invocation setTarget:self]; |
63 |
|
64 |
switch ([type characterAtIndex:0]) { |
65 |
case '@': // object |
66 |
if ([[type componentsSeparatedByString:@"\""] count] > 1) { |
67 |
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1]; |
68 |
value = [self performSelector:NSSelectorFromString(key)]; |
69 |
|
70 |
// only decode if the property conforms to NSCoding |
71 |
if (class_conformsToProtocol(NSClassFromString(className), @protocol(NSCoding))) { |
72 |
[coder encodeObject:value forKey:key]; |
73 |
} |
74 |
} |
75 |
break; |
76 |
case 'c': // bool |
77 |
[invocation invoke]; |
78 |
[invocation getReturnValue:&boolValue]; |
79 |
[coder encodeObject:[NSNumber numberWithBool:boolValue] forKey:key]; |
80 |
break; |
81 |
case 'f': // float |
82 |
[invocation invoke]; |
83 |
[invocation getReturnValue:&floatValue]; |
84 |
[coder encodeObject:[NSNumber numberWithFloat:floatValue] forKey:key]; |
85 |
break; |
86 |
case 'd': // double |
87 |
[invocation invoke]; |
88 |
[invocation getReturnValue:&doubleValue]; |
89 |
[coder encodeObject:[NSNumber numberWithDouble:doubleValue] forKey:key]; |
90 |
break; |
91 |
case 'i': // int |
92 |
[invocation invoke]; |
93 |
[invocation getReturnValue:&intValue]; |
94 |
[coder encodeObject:[NSNumber numberWithInt:intValue] forKey:key]; |
95 |
break; |
96 |
case 'L': // unsigned long |
97 |
[invocation invoke]; |
98 |
[invocation getReturnValue:&ulValue]; |
99 |
[coder encodeObject:[NSNumber numberWithUnsignedLong:ulValue] forKey:key]; |
100 |
break; |
101 |
case 'Q': // unsigned long long |
102 |
[invocation invoke]; |
103 |
[invocation getReturnValue:&ullValue]; |
104 |
[coder encodeObject:[NSNumber numberWithUnsignedLongLong:ullValue] forKey:key]; |
105 |
break; |
106 |
default: |
107 |
break; |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
- (void)autoDecode:(NSCoder *)coder { |
113 |
NSDictionary *properties = [self properties]; |
114 |
for (NSString *key in properties) { |
115 |
NSString *type = [properties objectForKey:key]; |
116 |
id value; |
117 |
NSNumber *number; |
118 |
unsigned int addr; |
119 |
NSInteger i; |
120 |
CGFloat f; |
121 |
BOOL b; |
122 |
double d; |
123 |
unsigned long ul; |
124 |
unsigned long long ull; |
125 |
Ivar ivar; |
126 |
double *varIndex; |
127 |
NSString *className; |
128 |
|
129 |
switch ([type characterAtIndex:0]) { |
130 |
case '@': // object |
131 |
if ([[type componentsSeparatedByString:@"\""] count] > 1) { |
132 |
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1]; |
133 |
// only decode if the property conforms to NSCoding |
134 |
if (class_conformsToProtocol(NSClassFromString(className), @protocol(NSCoding))) { |
135 |
value = [[coder decodeObjectForKey:key] retain]; |
136 |
addr = (NSInteger)&value; |
137 |
object_setInstanceVariable(self, [key UTF8String], *(id**)addr); |
138 |
[value release]; |
139 |
} |
140 |
} |
141 |
break; |
142 |
case 'c': // bool |
143 |
number = [coder decodeObjectForKey:key]; |
144 |
b = [number boolValue]; |
145 |
addr = (NSInteger)&b; |
146 |
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr); |
147 |
break; |
148 |
case 'f': // float |
149 |
number = [coder decodeObjectForKey:key]; |
150 |
f = [number floatValue]; |
151 |
addr = (NSInteger)&f; |
152 |
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr); |
153 |
break; |
154 |
case 'd': // double |
155 |
number = [coder decodeObjectForKey:key]; |
156 |
d = [number doubleValue]; |
157 |
if ((ivar = class_getInstanceVariable([self class], [key UTF8String]))) { |
158 |
varIndex = (double *)(void **)((char *)self + ivar_getOffset(ivar)); |
159 |
*varIndex = d; |
160 |
} |
161 |
break; |
162 |
case 'i': // int |
163 |
number = [coder decodeObjectForKey:key]; |
164 |
i = [number intValue]; |
165 |
addr = (NSInteger)&i; |
166 |
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr); |
167 |
break; |
168 |
case 'L': // unsigned long |
169 |
number = [coder decodeObjectForKey:key]; |
170 |
ul = [number unsignedLongValue]; |
171 |
addr = (NSInteger)&ul; |
172 |
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr); |
173 |
break; |
174 |
case 'Q': // unsigned long long |
175 |
number = [coder decodeObjectForKey:key]; |
176 |
ull = [number unsignedLongLongValue]; |
177 |
addr = (NSInteger)&ull; |
178 |
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr); |
179 |
break; |
180 |
default: |
181 |
break; |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
- (NSInteger)intForKey:(NSString *)key inDict:(NSDictionary *)dict { |
187 |
NSInteger result = 0; |
188 |
if ([dict objectForKey:key] != [NSNull null]) { |
189 |
result = [((NSNumber *)[dict objectForKey:key]) intValue]; |
190 |
} |
191 |
return result; |
192 |
} |
193 |
|
194 |
- (void)autoParse:(NSObject **)object fromJSONDict:(NSDictionary *)dict { |
195 |
NSDictionary *properties = [*object properties]; |
196 |
for (NSString *key in properties) { |
197 |
|
198 |
int i; |
199 |
|
200 |
NSString *type = [properties objectForKey:key]; |
201 |
SEL setter = NSSelectorFromString([NSString stringWithFormat:@"set%@%@:", [[key uppercaseString] substringToIndex:1], [key substringFromIndex:1]]); |
202 |
|
203 |
NSMethodSignature *signature = [*object methodSignatureForSelector:setter]; |
204 |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; |
205 |
[invocation setSelector:setter]; |
206 |
[invocation setTarget:*object]; |
207 |
|
208 |
switch ([type characterAtIndex:0]) { |
209 |
case '@': // object |
210 |
if ([[type componentsSeparatedByString:@"\""] count] > 1) { |
211 |
///NSString *className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1]; |
212 |
[*object performSelector:setter withObject:[dict objectForKey:key]]; |
213 |
} |
214 |
break; |
215 |
case 'c': // bool |
216 |
break; |
217 |
case 'f': // float |
218 |
break; |
219 |
case 'd': // double |
220 |
break; |
221 |
case 'i': // int |
222 |
if ([dict objectForKey:key]) { |
223 |
i = [*object intForKey:key inDict:dict]; |
224 |
[invocation setArgument:&i atIndex:2]; |
225 |
[invocation invoke]; |
226 |
} |
227 |
break; |
228 |
case 'L': // unsigned long |
229 |
break; |
230 |
case 'Q': // unsigned long long |
231 |
break; |
232 |
default: |
233 |
break; |
234 |
} |
235 |
} |
236 |
} |
237 |
|
238 |
@end |