-
Notifications
You must be signed in to change notification settings - Fork 17
/
MPWObjectBuilder.m
294 lines (252 loc) · 10.1 KB
/
MPWObjectBuilder.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
//
// MPWObjectBuilder.m
// ObjectiveXML
//
// Created by Marcel Weiher on 27.06.19.
//
#import "MPWObjectBuilder.h"
#import <MPWWriteStream.h>
#define ARRAYTOS (NSMutableArray*)(tos->container)
#define DICTTOS (NSMutableDictionary*)(tos->container)
@implementation MPWObjectBuilder
-(MPWSmallStringTable*)accessorsForClass:(Class)theClass
{
NSArray *ivars=[theClass allIvarNames];
if ( [[ivars lastObject] hasPrefix:@"_"]) {
ivars=(NSArray*)[[ivars collect] substringFromIndex:1];
}
NSMutableArray *accessors=[NSMutableArray arrayWithCapacity:ivars.count];
for (NSString *ivar in ivars) {
MPWPropertyBinding *accessor=[MPWPropertyBinding valueForName:ivar];
[accessor bindToClass:theClass];
[accessors addObject:accessor];
}
MPWSmallStringTable *table=[[[MPWSmallStringTable alloc] initWithKeys:ivars values:accessors] autorelease];
return table;
}
-(void)setupAccessors:(Class)theClass
{
self.accessorTable=[self accessorsForClass:theClass];
}
-(MPWSmallStringTable*)accessorTablesForClassDict:(NSDictionary*)classesByKey
{
NSMutableArray *accessorTables=[NSMutableArray array];
NSArray *keys=[classesByKey allKeys];
for ( NSArray *classKey in keys) {
Class theClass=classesByKey[classKey];
MPWSmallStringTable *accessorTable=[self accessorsForClass:theClass];
[accessorTables addObject:accessorTable];
}
MPWSmallStringTable *tables=[[[MPWSmallStringTable alloc] initWithKeys:keys values:accessorTables] autorelease];
return tables;
}
-(MPWSmallStringTable*)cachesForClassesByKey:(NSDictionary*)classesByKey
{
NSMutableArray *caches=[NSMutableArray array];
NSArray *keys=[classesByKey allKeys];
for ( NSArray *classKey in keys) {
Class theClass=classesByKey[classKey];
MPWObjectCache *cache=[MPWObjectCache cacheWithCapacity:20 class:theClass];
[caches addObject:cache];
}
MPWSmallStringTable *cacheMap=[[[MPWSmallStringTable alloc] initWithKeys:keys values:caches] autorelease];
return cacheMap;
}
-(void)setClassesForKeys:(NSDictionary*)classesByKey
{
self.classTable=classesByKey;
self.accessorTablesByKey=[self accessorTablesForClassDict:classesByKey];
self.cachesByKey=[self cachesForClassesByKey:classesByKey];
}
-(instancetype)initWithClass:(Class)theClass
{
self=[super init];
self.cache=[MPWObjectCache cacheWithCapacity:20 class:theClass];
[self setupAccessors:theClass];
[self.cache setUnsafeFastAlloc:YES];
return self;
}
-(void)beginArray
{
NSString *theKey=self.key;
[super beginArray];
if ( theKey ) {
tos->lookup=[self.accessorTablesByKey objectForKey:theKey];
tos->cache=[self.cachesByKey objectForKey:theKey];
}
}
-(void)beginDictionary
{
id container=nil;
MPWSmallStringTable *table=nil;
MPWObjectCache *cacheToUse=nil;
if (self.key ) {
table=[self.accessorTablesByKey objectForKey:self.key];
cacheToUse = [self.cachesByKey objectForKey:self.key];
}
if (!table) {
if ( tos-containerStack > 0 && (table=tos->lookup)) {
cacheToUse = tos->cache;
}
if (!table) {
table=self.accessorTable;
}
}
if (!cacheToUse) {
cacheToUse=_cache;
}
if (!container) {
container=GETOBJECT(cacheToUse);
}
[self pushContainer:container ];
tos->lookup=table;
}
-(void)writeString:(NSString*)aString
{
if ( key ) {
MPWPropertyBinding *accesssor=[tos->lookup objectForKey:key];
if (accesssor) {
[accesssor setValue:aString forTarget:tos->container];
} else if ( [tos->container respondsToSelector:@selector(setObject:forKey:)]) {
[tos->container setObject:aString forKey:key];
}
key=NULL;
} else {
[self pushObject:aString];
}
}
-(void)writeNumber:(NSString*)number
{
if ( key ) {
MPWPropertyBinding *accesssor=[tos->lookup objectForKey:key];
[accesssor setValue:number forTarget:tos->container];
key=nil;
} else {
[self pushObject:number];
}
}
-(void)writeInteger:(long)number
{
if ( key ) {
MPWPropertyBinding *accesssor=[tos->lookup objectForKey:key];
if (accesssor) {
[accesssor setIntValue:number forTarget:tos->container];
} else if ( [tos->container respondsToSelector:@selector(setObject:forKey:)]) {
[tos->container setObject:@(number) forKey:key];
}
key=nil;
} else {
[self pushObject:@(number)];
}
}
-(void)writeObject:anObject forKey:aKey
{
MPWPropertyBinding *accesssor=[tos->lookup objectForKey:aKey];
if (accesssor) {
[accesssor setValue:anObject forTarget:tos->container];
} else if ( [tos->container respondsToSelector:@selector(setObject:forKey:)]) {
[tos->container setObject:anObject forKey:aKey];
}
}
-(void)dealloc
{
[_cache release];
[super dealloc];
}
@end
@interface MPWJSONFlatDecodeTestClass : NSObject {}
@property (assign) long a,b;
@property (strong) id c;
@end
@implementation MPWJSONFlatDecodeTestClass
@end
@interface MPWJSONNestedDecodeTestClass : NSObject {}
@property (strong) MPWJSONFlatDecodeTestClass* firstNested;
@property (strong) NSArray<MPWJSONNestedDecodeTestClass*> *secondNested;
@property (assign) long nestedInt;
@property (strong) NSString* nestedString;
@end
@implementation MPWJSONNestedDecodeTestClass
@end
#import "MPWMASONParser.h"
@implementation MPWObjectBuilder(testing)
+(void)testDecodeFlatClassArray
{
NSData *jsonArrayOfFlatObjects=[@"[ { \"a\": 2, \"b\": 3, \"c\": \"First\" } , { \"a\": 20, \"b\": 10, \"c\": \"Second\"} ]" asData];
MPWObjectBuilder *builder=[[[self alloc] initWithClass:[MPWJSONFlatDecodeTestClass class]] autorelease];
MPWMASONParser *parser=[[[MPWMASONParser alloc] initWithBuilder:builder] autorelease];
NSArray *result = [parser parsedData:jsonArrayOfFlatObjects];
INTEXPECT(result.count, 2, @"parsed array count");
MPWJSONFlatDecodeTestClass *first=result.firstObject;
INTEXPECT(first.a, 2, @"first.a");
INTEXPECT(first.b, 3, @"first.b");
IDEXPECT(first.c, @"First", @"first.c");
MPWJSONFlatDecodeTestClass *last=result.lastObject;
INTEXPECT(last.a, 20, @"last.a");
INTEXPECT(last.b, 10, @"last.b");
IDEXPECT(last.c, @"Second", @"last.c");
}
+(void)testDecodeNestedObject
{
NSData *jsonNestedObjects=[@"[ { \"nestedInt\": 7, \"nestedString\": \"FirstNested\" \"firstNested\": { \"a\": 17, \"b\": 23, \"c\": \"SubObject\" } } ]" asData];
MPWObjectBuilder *builder=[[[self alloc] initWithClass:[MPWJSONNestedDecodeTestClass class]] autorelease];
[builder setClassesForKeys:@{ @"firstNested": [MPWJSONFlatDecodeTestClass class] }];
MPWMASONParser *parser=[[[MPWMASONParser alloc] initWithBuilder:builder] autorelease];
NSArray *result = [parser parsedData:jsonNestedObjects];
INTEXPECT(result.count, 1, @"parsed array count");
MPWJSONNestedDecodeTestClass *first=result.firstObject;
INTEXPECT(first.nestedInt, 7, @"first.nestedInt");
IDEXPECT(first.nestedString, @"FirstNested", @"first.nestedString");
MPWJSONFlatDecodeTestClass *firstSubObject=first.firstNested;
EXPECTNOTNIL(firstSubObject, @"there should be a subobject");
INTEXPECT(firstSubObject.a, 17, @"firstSubObject.a");
INTEXPECT(firstSubObject.b, 23, @"firstSubObject.b");
IDEXPECT(firstSubObject.c, @"SubObject", @"firstSubObject.c");
}
+(void)testDecodeNestedArrayWithObjects
{
NSData *jsonNestedObjects=[@"[ { \"nestedInt\": 7, \"nestedString\": \"FirstNested\" \"secondNested\": [{ \"a\": 17, \"b\": 23, \"c\": \"SubObject\" } ], \"firstNested\": { \"a\": 14, \"b\": 41, \"c\": \"LaterSubObject\" } } ]" asData];
MPWObjectBuilder *builder=[[[self alloc] initWithClass:[MPWJSONNestedDecodeTestClass class]] autorelease];
[builder setClassesForKeys:@{ @"secondNested": [MPWJSONFlatDecodeTestClass class] }];
MPWMASONParser *parser=[[[MPWMASONParser alloc] initWithBuilder:builder] autorelease];
NSArray *result = [parser parsedData:jsonNestedObjects];
INTEXPECT(result.count, 1, @"parsed array count");
MPWJSONNestedDecodeTestClass *first=result.firstObject;
INTEXPECT(first.nestedInt, 7, @"first.nestedInt");
IDEXPECT(first.nestedString, @"FirstNested", @"first.nestedString");
NSArray *nestedArray=first.secondNested;
INTEXPECT(nestedArray.count, 1, @"elements in nested array");
MPWJSONFlatDecodeTestClass *secondSubObject=nestedArray.firstObject;
INTEXPECT(secondSubObject.a, 17, @"secondSubObject.a");
INTEXPECT(secondSubObject.b, 23, @"secondSubObject.b");
IDEXPECT(secondSubObject.c, @"SubObject", @"secondSubObject.c");
}
+(void)testCanEmbedDictionaries
{
NSData *jsonNestedObjects=[@"[ { \"nestedInt\": 7, \"nestedString\": \"FirstNested\" \"firstNested\": { \"a\": 17, \"b\": 23, \"c\": \"SubObject\" } } ]" asData];
MPWObjectBuilder *builder=[[[self alloc] initWithClass:[MPWJSONNestedDecodeTestClass class]] autorelease];
[builder setClassesForKeys:@{ @"firstNested": [NSMutableDictionary class] }];
MPWMASONParser *parser=[[[MPWMASONParser alloc] initWithBuilder:builder] autorelease];
NSArray *result = [parser parsedData:jsonNestedObjects];
INTEXPECT(result.count, 1, @"parsed array count");
MPWJSONNestedDecodeTestClass *first=result.firstObject;
INTEXPECT(first.nestedInt, 7, @"first.nestedInt");
IDEXPECT(first.nestedString, @"FirstNested", @"first.nestedString");
NSMutableDictionary *firstSubObject=(NSMutableDictionary*)first.firstNested;
EXPECTNOTNIL(firstSubObject, @"there should be a subdict");
EXPECTTRUE([firstSubObject isKindOfClass:[NSMutableDictionary class]], @"and it's a dictionary");
INTEXPECT(firstSubObject.count,3,@"all the values");
IDEXPECT(firstSubObject[@"a"], @(17), @"firstSubObject.a");
IDEXPECT(firstSubObject[@"b"], @(23), @"firstSubObject.b");
IDEXPECT(firstSubObject[@"c"], @"SubObject", @"firstSubObject.c");
}
+(NSArray*)testSelectors
{
return @[
@"testDecodeFlatClassArray",
@"testDecodeNestedObject",
@"testDecodeNestedArrayWithObjects",
@"testCanEmbedDictionaries",
];
}
@end