-
Notifications
You must be signed in to change notification settings - Fork 5
/
jsonToOCModel.js
256 lines (213 loc) · 12.2 KB
/
jsonToOCModel.js
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
//大写转换
let uppercaseFirst = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let shortClassName = false;
//获取数组泛型字符串和最内层对象
let getGenericStringAndInnerObjWithArr = (arr, className) => {
let count = 0;
let getInnerObj = (arr) => {
if (Array.isArray(arr)) {
let first = arr[0];
count++;
return getInnerObj(first);
}
else {
return arr;
}
}
let inner = getInnerObj(arr);
let innerClass = className;
if (typeof inner === 'string') {
innerClass = 'NSString';
}
else if (typeof inner === 'number') {
innerClass = 'NSNumber /*double*/';
}
else if (typeof inner === 'boolean') {
innerClass = 'NSNumber /*bool*/';
}
let genericStrings = [innerClass];
while (count) {
genericStrings.unshift('NSArray <');
genericStrings.push(' *>');
count--;
}
let genericString = genericStrings.join('');
return { genericString, inner };
}
//获取数组循环语句和最内层对象
let getIterateLinesAndInnerObjWithArr = (arr, className, key, NSStringKey) => {
let count = 0;
let getInnerObj = (arr) => {
if (Array.isArray(arr)) {
let first = arr[0];
count++;
return getInnerObj(first);
}
else {
return arr;
}
}
let inner = getInnerObj(arr);
let initWithDictionaryLines = [];
let dictionaryRepresentationLines = [];
if (typeof inner === 'object') {
count--;
initWithDictionaryLines.push(`for (NSDictionary *dict in array) {\r\n${className} *${key} = [${className} modelObjectWithDictionary:dict];\r\n[array${count} addObject:${key}];\r\n}\r\n`);
dictionaryRepresentationLines.push(`for (NSObject *obj in array${count}) {\r\nif([obj respondsToSelector:@selector(dictionaryRepresentation)]) {\r\n// This class is a model object\r\n[mArray${count} addObject:[obj performSelector:@selector(dictionaryRepresentation)]];\r\n}\r\n}`);
while (count) {
initWithDictionaryLines.unshift(`for (NSObject *obj in array) {\r\nif ([obj isKindOfClass:[NSArray class]]) {\r\nNSArray *array = (NSArray *)obj;\r\nNSMutableArray *array${count} = [NSMutableArray new];`);
initWithDictionaryLines.push(`[array${count - 1} addObject:array${count}];\r\n}\r\n}`);
dictionaryRepresentationLines.unshift(`for (NSArray *array${count} in array${count - 1}) {\r\nNSMutableArray *mArray${count} = [NSMutableArray new];`);
dictionaryRepresentationLines.push(`[mArray${count - 1} addObject:array${count}];\r\n}`);
count--;
}
initWithDictionaryLines.unshift(`NSObject *${key} = [self objectOrNilForKey:${NSStringKey} fromDictionary:dict];\r\nif ([${key} isKindOfClass:[NSArray class]]) {\r\nNSArray *array = (NSArray *)${key};\r\nNSMutableArray *array${count} = [NSMutableArray new];`);
initWithDictionaryLines.push(`self.${key} = [NSArray arrayWithArray:array${count}];\r\n}`);
dictionaryRepresentationLines.unshift(`{\r\nNSArray *array0 = self.${key};\r\nNSMutableArray *mArray0 = [NSMutableArray new];`);
dictionaryRepresentationLines.push(`[mutableDict setValue:[NSArray arrayWithArray:mArray0] forKey:${NSStringKey}];\r\n}`);
} else {
initWithDictionaryLines.push(`self.${key} = [self objectOrNilForKey:${NSStringKey} fromDictionary:dict];`);
dictionaryRepresentationLines.push(``);
}
let initWithDictionary = initWithDictionaryLines.join('\r\n');
let dictionaryRepresentation = dictionaryRepresentationLines.join('\r\n');
return { initWithDictionary, dictionaryRepresentation, inner };
}
//对象转Objective-C头文件
let objToOCHeader = (jsonObj, prefix, baseClass) => {
if (Array.isArray(jsonObj)) {
return objToOCHeader(jsonObj[0], prefix, baseClass);
}
let lines = [];
let className = `${prefix}${uppercaseFirst(baseClass)}`;
lines.push(`@interface ${className} : NSObject <NSCoding, NSCopying>\r\n\r\n`);
for (let key in jsonObj) {
if (jsonObj.hasOwnProperty(key)) {
let element = jsonObj[key];
if (typeof element === 'string') {
lines.push(`@property (nonatomic, strong) NSString *${key};\r\n`);
}
else if (typeof element === 'number') {
lines.push(`@property (nonatomic, assign) double ${key};\r\n`);
}
else if (typeof element === 'boolean') {
lines.push(`@property (nonatomic, assign) BOOL ${key};\r\n`);
}
else if (typeof element === 'object') {
if (shortClassName) {
className = prefix;
}
let subClassName = `${className}${uppercaseFirst(key)}`;
if (Array.isArray(element)) {
let { genericString, inner } = getGenericStringAndInnerObjWithArr(element, subClassName);
lines.push(`@property (nonatomic, strong) ${genericString} *${key};\r\n`);
if (typeof inner === 'object') {
lines.unshift(objToOCHeader(element, className, key));
}
}
else {
lines.push(`@property (nonatomic, strong) ${subClassName} *${key};\r\n`);
lines.unshift(objToOCHeader(element, className, key));
}
}
}
}
lines.push(`\r\n@end\r\n\r\n`);
let linesOutput = lines.join('');
return linesOutput;
}
//对象转Objective-C实现文件
let objToOCImplementation = (jsonObj, prefix, baseClass) => {
if (Array.isArray(jsonObj)) {
return objToOCImplementation(jsonObj[0], prefix, baseClass);
}
let lines = [];
let NSStringKeyLines = [];
let initWithDictionaryLines = [];
let dictionaryRepresentationLines = [];
let initWithCoderLines = [];
let encodeWithCoderLines = [];
let copyWithZoneLines = [];
let className = `${prefix}${uppercaseFirst(baseClass)}`;
lines.push(`@interface ${className} ()\r\n- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;\r\n@end\r\n\r\n@implementation ${className}\r\n`);
lines.push(`+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict{\r\nreturn [[self alloc] initWithDictionary:dict];\r\n}`);
initWithDictionaryLines.push(`- (instancetype)initWithDictionary:(NSDictionary *)dict{\r\nself = [super init];\r\nif(self && [dict isKindOfClass:[NSDictionary class]]) {\r\n`);
dictionaryRepresentationLines.push(`- (NSDictionary *)dictionaryRepresentation{\r\nNSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];`);
initWithCoderLines.push(`- (id)initWithCoder:(NSCoder *)aDecoder{\r\nself = [super init];`);
encodeWithCoderLines.push(`- (void)encodeWithCoder:(NSCoder *)aCoder{`);
copyWithZoneLines.push(`- (id)copyWithZone:(NSZone *)zone{\r\n${className} *copy = [[${className} alloc] init];\r\nif (copy) {`);
for (let key in jsonObj) {
if (jsonObj.hasOwnProperty(key)) {
let element = jsonObj[key];
const NSStringKey = `k${className}${uppercaseFirst(key)}`;
if (typeof element === 'string') {
NSStringKeyLines.push(`NSString *const ${NSStringKey} = @"${key}";`);
initWithDictionaryLines.push(`self.${key} = [self objectOrNilForKey:${NSStringKey} fromDictionary:dict];`);
dictionaryRepresentationLines.push(`[mutableDict setValue:self.${key} forKey:${NSStringKey}];`);
initWithCoderLines.push(`self.${key} = [aDecoder decodeObjectForKey:${NSStringKey}];`);
encodeWithCoderLines.push(`[aCoder encodeObject:_${key} forKey:${NSStringKey}];`);
copyWithZoneLines.push(`copy.${key} = [self.${key} copyWithZone:zone];`);
}
else if (typeof element === 'number') {
NSStringKeyLines.push(`NSString *const ${NSStringKey} = @"${key}";`);
initWithDictionaryLines.push(`self.${key} = [[self objectOrNilForKey:${NSStringKey} fromDictionary:dict] doubleValue];`);
dictionaryRepresentationLines.push(`[mutableDict setValue:[NSNumber numberWithDouble:self.${key}] forKey:${NSStringKey}];`);
initWithCoderLines.push(`self.${key} = [aDecoder decodeDoubleForKey:${NSStringKey}];`);
encodeWithCoderLines.push(`[aCoder encodeDouble:_${key} forKey:${NSStringKey}];`);
copyWithZoneLines.push(`copy.${key} = self.${key};`);
}
else if (typeof element === 'boolean') {
NSStringKeyLines.push(`NSString *const ${NSStringKey} = @"${key}";`);
initWithDictionaryLines.push(`self.${key} = [[self objectOrNilForKey:${NSStringKey} fromDictionary:dict] boolValue];`);
dictionaryRepresentationLines.push(`[mutableDict setValue:[NSNumber numberWithBool:self.${key}] forKey:${NSStringKey}];`);
initWithCoderLines.push(`self.${key} = [aDecoder decodeBoolForKey:${NSStringKey}];`);
encodeWithCoderLines.push(`[aCoder encodeBool:_${key} forKey:${NSStringKey}];`);
copyWithZoneLines.push(`copy.${key} = self.${key};`);
}
else if (typeof element === 'object') {
if (shortClassName) {
className = prefix;
}
let subClassName = `${className}${uppercaseFirst(key)}`;
if (Array.isArray(element)) {
let { initWithDictionary, dictionaryRepresentation, inner } = getIterateLinesAndInnerObjWithArr(element, subClassName, key, NSStringKey);
NSStringKeyLines.push(`NSString *const ${NSStringKey} = @"${key}";`);
initWithDictionaryLines.push(initWithDictionary);
dictionaryRepresentationLines.push(dictionaryRepresentation);
if (typeof inner === 'object') {
lines.unshift(objToOCImplementation(element, className, key));
}
initWithCoderLines.push(`self.${key} = [aDecoder decodeObjectForKey:${NSStringKey}];`);
encodeWithCoderLines.push(`[aCoder encodeObject:_${key} forKey:${NSStringKey}];`);
copyWithZoneLines.push(`copy.${key} = [self.${key} copyWithZone:zone];`);
}
else {
NSStringKeyLines.push(`NSString *const ${NSStringKey} = @"${key}";`);
initWithDictionaryLines.push(`self.${key} = [${subClassName} modelObjectWithDictionary:[dict objectForKey:${NSStringKey}]];`);
dictionaryRepresentationLines.push(`[mutableDict setValue:[self.${key} dictionaryRepresentation] forKey:${NSStringKey}];`);
initWithCoderLines.push(`self.${key} = [aDecoder decodeObjectForKey:${NSStringKey}];`);
encodeWithCoderLines.push(`[aCoder encodeObject:_${key} forKey:${NSStringKey}];`);
copyWithZoneLines.push(`copy.${key} = [self.${key} copyWithZone:zone];`);
lines.unshift(objToOCImplementation(element, className, key));
}
}
}
}
initWithDictionaryLines.push(`\r\n}\r\nreturn self;\r\n}`);
dictionaryRepresentationLines.push(`return [NSDictionary dictionaryWithDictionary:mutableDict];\r\n}`);
initWithCoderLines.push(`return self;\r\n}`);
encodeWithCoderLines.push(`}`);
copyWithZoneLines.push(`}\r\nreturn copy;\r\n}`);
lines.unshift(NSStringKeyLines.join('\r\n'));
lines.push(initWithDictionaryLines.join('\r\n'));
lines.push(dictionaryRepresentationLines.join('\r\n'));
lines.push(`\r\n- (NSString *)description{\r\nreturn [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];\r\n}\r\n\r\n#pragma mark - Helper Method\r\n- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict{\r\nid object = [dict objectForKey:aKey];\r\nreturn [object isEqual:[NSNull null]] ? nil : object;\r\n}\r\n\r\n#pragma mark - NSCoding Methods\r\n`);
lines.push(initWithCoderLines.join('\r\n'));
lines.push(encodeWithCoderLines.join('\r\n'));
lines.push(copyWithZoneLines.join('\r\n'));
lines.push(`\r\n@end\r\n\r\n`);
let linesOutput = lines.join('\r\n');
return linesOutput;
}