Skip to content

Commit 997c267

Browse files
author
Josh Holtz
committed
JSONAPIResources can now map values and resouces into properties with a definition of a dictionary in the subclass
1 parent c2108e2 commit 997c267

File tree

8 files changed

+62
-0
lines changed

8 files changed

+62
-0
lines changed

Project/JSONAPI/CommentResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
@interface CommentResource : JSONAPIResource
1212

13+
@property (nonatomic, strong) NSString *mapText;
14+
1315
- (NSString*)text;
1416

1517
@end

Project/JSONAPI/CommentResource.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ - (NSString *)text {
1414
return [self objectForKey:@"text"];
1515
}
1616

17+
- (NSDictionary *)mapKeysToProperties {
18+
return @{
19+
@"text" : @"mapText"
20+
};
21+
}
22+
1723
@end

Project/JSONAPI/PeopleResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
@interface PeopleResource : JSONAPIResource
1212

13+
@property (nonatomic, strong) NSString *mapName;
14+
1315
- (NSString*)name;
1416

1517
@end

Project/JSONAPI/PeopleResource.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ - (NSString *)name {
1414
return [self objectForKey:@"name"];
1515
}
1616

17+
- (NSDictionary *)mapKeysToProperties {
18+
return @{
19+
@"name" : @"mapName"
20+
};
21+
}
22+
1723
@end

Project/JSONAPI/PostResource.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
@interface PostResource : JSONAPIResource
1515

16+
@property (nonatomic, strong) NSString *mapName;
17+
@property (nonatomic, strong) PeopleResource *mapAuthor;
18+
@property (nonatomic, strong) NSArray *mapComments;
19+
1620
- (PeopleResource*)author;
1721
- (NSArray*)comments;
1822
- (NSString*)name;

Project/JSONAPI/PostResource.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ - (NSString *)name {
2222
return [self objectForKey:@"name"];
2323
}
2424

25+
- (NSDictionary *)mapKeysToProperties {
26+
return @{
27+
@"name" : @"mapName",
28+
@"links.author" : @"mapAuthor",
29+
@"links.comments" : @"mapComments"
30+
};
31+
}
32+
2533
@end

Project/JSONAPI/ViewController.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ - (void)viewDidLoad
3333
// Parsing using JSONAPI and JSONAPIResource
3434
JSONAPI *jsonApi = [JSONAPI JSONAPIWithDictionary:[self authorResponse]];
3535

36+
NSLog(@"------------ Parsing using JSONAPI and JSONAPIResource");
3637
NSArray *posts = [jsonApi resourcesForKey:@"posts"];
3738
for (JSONAPIResource *post in posts) {
3839

@@ -46,6 +47,7 @@ - (void)viewDidLoad
4647
}
4748

4849
// Parsing using JSONAPI and modeled resources (PostResource, PeopleResource, CommentResource
50+
NSLog(@"\n\n------------ Parsing using JSONAPI and modeled resources (PostResource, PeopleResource, CommentResource");
4951
for (PostResource *post in posts) {
5052

5153
PeopleResource *author = post.author;
@@ -57,6 +59,19 @@ - (void)viewDidLoad
5759
}
5860
}
5961

62+
// Parsing using JSONAPI, modeled resources, and mapped properties (PostResource, PeopleResource, CommentResource
63+
NSLog(@"\n\n------------ Parsing using JSONAPI, modeled resources, and mapped properties (PostResource, PeopleResource, CommentResource");
64+
for (PostResource *post in posts) {
65+
66+
PeopleResource *author = post.author;
67+
NSLog(@"\"%@\" by %@", post.mapName, author.mapName);
68+
69+
NSArray *comments = post.mapComments;
70+
for (CommentResource *comment in comments) {
71+
NSLog(@"\t%@", comment.mapText);
72+
}
73+
}
74+
6075
}
6176

6277
- (void)didReceiveMemoryWarning

Project/JSONAPITests/JSONAPITests.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,23 @@ - (void)testLinksInLinked {
247247
XCTAssertEqualObjects([authorResource objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Author name should equal %@", [chapter objectForKey:@"name"]);
248248
}
249249

250+
- (void)testMapKeysToProperties {
251+
NSDictionary *meta = @{ @"page_number" : @1, @"number_of_pages" : @5};
252+
NSDictionary *linkedAuthor9 = @{ @"id" : @9, @"name" : @"Josh" };
253+
NSDictionary *linkedAuthor11 = @{ @"id" : @11, @"name" : @"Bandit" };
254+
NSArray *linkedAuthors = @[ linkedAuthor9, linkedAuthor11 ];
255+
NSDictionary *linked = @{ @"authors" : linkedAuthors };
256+
NSDictionary *post = @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } };
257+
NSArray *posts = @[ post ];
258+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : posts };
259+
260+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
261+
PostResource *postResource = [jsonAPI resourceForKey:@"posts"];
262+
263+
XCTAssertEqual([postResource class], [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
264+
XCTAssertEqual([postResource.mapAuthor class], [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.mapAuthor class]);
265+
XCTAssertEqualObjects(postResource.mapName, [post objectForKey:@"name"], @"Post name is not equal to %@", [post objectForKey:@"name"]);
266+
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
267+
}
268+
250269
@end

0 commit comments

Comments
 (0)