-
Notifications
You must be signed in to change notification settings - Fork 27
/
JAGPropertyConverter.m
355 lines (327 loc) · 13.4 KB
/
JAGPropertyConverter.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// JAGPropertyConverter.m
// JAGPropertyConverter
//
// Created by James Gill on 2/13/12.
//
// Copyright (c) 2012 James A. Gill
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "JAGPropertyConverter.h"
#import "JAGPropertyFinder.h"
#import "JAGProperty.h"
@interface JAGPropertyConverter ()
- (id) composeCollection: (id) collection withTargetClass: (Class) targetClass;
/*
* This converts a property to a PropertyModel-friendly form.
* Dictionaries that can be detected as a PropertyModel subclass
* are converted to that subclass and returned.
* Other collections of objects are turned as those same collections,
* but with their elements/values converted recursively.
* Other objects, if they match the target class, are returned
* unmodified, while "Base" PropertyList objects
* (NSNull, NSString, NSNumber, NSDate, NSData, and NSValue) are
* returned either unmodified, or if there is a 'convertable'
* targetClass, converted to that.
*/
- (id) composeModelFromObject: (id) object withTargetClass: (Class) targetClass;
- (BOOL) shouldConvertClass: (Class) aClass;
@end
@implementation JAGPropertyConverter
@synthesize outputType = _outputType;
@synthesize identifyDict = _identifyDict;
@synthesize classesToConvert = _classesToConvert;
@synthesize convertToDate = _convertToDate;
@synthesize convertFromDate = _convertFromDate;
@synthesize numberFormatter = _numberFormatter;
@synthesize shouldConvertWeakProperties = _shouldConvertWeakProperties;
#pragma mark - Lifecycle
+ (JAGPropertyConverter *) converterWithOutputType: (JAGOutputType) outputType {
return [[JAGPropertyConverter alloc] initWithOutputType:outputType];
}
- (id) initWithOutputType: (JAGOutputType) outputType {
self = [super init];
if (self) {
self.outputType = outputType;
self.identifyDict = nil;
self.convertToDate = nil;
self.convertFromDate = nil;
self.classesToConvert = [NSMutableSet set];
self.shouldConvertWeakProperties = NO;
}
return self;
}
- (id) init {
return [self initWithOutputType:kJAGFullOutput];
}
#pragma mark - Convert To Dictionary
- (BOOL) shouldConvertClass: (Class) aClass {
for (Class class in self.classesToConvert) {
if ([aClass isSubclassOfClass:class]) {
return YES;
}
}
return NO;
}
- (id) decomposeObject: (id) object {
if (!object) {
return nil;
} else if ([object isKindOfClass: [NSNull class]]
|| [object isKindOfClass: [NSString class]]) {
//These objects are fine for all output types
return object;
} else if ([object isKindOfClass: [NSNumber class]]) {
if ( self.outputType == kJAGJSONOutput ) {
if ( isfinite([object doubleValue]) ) {
return object;
} else {
//JSON cannot handle +-infinity or NaN
return nil;
}
} else {
return object;
}
} else if ([object isKindOfClass: [NSDate class]]) {
if ( self.outputType == kJAGFullOutput
|| self.outputType == kJAGPropertyListOutput ) {
return object;
} else if (self.convertFromDate) {
return self.convertFromDate(object);
} else {
//Object is not safe for JSON. Removing.
return nil;
}
} else if ([object isKindOfClass: [NSData class]]) {
//These objects are fine for PropertyLists, but not JSON
if ( self.outputType == kJAGFullOutput
|| self.outputType == kJAGPropertyListOutput ) {
return object;
} else {
//Object is not safe for JSON. Removing.
return nil;
}
} else if ( [object isKindOfClass: [NSValue class]] ) {
//These objects are only ok for FullOutput
if ( self.outputType == kJAGFullOutput ) {
return object;
} else {
//Object is not safe for JSON or PropertyLists. Removing.
return nil;
}
} else if ( [object isKindOfClass: [NSURL class]] ) {
//These objects are only ok for FullOutput
if ( self.outputType == kJAGFullOutput ) {
return object;
} else {
//Convert them to strings.
return [object absoluteString];
}
} else if ([object isKindOfClass: [NSArray class]]) {
NSMutableArray *array = [NSMutableArray array];
for (id obj in object) {
id value = [self decomposeObject:obj];
if (value) {
[array addObject: value];
} else {
NSLog(@"Object %@ can't be converted to properties.", obj);
}
}
return array;
} else if ([object isKindOfClass: [NSSet class]]) {
id collection;
if (self.outputType == kJAGJSONOutput || self.outputType == kJAGPropertyListOutput) {
//JSON and PropertyLists only support arrays.
collection = [NSMutableArray array];
} else {
collection = [NSMutableSet set];
}
for (id obj in object) {
id value = [self decomposeObject:obj];
if (value) {
[collection addObject: value];
} else {
NSLog(@"Object %@ can't be converted to properties.", obj);
}
}
return collection;
} else if ([object isKindOfClass: [NSDictionary class]]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (id key in object) {
if ( self.outputType == kJAGJSONOutput && ![key isKindOfClass:[NSString class]] ) {
NSLog(@"JSON dictionaries must have string keys, skipping key %@", key);
continue;
}
id value = [self decomposeObject:[object objectForKey: key]];
if (value) {
[dict setObject: [self decomposeObject: value] forKey: key];
} else {
NSLog(@"Unable to convert %@ to properties.", [object objectForKey: key]);
}
}
return dict;
} else if ([self shouldConvertClass:[object class]]) {
return [self convertToDictionary:object];
} else {
if ( self.outputType == kJAGFullOutput ) {
return object;
} else {
NSLog(@"Object %@ is not safe for JSON or PropertyLists. Removing.", [object class]);
return nil;
}
}
}
- (NSDictionary*) convertToDictionary: (id) model {
if (!model) return nil;
NSMutableDictionary *values = [NSMutableDictionary dictionary];
NSArray* properties = [JAGPropertyFinder propertiesForClass:[model class]];
NSString* propertyName;
for (JAGProperty *property in properties) {
propertyName = [property name];
if (!self.shouldConvertWeakProperties && [property isWeak]) {
continue;
}
SEL getter = [property getter];
if (![model respondsToSelector:getter]) {
//Found property without a valid getter. Skipping.
continue;
}
//TODO: Should use the getter for this? Harder to handle non-objects.
id object = [model valueForKey:propertyName];
[values setValue:[self decomposeObject: object] forKey:propertyName];
}
return values;
}
#pragma mark - Convert From Dictionary
- (id) composeCollection: (id) collection withTargetClass: (Class) targetClass {
if (!targetClass) {
targetClass = [collection class];
}
id mutableCollection;
//FIXME: If targetClass is a proper subclass, the property may not be settable to mutableCollection.
if ([targetClass isSubclassOfClass:[NSArray class]]) {
mutableCollection = [[NSMutableArray alloc] init];
} else if ([targetClass isSubclassOfClass:[NSSet class]]) {
mutableCollection = [[NSMutableSet alloc] init];
} else {
//TODO: Catch mutisets and the like.
NSLog(@"Unable to convert %@ to collection type %@", [collection class], targetClass);
return nil;
}
for (id elt in collection) {
id value = [self composeModelFromObject:elt];
if (value) {
[mutableCollection addObject: value];
} else {
NSLog(@"Object %@ can't be converted to properties.", [elt class]);
}
}
return mutableCollection;
}
- (id) composeModelFromObject: (id) object {
return [self composeModelFromObject:object withTargetClass: nil];
}
- (id) composeModelFromObject: (id) object withTargetClass: (Class) targetClass {
if (!object) {
return nil;
} else if ([object isKindOfClass: [NSArray class]]
|| [object isKindOfClass: [NSSet class]]) {
return [self composeCollection:object withTargetClass:targetClass];
} else if ([object isKindOfClass: [NSDictionary class]]) {
//Is this a PropertyModel in disguise?
Class modelClass = nil;
if (self.identifyDict) {
modelClass = self.identifyDict(object);
}
if (modelClass) {
id model = [[modelClass alloc] init];
[self setPropertiesOf:model fromDictionary:object];
return model;
} else if (targetClass && [self shouldConvertClass:targetClass]) {
//Try to coerce it into targetClass.
id model = [[targetClass alloc] init];
[self setPropertiesOf:model fromDictionary:object];
return model;
} else {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (id key in object) {
[dict setValue: [self composeModelFromObject: [object valueForKey: key]]
forKey: key];
}
return dict;
}
} else if (targetClass && [object isKindOfClass: targetClass]) {
//TODO: If there are other collections that aren't subclasses of NSSet, NSArray, or NSDictionary,
//this won't convert their elements/values.
return object;
} else if (targetClass
&& [targetClass isSubclassOfClass:[NSDate class]]
&& self.convertToDate) {
// NSLog(@"Found prop %@ for NSDate targetClass. Converting.", prop);
return self.convertToDate(object);
} else if ( targetClass
&& [targetClass isSubclassOfClass:[NSURL class]]
&& [object isKindOfClass:[NSString class]]
)
{
return [NSURL URLWithString:object];
} else if ( self.numberFormatter
&& targetClass
&& [targetClass isSubclassOfClass:[NSNumber class]]
&& [object isKindOfClass:[NSString class]])
{
return [self.numberFormatter numberFromString:object];
} else if ([object isKindOfClass: [NSNull class]]
|| [object isKindOfClass: [NSString class]]
|| [object isKindOfClass: [NSNumber class]]
|| [object isKindOfClass: [NSDate class]]
|| [object isKindOfClass: [NSData class]]
|| [object isKindOfClass: [NSValue class]]
) {
return object;
}
//TODO: Don't know what to do with this! If we are using fullOutputType, we might be
//getting other NSObject types, which we should be able to handle.
NSLog(@"Unable to convert value %@ to an object property, returning nil.", [object class]);
return nil;
}
- (void) setPropertiesOf: (id) object fromDictionary: (NSDictionary*) dictionary {
JAGProperty *property;
for (NSString *key in dictionary) {
property = [JAGPropertyFinder propertyForName: key inClass:[object class] ];
if (!property || [property isReadOnly]) continue;
id value = [dictionary valueForKey:key];
//See if we should convert an NSString to an NSNumber
if (self.numberFormatter && property.isNumber && [value isKindOfClass:[NSString class]])
{
//Handle NSNumber propertyClasses in the compose function
value = [self.numberFormatter numberFromString:value];
}
if ([property isObject]) {
Class propertyClass = [property propertyClass];
value = [self composeModelFromObject: value withTargetClass:propertyClass];
}
if ([property canAcceptValue:value]) {
[object setValue:value forKey:key];
} else {
NSLog(@"Unable to set value of class %@ into property %@ of typeEncoding %@",
[value class], [property name], [property typeEncoding]);
}
}
}
@end