-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMBJSONModel.m
310 lines (267 loc) · 10.2 KB
/
MBJSONModel.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
//
// MBJSONModel.m
//
// Created by Mo Bitar on 11/27/13.
// Copyright (c) 2013 Mo Bitar. All rights reserved.
//
#import "MBJSONModel.h"
#import <objc/runtime.h>
NSString *MBSetSelectorForKey(NSString *key)
{
NSString *firstLetter = [[key substringToIndex:1] capitalizedString];
NSString *capitlizedPropertyName = [firstLetter stringByAppendingString:[key substringFromIndex:1]];
NSString *selectorName = [[@"set" stringByAppendingString:capitlizedPropertyName] stringByAppendingString:@":"];
return selectorName;
}
@implementation MBJSONModel
- (NSString *)description
{
return [[self dictionaryFromObjectProperties] description];
}
- (instancetype)initWithJSONDictionary:(NSDictionary *)dictionary
{
if(self = [super init])
{
[self setValuesFromJSONDictionary:dictionary ignoreNil:YES];
}
return self;
}
+ (NSArray *)arrayOfObjectPropertyNamesForClass:(Class)aClass
{
unsigned int count;
objc_property_t *propertyList = class_copyPropertyList(aClass, &count);
NSMutableArray *properties = [NSMutableArray new];
for (int i = 0; i < count; i++) {
objc_property_t property = propertyList[i];
const char *propertyName = property_getName(property);
[properties addObject:[NSString stringWithUTF8String:propertyName]];
}
free(propertyList);
return [properties copy];
}
- (NSArray *)extendedArrayOfProperties
{
NSMutableArray *properties = [NSMutableArray new];
Class aClass = self.class;
while (aClass != [MBJSONModel class]) {
[properties addObjectsFromArray:[MBJSONModel arrayOfObjectPropertyNamesForClass:aClass]];
aClass = [aClass superclass];
}
return [properties copy];
}
- (NSDictionary *)dictionaryFromObjectProperties
{
NSMutableDictionary *valuesDictionary = [[NSMutableDictionary alloc] init];
NSArray *properties = [self extendedArrayOfProperties];
for (NSString *propertyName in properties) {
if([self respondsToSelector:NSSelectorFromString(propertyName)]) {
id value = [self valueForKey:propertyName];
if (value) {
[valuesDictionary setObject:value forKey:propertyName];
}
}
}
return valuesDictionary;
}
- (NSDictionary *)JSONDictionaryRepresentation
{
NSMutableDictionary *valuesDictionary = [[self dictionaryFromObjectProperties] mutableCopy];
NSDictionary *mappingDict = [self.class JSONKeyTranslationDictionary];
for(NSString *key in [valuesDictionary mutableCopy]) {
// transform existing values if transformer exists
MBValueTransformer *transformer = [self.class valueTransformerForKey:key];
if(transformer.reverseBlock) {
valuesDictionary[key] = [transformer reverseTransformedValue:valuesDictionary[key]];
}
if([mappingDict allKeysForObject:key].count == 0) {
// property not found in translation dict, remove it from JSON dict
[valuesDictionary removeObjectForKey:key];
} else if([[mappingDict objectForKey:key] isKindOfClass:[NSDictionary class]]) {
// no support currently for reverse coding a relationship class, will skip
[valuesDictionary removeObjectForKey:key];
}
}
NSDictionary *JSONDictionary = [self dictionaryByConvertingKeysToJSONKeysFromDictionary:valuesDictionary];
return JSONDictionary;
}
- (NSData *)JSONDataRepresentation
{
NSDictionary *JSONDictionary = [self JSONDictionaryRepresentation];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:JSONDictionary options:0 error:&error];
if (error) {
NSLog(@"Error serializing JSON: %@", error);
}
return data;
}
- (NSString *)JSONKeyForPropertyName:(NSString *)propertyName
{
NSDictionary *mapping = [self.class JSONKeyTranslationDictionary];
NSString *JSONKey = [[mapping allKeysForObject:propertyName] lastObject];
if(JSONKey.length)
{
return JSONKey;
}
return propertyName;
}
- (NSDictionary *)dictionaryByConvertingKeysToJSONKeysFromDictionary:(NSDictionary *)dictionary
{
NSMutableDictionary *JSONDictionary = [NSMutableDictionary new];
for(NSString *key in dictionary)
{
NSString *JSONKey = [self JSONKeyForPropertyName:key];
[JSONDictionary setObject:dictionary[key] forKey:JSONKey];
}
return JSONDictionary;
}
- (void)setValuesFromJSONDictionary:(NSDictionary *)dictionary
{
[self setValuesFromJSONDictionary:dictionary ignoreNil:NO];
}
- (void)setValuesFromJSONDictionary:(NSDictionary *)dictionary ignoreNil:(BOOL)ignoreNil
{
NSDictionary *mapping = [[self class] JSONKeyTranslationDictionary];
for (NSString *JSONKey in mapping.allKeys) {
id mappedKey = [mapping objectForKey:JSONKey];
id value = nil;
if([JSONKey rangeOfString:@"."].location != NSNotFound) {
// trace keypath
NSScanner *scanner = [NSScanner scannerWithString:JSONKey];
BOOL hasResult = YES;
while(!scanner.isAtEnd && hasResult) {
NSString *currentKeyPath;
hasResult = [scanner scanUpToString:@"." intoString:¤tKeyPath];
NSInteger targetLocation = scanner.scanLocation + 1;
if(targetLocation < scanner.string.length) {
[scanner setScanLocation:targetLocation];
}
if(currentKeyPath.length) {
if(value) {
value = value[currentKeyPath];
} else {
value = dictionary[currentKeyPath];
}
}
}
} else {
value = [dictionary objectForKey:JSONKey];
}
if((!value || [value isEqual:[NSNull null]]) && ignoreNil) {
continue;
}
if(mappedKey) {
if([mappedKey isKindOfClass:[NSDictionary class]]) {
BOOL isDate = [mappedKey[@"isDate"] boolValue];
if(isDate) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:mappedKey[@"format"]];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[self setValue:[formatter dateFromString:value] forKeyPath:mappedKey[@"property"]];
} else {
// relationship
BOOL isArray = [mappedKey[@"isArray"] boolValue];
Class relationshipClass = NSClassFromString(mappedKey[@"class"]);
if(relationshipClass) {
if(isArray) {
[self setValue:[relationshipClass arrayOfModelsFromJSONDictionaryArray:value] forKey:mappedKey[@"relationship"]];
} else {
[self setValue:[relationshipClass modelFromJSONDictionary:value] forKey:mappedKey[@"relationship"]];
}
}
}
} else {
MBValueTransformer *transformer = nil;
if([mappedKey isKindOfClass:[NSString class]]) {
transformer = [self.class valueTransformerForKey:mappedKey];
}
if(!transformer) {
NSString *selectorString = [mappedKey stringByAppendingString:@"JSONValueTransformer"];
if([self respondsToSelector:NSSelectorFromString(selectorString)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
transformer = [self performSelector:NSSelectorFromString(selectorString)];
#pragma clang diagnostic pop
}
}
if(transformer) {
value = [transformer transformedValue:value];
if(value) {
[self setValue:value forKey:mappedKey];
}
}
else {
if([value isEqual:[NSNull null]] == NO) {
[self setValue:value forKey:mappedKey];
}
}
}
}
}
}
- (void)setNilValueForKey:(NSString *)key
{
@try {
[self setValue:@(0) forKey:key];
}
@catch (NSException *exception) {}
}
- (void)updateWithValuesOfModel:(MBJSONModel *)sourceModel
{
NSAssert([self class] == [sourceModel class], @"Cannot copy properties of models of different classes");
NSArray *properties = [sourceModel extendedArrayOfProperties];
[self updateWithValuesOfModel:sourceModel forKeys:properties];
}
- (void)updateWithValuesOfModel:(MBJSONModel *)sourceModel forKeys:(NSArray *)keys
{
for(NSString *propertyName in keys) {
NSString *selectorName = MBSetSelectorForKey(propertyName);
if(![self respondsToSelector:NSSelectorFromString(selectorName)]) {
continue;
}
id value = [sourceModel valueForKey:propertyName];
if([value conformsToProtocol:@protocol(NSCopying)]) {
[self setValue:[value copy] forKey:propertyName];
} else {
[self setValue:value forKey:propertyName];
}
}
}
+ (MBValueTransformer *)valueTransformerForKey:(NSString *)key
{
return nil;
}
+ (NSDictionary *)JSONKeyTranslationDictionary
{
return nil;
}
+ (NSDictionary *)reverseKeyValuePairingOfDictionary:(NSDictionary *)dictionary
{
NSMutableDictionary *reversedDictionary = [NSMutableDictionary new];
for(NSString *key in dictionary) {
[reversedDictionary setObject:key forKey:dictionary[key]];
}
return reversedDictionary;
}
+ (instancetype)modelFromJSONDictionary:(NSDictionary *)dictionary
{
return [[self alloc] initWithJSONDictionary:dictionary];
}
+ (NSArray *)arrayOfModelsFromJSONDictionaryArray:(NSArray *)dictionaries
{
NSMutableArray *models = [NSMutableArray new];
for(NSDictionary *jsonDic in dictionaries)
{
[models addObject:[self modelFromJSONDictionary:jsonDic]];
}
return models;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
MBJSONModel *model = [[[self class] alloc] init];
if(model) {
[model updateWithValuesOfModel:self];
}
return model;
}
@end