forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONValueTransformer.m
245 lines (202 loc) · 6.26 KB
/
JSONValueTransformer.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
//
// JSONValueTransformer.m
// JSONModel
//
#import "JSONValueTransformer.h"
#pragma mark - functions
extern BOOL isNull(id value)
{
if (!value) return YES;
if ([value isKindOfClass:[NSNull class]]) return YES;
return NO;
}
@implementation JSONValueTransformer
-(id)init
{
self = [super init];
if (self) {
_primitivesNames = @{@"f":@"float", @"i":@"int", @"d":@"double", @"l":@"long", @"c":@"BOOL", @"s":@"short", @"q":@"long",
//and some famous aliases of primitive types
// BOOL is now "B" on iOS __LP64 builds
@"I":@"NSInteger", @"Q":@"NSUInteger", @"B":@"BOOL",
@"@?":@"Block"};
}
return self;
}
+(Class)classByResolvingClusterClasses:(Class)sourceClass
{
//check for all variations of strings
if ([sourceClass isSubclassOfClass:[NSString class]]) {
return [NSString class];
}
//check for all variations of numbers
if ([sourceClass isSubclassOfClass:[NSNumber class]]) {
return [NSNumber class];
}
//check for all variations of dictionaries
if ([sourceClass isSubclassOfClass:[NSArray class]]) {
return [NSArray class];
}
//check for all variations of arrays
if ([sourceClass isSubclassOfClass:[NSDictionary class]]) {
return [NSDictionary class];
}
//check for all variations of dates
if ([sourceClass isSubclassOfClass:[NSDate class]]) {
return [NSDate class];
}
//no cluster parent class found
return sourceClass;
}
#pragma mark - NSMutableString <-> NSString
-(NSMutableString*)NSMutableStringFromNSString:(NSString*)string
{
return [NSMutableString stringWithString:string];
}
#pragma mark - NSMutableArray <-> NSArray
-(NSMutableArray*)NSMutableArrayFromNSArray:(NSArray*)array
{
return [NSMutableArray arrayWithArray:array];
}
#pragma mark - NSMutableDictionary <-> NSDictionary
-(NSMutableDictionary*)NSMutableDictionaryFromNSDictionary:(NSDictionary*)dict
{
return [NSMutableDictionary dictionaryWithDictionary:dict];
}
#pragma mark - NSSet <-> NSArray
-(NSSet*)NSSetFromNSArray:(NSArray*)array
{
return [NSSet setWithArray:array];
}
-(NSMutableSet*)NSMutableSetFromNSArray:(NSArray*)array
{
return [NSMutableSet setWithArray:array];
}
-(id)JSONObjectFromNSSet:(NSSet*)set
{
return [set allObjects];
}
-(id)JSONObjectFromNSMutableSet:(NSMutableSet*)set
{
return [set allObjects];
}
//
// 0 converts to NO, everything else converts to YES
//
#pragma mark - BOOL <-> number/string
-(NSNumber*)BOOLFromNSNumber:(NSNumber*)number
{
if (isNull(number)) return [NSNumber numberWithBool:NO];
return [NSNumber numberWithBool: number.intValue==0?NO:YES];
}
-(NSNumber*)BOOLFromNSString:(NSString*)string
{
if (string != nil &&
([string caseInsensitiveCompare:@"true"] == NSOrderedSame ||
[string caseInsensitiveCompare:@"yes"] == NSOrderedSame)) {
return [NSNumber numberWithBool:YES];
}
return [NSNumber numberWithBool: ([string intValue]==0)?NO:YES];
}
-(NSNumber*)JSONObjectFromBOOL:(NSNumber*)number
{
return [NSNumber numberWithBool: number.intValue==0?NO:YES];
}
#pragma mark - string/number <-> float
-(float)floatFromObject:(id)obj
{
return [obj floatValue];
}
-(float)floatFromNSString:(NSString*)string
{
return [self floatFromObject:string];
}
-(float)floatFromNSNumber:(NSNumber*)number
{
return [self floatFromObject:number];
}
-(NSNumber*)NSNumberFromfloat:(float)f
{
return [NSNumber numberWithFloat:f];
}
#pragma mark - string <-> number
-(NSNumber*)NSNumberFromNSString:(NSString*)string
{
return [NSNumber numberWithDouble:[string doubleValue]];
}
-(NSString*)NSStringFromNSNumber:(NSNumber*)number
{
return [number stringValue];
}
-(NSDecimalNumber*)NSDecimalNumberFromNSString:(NSString*)string
{
return [NSDecimalNumber decimalNumberWithString:string];
}
-(NSString*)NSStringFromNSDecimalNumber:(NSDecimalNumber*)number
{
return [number stringValue];
}
#pragma mark - string <-> url
-(NSURL*)NSURLFromNSString:(NSString*)string
{
// do not change this behavior - there are other ways of overriding it
// see: https://github.com/jsonmodel/jsonmodel/pull/119
return [NSURL URLWithString:string];
}
-(NSString*)JSONObjectFromNSURL:(NSURL*)url
{
return [url absoluteString];
}
#pragma mark - string <-> date
-(NSDateFormatter*)importDateFormatter
{
static dispatch_once_t onceInput;
static NSDateFormatter* inputDateFormatter;
dispatch_once(&onceInput, ^{
inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[inputDateFormatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZZZ"];
});
return inputDateFormatter;
}
-(NSDate*)__NSDateFromNSString:(NSString*)string
{
string = [string stringByReplacingOccurrencesOfString:@":" withString:@""]; // this is such an ugly code, is this the only way?
return [self.importDateFormatter dateFromString: string];
}
-(NSString*)__JSONObjectFromNSDate:(NSDate*)date
{
static dispatch_once_t onceOutput;
static NSDateFormatter *outputDateFormatter;
dispatch_once(&onceOutput, ^{
outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
});
return [outputDateFormatter stringFromDate:date];
}
#pragma mark - number <-> date
- (NSDate*)NSDateFromNSNumber:(NSNumber*)number
{
return [NSDate dateWithTimeIntervalSince1970:number.doubleValue];
}
#pragma mark - string <-> NSTimeZone
- (NSTimeZone *)NSTimeZoneFromNSString:(NSString *)string {
return [NSTimeZone timeZoneWithName:string];
}
- (id)JSONObjectFromNSTimeZone:(NSTimeZone *)timeZone {
return [timeZone name];
}
#pragma mark - hidden transform for empty dictionaries
//https://github.com/jsonmodel/jsonmodel/issues/163
-(NSDictionary*)__NSDictionaryFromNSArray:(NSArray*)array
{
if (array.count==0) return @{};
return (id)array;
}
-(NSMutableDictionary*)__NSMutableDictionaryFromNSArray:(NSArray*)array
{
if (array.count==0) return [[self __NSDictionaryFromNSArray:array] mutableCopy];
return (id)array;
}
@end