forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONKeyMapper.m
178 lines (137 loc) · 5.04 KB
/
JSONKeyMapper.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
//
// JSONKeyMapper.m
// JSONModel
//
#import "JSONKeyMapper.h"
#import <libkern/OSAtomic.h>
@interface JSONKeyMapper()
@property (nonatomic, strong) NSMutableDictionary *toJSONMap;
@property (nonatomic, assign) OSSpinLock lock;
@end
@implementation JSONKeyMapper
- (instancetype)init
{
if (!(self = [super init]))
return nil;
_toJSONMap = [NSMutableDictionary new];
return self;
}
- (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON
{
return [self initWithModelToJSONBlock:toJSON];
}
- (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON
{
if (!(self = [self init]))
return nil;
__weak JSONKeyMapper *weakSelf = self;
_modelToJSONKeyBlock = ^NSString *(NSString *keyName)
{
__strong JSONKeyMapper *strongSelf = weakSelf;
id cached = strongSelf.toJSONMap[keyName];
if (cached == [NSNull null])
return nil;
if (cached)
return strongSelf.toJSONMap[keyName];
NSString *result = toJSON(keyName);
OSSpinLockLock(&strongSelf->_lock);
strongSelf.toJSONMap[keyName] = result ? result : [NSNull null];
OSSpinLockUnlock(&strongSelf->_lock);
return result;
};
return self;
}
- (instancetype)initWithDictionary:(NSDictionary *)map
{
NSDictionary *toJSON = [JSONKeyMapper swapKeysAndValuesInDictionary:map];
return [self initWithModelToJSONDictionary:toJSON];
}
- (instancetype)initWithModelToJSONDictionary:(NSDictionary *)toJSON
{
if (!(self = [super init]))
return nil;
_modelToJSONKeyBlock = ^NSString *(NSString *keyName)
{
return [toJSON valueForKeyPath:keyName] ?: keyName;
};
return self;
}
+ (NSDictionary *)swapKeysAndValuesInDictionary:(NSDictionary *)dictionary
{
NSArray *keys = dictionary.allKeys;
NSArray *values = [dictionary objectsForKeys:keys notFoundMarker:[NSNull null]];
return [NSDictionary dictionaryWithObjects:keys forKeys:values];
}
- (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing
{
return [self convertValue:value];
}
- (NSString *)convertValue:(NSString *)value
{
return _modelToJSONKeyBlock(value);
}
+ (instancetype)mapperFromUnderscoreCaseToCamelCase
{
return [self mapperForSnakeCase];
}
+ (instancetype)mapperForSnakeCase
{
return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
{
NSMutableString *result = [NSMutableString stringWithString:keyName];
NSRange range;
// handle upper case chars
range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
while (range.location != NSNotFound)
{
NSString *lower = [result substringWithRange:range].lowercaseString;
[result replaceCharactersInRange:range withString:[NSString stringWithFormat:@"_%@", lower]];
range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
}
// handle numbers
range = [result rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
while (range.location != NSNotFound)
{
NSRange end = [result rangeOfString:@"\\D" options:NSRegularExpressionSearch range:NSMakeRange(range.location, result.length - range.location)];
// spans to the end of the key name
if (end.location == NSNotFound)
end = NSMakeRange(result.length, 1);
NSRange replaceRange = NSMakeRange(range.location, end.location - range.location);
NSString *digits = [result substringWithRange:replaceRange];
[result replaceCharactersInRange:replaceRange withString:[NSString stringWithFormat:@"_%@", digits]];
range = [result rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet] options:0 range:NSMakeRange(end.location + 1, result.length - end.location - 1)];
}
return result;
}];
}
+ (instancetype)mapperForTitleCase
{
return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
{
return [keyName stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[keyName substringToIndex:1].uppercaseString];
}];
}
+ (instancetype)mapperFromUpperCaseToLowerCase
{
return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
{
return keyName.uppercaseString;
}];
}
+ (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions
{
NSDictionary *toJSON = [JSONKeyMapper swapKeysAndValuesInDictionary:exceptions];
return [self baseMapper:baseKeyMapper withModelToJSONExceptions:toJSON];
}
+ (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON
{
return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
{
if (!keyName)
return nil;
if (toJSON[keyName])
return toJSON[keyName];
return baseKeyMapper.modelToJSONKeyBlock(keyName);
}];
}
@end