-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeaModel.m
More file actions
executable file
·214 lines (187 loc) · 5.27 KB
/
Copy pathGeaModel.m
File metadata and controls
executable file
·214 lines (187 loc) · 5.27 KB
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
//
// GeaModel.m
// GeaBookDesigner
//
// Created by Johnny Cubehead on 12-9-6.
//
//
#import "GeaModel.h"
static NSString const *CLASS_KEY = @"__class";
@implementation GeaModel
+ (id)modelWithJson:(NSDictionary *)json {
GeaModel *model = [[self alloc] init];
[model loadFromJson:json];
#if !__has_feature(objc_arc)
return [model autorelease];
#else
return model;
#endif
}
+(NSArray *)modelWithJsonArray:(NSArray *)array {
NSMutableArray *list = [NSMutableArray array];
if ([array isKindOfClass:[NSArray class]]&&array.count){
for (NSDictionary *dict in array){
[list addObject:[self modelWithJson:dict]];
}
}
return [NSArray arrayWithArray:list];
}
- (id)init {
self = [super init];
if (!self) {
return nil;
}
[self setup];
return self;
}
- (void)setup {
}
- (NSSet *)propertiesForJson {
return [NSSet set];
}
-(NSDictionary *) propertiesMap{
return nil;
}
-(NSDictionary *) propertyClassMap{
return nil;
}
- (void)loadProperty:(NSString *)prop with:(NSString *)p fromJson:(NSDictionary *)json {
if (![json.allKeys containsObject:p]) {
return;
}
id value = [json objectForKey:p];
if ([value isKindOfClass:[NSArray class]]) {
BOOL flag = NO;
NSMutableArray *list = [NSMutableArray array];
for (id v in value) {
if ([v isKindOfClass:[NSDictionary class]]) {
NSString *c = [v objectForKey:CLASS_KEY];
if (!c){
c = [[self propertyClassMap] objectForKey:prop];
}
if (c){
Class clazz = NSClassFromString(c);
if (clazz) {
GeaModel *obj = [[clazz alloc] init];
[obj loadFromJson:v];
[list addObject:obj];
#if !__has_feature(objc_arc)
[obj release];
#endif
}
} else {
[list addObject:v];
}
} else {
flag = YES;
break;
}
}
if (flag) {
[self setValue:value forKey:prop];
} else {
[self setValue:list forKey:prop];
}
} else if ([value isKindOfClass:[NSDictionary class]]) {
NSString *c = [value objectForKey:CLASS_KEY];
if (!c){
c = [[self propertyClassMap] objectForKey:prop];
}
Class clazz = NSClassFromString(c);
if (clazz) {
GeaModel *obj = [[clazz alloc] init];
[obj loadFromJson:value];
[self setValue:obj forKey:prop];
#if !__has_feature(objc_arc)
[obj release];
#endif
}
} else {
if (![value isKindOfClass:[NSNull class]]) {
[self setValue:value forKey:prop];
}
}
}
- (void)writeProperty:(NSString *)prop toJson:(NSMutableDictionary *)json {
id value = [self valueForKey:prop];
if (!value)
return;
NSString *key = prop;
NSDictionary *map = self.propertiesMap;
if ([map.allKeys containsObject:prop]){
NSString *s = [map objectForKey:prop];
if (s.length){
key = s;
}
}
if ([value isKindOfClass:[NSArray class]]) {
BOOL flag = NO;
NSMutableArray *list = [NSMutableArray array];
for (id v in value) {
if (![v isKindOfClass:[GeaModel class]]) {
flag = YES;
break;
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
GeaModel *model = v;
[model writeToJson:dict];
[list addObject:dict];
}
if (flag) {
[json setObject:value forKey:key];
} else {
[json setObject:list forKey:key];
}
} else if ([value isKindOfClass:[GeaModel class]]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
GeaModel *model = value;
[model writeToJson:dict];
[json setObject:dict forKey:key];
} else {
[json setObject:value forKey:key];
}
}
- (void)loadFromJson:(NSDictionary *)json {
NSSet *set = [self propertiesForJson];
NSDictionary *map = [self propertiesMap];
NSString *prop = nil;
for (prop in set) {
NSString *p = [map objectForKey:prop];
if (!p){
p = prop;
}
[self loadProperty:prop with:p fromJson:json];
}
[self afterLoaded];
}
- (void)writeToJson:(NSMutableDictionary *)json {
[self beforeWrite];
NSSet *set = [self propertiesForJson];
NSString *prop = nil;
for (prop in set) {
[self writeProperty:prop toJson:json];
}
// [json setObject:NSStringFromClass([self class]) forKey:CLASS_KEY];
}
- (void)beforeWrite {
}
- (void)afterLoaded {
}
-(NSDictionary*) json{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[self writeToJson:dict];
return [NSDictionary dictionaryWithDictionary:dict];
}
@end
@implementation NSArray (GeaModel)
-(NSString*) geaJSON{
NSMutableArray *list = [NSMutableArray array];
for (id obj in self) {
if ([obj isKindOfClass:[GeaModel class]]) {
GeaModel *model = obj;
[list addObject:[model json]];
}
}
return [list JSONString];
}
@end