forked from daltoniam/JSONJoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONJoy.m
360 lines (345 loc) · 15.4 KB
/
JSONJoy.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
356
357
358
359
360
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// JSONJoy.m
//
// Created by Dalton Cherry on 10/23/13.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#import "JSONJoy.h"
#import <objc/runtime.h>
#import <objc/message.h>
@interface JSONJoy ()
@property(nonatomic,assign)Class objClass;
@property(nonatomic,strong)NSMutableDictionary* arrayMap;
@property(nonatomic,strong)NSMutableDictionary* propertyClasses;
@end
@implementation JSONJoy
static BOOL isLoose;
static BOOL boxDisabled;
////////////////////////////////////////////////////////////////////////////////////////////////////
-(instancetype)initWithClass:(Class)class
{
if(self = [super init])
{
self.objClass = class;
}
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(void)addArrayClassMap:(NSString*)propertyName class:(Class)classID
{
if(!self.arrayMap)
self.arrayMap = [[NSMutableDictionary alloc] init];
[self.arrayMap setValue:classID forKey:propertyName];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(id)process:(id)object error:(NSError *__autoreleasing *)error
{
if(!object)
return nil;
if([object isKindOfClass:[NSString class]])
{
NSData *data = [object dataUsingEncoding:NSUTF8StringEncoding];
NSError* error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(error)
return nil;
return [self process:json error:&error];
}
else if([object isKindOfClass:[NSData class]])
{
NSError* error = nil;
id json = [NSJSONSerialization JSONObjectWithData:object options:0 error:&error];
if(error)
return nil;
return [self process:json error:&error];
}
else if([object isKindOfClass:[NSArray class]])
{
NSMutableArray* gather = [NSMutableArray arrayWithCapacity:[object count]];
for(id child in object)
[gather addObject:[self process:child error:error]];
return gather;
}
if([object isKindOfClass:[NSDictionary class]])
{
NSDictionary* dict = object;
NSArray* propArray = [self getPropertiesOfClass:self.objClass];
id newObject = nil;
if([[self.objClass class] respondsToSelector:@selector(newModel)])//for coreData support with DCModel
newObject = objc_msgSend([self.objClass class], @selector(newModel));//[[self.objClass class] performSelector:@selector(newModel)];
else
newObject = [[self.objClass alloc] init];
for(NSString* propName in propArray)
{
if([propName isEqualToString:@"objID"]) //special edge case for objective-c using the id keyword
{
if([self assignValue:@"id" propName:propName dict:dict obj:newObject error:error])
continue;
}
if([propName isEqualToString:@"objDescription"]) //special edge case for objective-c using the description method
{
if([self assignValue:@"description" propName:propName dict:dict obj:newObject error:error])
continue;
}
if([self assignValue:propName propName:propName dict:dict obj:newObject error:error])
{
continue;
}
NSString* objCName = [JSONJoy convertToJsonName:propName];
[self assignValue:objCName propName:propName dict:dict obj:newObject error:error];
if(isLoose)
{
NSString *looseName = [propName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[propName substringToIndex:1] lowercaseString]];
[self assignValue:looseName propName:propName dict:dict obj:newObject error:error];
}
if(error && *error)
return nil;
}
return newObject;
}
return nil;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(BOOL)assignValue:(NSString*)key propName:(NSString*)propName dict:(NSDictionary*)dict obj:(id)obj error:(NSError**)error
{
id value = dict[key];
if(value)
{
if([[NSDate class] isSubclassOfClass:self.propertyClasses[propName]])
{
NSDate* date = [self formatDate:value];
if(date)
[obj setValue:date forKey:propName];
return YES;
}
if([value isKindOfClass:[NSDictionary class]] && ![[obj valueForKey:propName] isKindOfClass:[NSDictionary class]])
{
NSError *childError = nil;
id joy = [self.propertyClasses[propName] objectWithJoy:value error:&childError];
if(childError && error)
{
*error = childError;
return NO;
}
if(joy)
{
[obj setValue:joy forKey:propName];
return YES;
}
}
if([value isKindOfClass:[NSArray class]])
{
Class arrayClass = self.arrayMap[propName];
NSArray *array = value;
if(array.count > 0 && [array[0] isKindOfClass:[NSDictionary class]] && arrayClass)
{
NSMutableArray* gather = [NSMutableArray arrayWithCapacity:array.count];
for(NSDictionary* dict in array)
{
NSError *childError = nil;
id joy = [[JSONJoy JSONJoyWithClass:arrayClass] process:dict error:&childError];
if(joy && !childError)
[gather addObject:joy];
if(childError && error)
{
*error = childError;
return NO;
}
}
[obj setValue:gather forKey:propName];
return YES;
}
}
//this is a type check to ensure that the value is same type as expected.
if(self.propertyClasses[propName] && ![value isKindOfClass:self.propertyClasses[propName]] && [NSNull null] != (NSNull*)value)
{
//special handling of BOOL type. It will auto box/convert a BOOL to a number or string if already set.
if([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")] && !boxDisabled)
{
if([self.propertyClasses[propName] isSubclassOfClass:[NSString class]])
{
if([NSNull null] == (NSNull*)value)
[obj setValue:nil forKey:propName];
else
[obj setValue:[NSString stringWithFormat:@"%@",value] forKey:propName];
return YES;
}
else if([self.propertyClasses[propName] isSubclassOfClass:[NSNumber class]])
{
if([NSNull null] == (NSNull*)value)
[obj setValue:nil forKey:propName];
else
[obj setValue:[NSNumber numberWithBool:(BOOL)value] forKey:propName];
return YES;
}
}
NSString* errorString = [NSString stringWithFormat:@"%@. Value: %@ is of class type: %@ expected: %@",
NSLocalizedString(@"Type does not match expected response", nil),propName,NSStringFromClass([value class]),self.propertyClasses[propName]];
if(error)
*error = [JSONJoy errorWithDetail:errorString code:JSONJoyErrorCodeIncorrectType];
return NO;
}
if([NSNull null] == (NSNull*)value)
[obj setValue:nil forKey:propName];
else
[obj setValue:value forKey:propName];
return YES;
}
return NO;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//gets all the properties names of the class
-(NSArray*)getPropertiesOfClass:(Class)objectClass
{
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(objectClass, &outCount);
NSMutableArray *gather = [NSMutableArray arrayWithCapacity:outCount];
for(i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
NSString* propName = [NSString stringWithUTF8String:property_getName(property)];
if([propName isEqualToString:@"debugDescription"] || [propName isEqualToString:@"superclass"]
|| [propName isEqualToString:@"hash"] || [propName isEqualToString:@"description"]) {
continue;
}
const char *type = property_getAttributes(property);
NSString *typeString = [NSString stringWithUTF8String:type];
NSArray *attributes = [typeString componentsSeparatedByString:@","];
NSString *typeAttribute = [attributes objectAtIndex:0];
//may need to support these in the future.
//NSString *propertyType = [typeAttribute substringFromIndex:1];
//const char *rawPropertyType = [propertyType UTF8String];
//if (strcmp(rawPropertyType, @encode(float)) == 0) //it's a float
//else if (strcmp(rawPropertyType, @encode(int)) == 0)//it's an int
//else if (strcmp(rawPropertyType, @encode(id)) == 0) //is id, so any NSObject
if ([typeAttribute hasPrefix:@"T@"] && [typeAttribute length] > 3)
{
NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)]; //turns @"NSDate" into NSDate
Class typeClass = NSClassFromString(typeClassName);
if(!self.propertyClasses)
self.propertyClasses = [[NSMutableDictionary alloc] init];
[self.propertyClasses setObject:typeClass forKey:propName];
}
[gather addObject:propName];
}
free(properties);
if([objectClass superclass] && [objectClass superclass] != [NSObject class])
[gather addObjectsFromArray:[self getPropertiesOfClass:[objectClass superclass]]];
return gather;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//converts some like avatarThumbUrl to avatar_thumb_url.
+(NSString*)convertToJsonName:(NSString*)propName
{
return [self convertToJsonName:propName start:0];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(NSString*)convertToJsonName:(NSString*)propName start:(NSInteger)start
{
NSRange range = [propName rangeOfString:@"[a-z.-][^a-z .-]" options:NSRegularExpressionSearch range:NSMakeRange(start, propName.length-start)];
if(range.location != NSNotFound && range.location < propName.length)
{
unichar c = [propName characterAtIndex:range.location+1];
propName = [propName stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c",c]
withString:[[NSString stringWithFormat:@"_%c",c] lowercaseString]
options:0 range:range]; //NSMakeRange(start, propName.length-start)
return [self convertToJsonName:propName start:range.location+1];
}
return propName;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(NSDate*)formatDate:(NSString*)dateString
{
if([NSNull null] == (NSNull*)dateString)
return nil;
if (dateString.length > 20)
{
dateString = [dateString stringByReplacingOccurrencesOfString:@":"
withString:@""
options:0
range:NSMakeRange(20, dateString.length-20)];
NSRange range = [dateString rangeOfString:@"." options:NSBackwardsSearch];
if(range.location == NSNotFound)
{
range = [dateString rangeOfString:@"+" options:NSBackwardsSearch];
if(range.location == NSNotFound)
range = [dateString rangeOfString:@"-" options:NSBackwardsSearch range:NSMakeRange(19, dateString.length-21)];
if(range.location != NSNotFound)
{
NSString *save = [dateString substringFromIndex:range.location];
dateString = [dateString substringToIndex:range.location];
dateString = [dateString stringByAppendingFormat:@".000%@",save];
}
else
dateString = [dateString stringByAppendingString:@".000+0000"];
}
}
else
dateString = [dateString stringByAppendingString:@".000+0000"];
static NSDateFormatter *dateFormatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
});
return [dateFormatter dateFromString:dateString];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(instancetype)JSONJoyWithClass:(Class)class
{
JSONJoy* mapper = [class jsonMapper];
return mapper;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(NSError*)errorWithDetail:(NSString*)detail code:(JSONJoyErrorCode)code
{
NSMutableDictionary* details = [NSMutableDictionary dictionary];
[details setValue:detail forKey:NSLocalizedDescriptionKey];
return [[NSError alloc] initWithDomain:NSLocalizedString(@"JSONJoy", nil) code:code userInfo:details];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(void)setLoose:(BOOL)loose
{
isLoose = !loose;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(void)setAutoConvertBOOLs:(BOOL)box
{
boxDisabled = !box;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(NSArray*)propertyKeys
{
return [self.propertyClasses allKeys];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//just to silence the warnings
-(void)newModel
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@end
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NSObject (JSONJoy)
////////////////////////////////////////////////////////////////////////////////////////////////////
+(JSONJoy*)jsonMapper
{
return [[JSONJoy alloc] initWithClass:[self class]];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(id)objectWithJoy:(id)jsonObj
{
return [self objectWithJoy:jsonObj error:nil];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(id)objectWithJoy:(id)jsonObj error:(NSError *__autoreleasing *)error
{
JSONJoy* mapper = [self jsonMapper];
return [mapper process:jsonObj error:error];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@end