Skip to content

Commit 1b00624

Browse files
author
Josh Holtz
committed
Merge pull request #32 from ViralFoundry/master
Add ability to map different types of objects
2 parents 1851be3 + cb23632 commit 1b00624

15 files changed

+481
-12
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: objective-c
2+
osx_image: xcode7.1
23
before_install:
34
- brew update
45
- brew uninstall xctool && brew install xctool --HEAD

Classes/JSONAPIResourceParser.m

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
112112
if (valueArray.count > 0) {
113113
NSMutableArray *dictionaryArray = [[NSMutableArray alloc] initWithCapacity:valueArray.count];
114114

115-
if ([property resourceType]) {
115+
if ([property resourceType] || [((NSArray *)value).firstObject conformsToProtocol:@protocol(JSONAPIResource)]) {
116116
if (linkage == nil) {
117117
linkage = [[NSMutableDictionary alloc] init];
118118
}
@@ -138,7 +138,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
138138
}
139139
}
140140
} else {
141-
if ([property resourceType]) {
141+
if ([property resourceType] || [value conformsToProtocol:@protocol(JSONAPIResource)]) {
142142
if (linkage == nil) {
143143
linkage = [[NSMutableDictionary alloc] init];
144144
}
@@ -203,6 +203,11 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
203203
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
204204
}
205205

206+
} else if (relationships[key]) {
207+
if (relationships) {
208+
id value = relationships[key];
209+
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
210+
}
206211
} else {
207212
id value = [attributes objectForKey:[property jsonName]];;
208213
if ((id)[NSNull null] == value) {
@@ -270,29 +275,45 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
270275
for (NSString *key in properties) {
271276
JSONAPIPropertyDescriptor *propertyDescriptor = [properties objectForKey:key];
272277
id value = [resource valueForKey:key];
273-
id includedValue = included[[[propertyDescriptor.resourceType descriptor] type]];
278+
279+
Class valueClass = nil;
280+
if (propertyDescriptor.resourceType) {
281+
valueClass = propertyDescriptor.resourceType;
282+
} else if ([value conformsToProtocol:@protocol(JSONAPIResource)] || [value isKindOfClass:[NSArray class]]) {
283+
valueClass = [value class];
284+
}
274285

275286
// ordinary attribute
276-
if (propertyDescriptor.resourceType == nil) {
287+
if (valueClass == nil) {
277288
continue;
278289
// has many
279290
} else if ([value isKindOfClass:[NSArray class]]) {
280291
NSMutableArray *matched = [value mutableCopy];
281292
[value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
282-
NSObject <JSONAPIResource> *res = obj;
283-
id v = includedValue[res.ID];
284-
if (v != nil) {
285-
matched[idx] = v;
293+
if ([obj conformsToProtocol:@protocol(JSONAPIResource)]) {
294+
NSObject <JSONAPIResource> *res = obj;
295+
id includedValue = included[[[res.class descriptor] type]];
296+
if (includedValue) {
297+
id v = includedValue[res.ID];
298+
if (v != nil) {
299+
matched[idx] = v;
300+
}
301+
}
286302
}
287303
}];
288304

289305
[resource setValue:matched forKey:key];
290306
// has one
291307
} else if (value != nil) {
292-
NSObject <JSONAPIResource> *res = value;
293-
id v = includedValue[res.ID];
294-
if (v != nil) {
295-
[resource setValue:v forKey:key];
308+
if ([value conformsToProtocol:@protocol(JSONAPIResource)]) {
309+
id <JSONAPIResource> res = value;
310+
id includedValue = included[[[res.class descriptor] type]];
311+
if (includedValue) {
312+
id v = included[[[res.class descriptor] type]][res.ID];
313+
if (v != nil) {
314+
[resource setValue:v forKey:key];
315+
}
316+
}
296317
}
297318
}
298319
}

Project/JSONAPI.xcodeproj/project.pbxproj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */; };
5252
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EE1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m */; };
5353
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */; };
54+
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
55+
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
56+
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */; };
57+
78A5C1CD1C1E13A8008C8632 /* UserResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1CC1C1E13A8008C8632 /* UserResource.m */; };
58+
78A5C1D01C1E147B008C8632 /* SocialCommunityResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */; };
59+
78A5C1D31C1E14D6008C8632 /* MediaResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1D21C1E14D6008C8632 /* MediaResource.m */; };
60+
78A5C1D61C1E154C008C8632 /* WebPageResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1D51C1E154C008C8632 /* WebPageResource.m */; };
5461
/* End PBXBuildFile section */
5562

5663
/* Begin PBXContainerItemProxy section */
@@ -109,6 +116,17 @@
109116
68A469921AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+JSONAPIDateFormatter.h"; sourceTree = "<group>"; };
110117
68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+JSONAPIDateFormatter.m"; sourceTree = "<group>"; };
111118
68DF9E951B06B4C500FA6429 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
119+
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = generic_relationships_example.json; sourceTree = "<group>"; };
120+
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewsFeedPostResource.h; sourceTree = "<group>"; };
121+
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewsFeedPostResource.m; sourceTree = "<group>"; };
122+
78A5C1CB1C1E13A8008C8632 /* UserResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserResource.h; sourceTree = "<group>"; };
123+
78A5C1CC1C1E13A8008C8632 /* UserResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UserResource.m; sourceTree = "<group>"; };
124+
78A5C1CE1C1E147B008C8632 /* SocialCommunityResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocialCommunityResource.h; sourceTree = "<group>"; };
125+
78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocialCommunityResource.m; sourceTree = "<group>"; };
126+
78A5C1D11C1E14D6008C8632 /* MediaResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MediaResource.h; sourceTree = "<group>"; };
127+
78A5C1D21C1E14D6008C8632 /* MediaResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MediaResource.m; sourceTree = "<group>"; };
128+
78A5C1D41C1E154C008C8632 /* WebPageResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebPageResource.h; sourceTree = "<group>"; };
129+
78A5C1D51C1E154C008C8632 /* WebPageResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebPageResource.m; sourceTree = "<group>"; };
112130
/* End PBXFileReference section */
113131

114132
/* Begin PBXFrameworksBuildPhase section */
@@ -144,6 +162,16 @@
144162
03866450186A909200985CEC /* PeopleResource.m */,
145163
03866452186A94B700985CEC /* CommentResource.h */,
146164
03866453186A94B700985CEC /* CommentResource.m */,
165+
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */,
166+
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */,
167+
78A5C1CB1C1E13A8008C8632 /* UserResource.h */,
168+
78A5C1CC1C1E13A8008C8632 /* UserResource.m */,
169+
78A5C1CE1C1E147B008C8632 /* SocialCommunityResource.h */,
170+
78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */,
171+
78A5C1D11C1E14D6008C8632 /* MediaResource.h */,
172+
78A5C1D21C1E14D6008C8632 /* MediaResource.m */,
173+
78A5C1D41C1E154C008C8632 /* WebPageResource.h */,
174+
78A5C1D51C1E154C008C8632 /* WebPageResource.m */,
147175
);
148176
name = ResourceModels;
149177
sourceTree = "<group>";
@@ -253,6 +281,7 @@
253281
03FBD8D81AB879E800789DF3 /* Assets */ = {
254282
isa = PBXGroup;
255283
children = (
284+
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */,
256285
03FBD8D91AB879FD00789DF3 /* main_example.json */,
257286
03FBD8E11AB8E46E00789DF3 /* error_example.json */,
258287
);
@@ -339,6 +368,7 @@
339368
isa = PBXResourcesBuildPhase;
340369
buildActionMask = 2147483647;
341370
files = (
371+
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */,
342372
03A055D31868E038004807F0 /* Images.xcassets in Resources */,
343373
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */,
344374
03A055C51868E038004807F0 /* InfoPlist.strings in Resources */,
@@ -355,6 +385,7 @@
355385
files = (
356386
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */,
357387
03A055E41868E038004807F0 /* InfoPlist.strings in Resources */,
388+
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */,
358389
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */,
359390
);
360391
runOnlyForDeploymentPostprocessing = 0;
@@ -382,16 +413,21 @@
382413
isa = PBXSourcesBuildPhase;
383414
buildActionMask = 2147483647;
384415
files = (
416+
78A5C1CD1C1E13A8008C8632 /* UserResource.m in Sources */,
417+
78A5C1D61C1E154C008C8632 /* WebPageResource.m in Sources */,
385418
03866451186A909200985CEC /* PeopleResource.m in Sources */,
386419
681B47761B08EA9800A99D76 /* JSONAPIResourceBase.m in Sources */,
420+
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */,
387421
680E14F51B08146E004FF8CD /* JSONAPIResourceParser.m in Sources */,
388422
03866457186A94C200985CEC /* ArticleResource.m in Sources */,
389423
03A055D11868E038004807F0 /* ViewController.m in Sources */,
390424
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */,
425+
78A5C1D31C1E14D6008C8632 /* MediaResource.m in Sources */,
391426
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */,
392427
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */,
393428
03866454186A94B700985CEC /* CommentResource.m in Sources */,
394429
03A055CB1868E038004807F0 /* AppDelegate.m in Sources */,
430+
78A5C1D01C1E147B008C8632 /* SocialCommunityResource.m in Sources */,
395431
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */,
396432
03A055C71868E038004807F0 /* main.m in Sources */,
397433
03FBD8DF1AB8DF8E00789DF3 /* JSONAPIErrorResource.m in Sources */,

Project/JSONAPI/MediaResource.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MediaResource.h
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 14.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResourceBase.h"
10+
11+
@interface MediaResource : JSONAPIResourceBase
12+
13+
@property (nonatomic, strong) NSString *mimeType;
14+
@property (nonatomic, strong) NSString *fileUrl;
15+
16+
@end

Project/JSONAPI/MediaResource.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// MediaResource.m
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 14.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "MediaResource.h"
10+
11+
#import "JSONAPIPropertyDescriptor.h"
12+
#import "JSONAPIResourceDescriptor.h"
13+
14+
@implementation MediaResource
15+
16+
static JSONAPIResourceDescriptor *__descriptor = nil;
17+
18+
+ (JSONAPIResourceDescriptor*)descriptor {
19+
static dispatch_once_t onceToken;
20+
dispatch_once(&onceToken, ^{
21+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"Media"];
22+
23+
[__descriptor setIdProperty:@"ID"];
24+
25+
[__descriptor addProperty:@"mimeType"];
26+
[__descriptor addProperty:@"fileUrl"];
27+
});
28+
29+
return __descriptor;
30+
}
31+
32+
@end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// NewsFeedPostResource.h
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 13.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResourceBase.h"
10+
#import "UserResource.h"
11+
#import "SocialCommunityResource.h"
12+
#import "MediaResource.h"
13+
#import "WebPageResource.h"
14+
15+
@interface NewsFeedPostResource : JSONAPIResourceBase
16+
17+
@property (nonatomic, strong) NSDate *createdAt;
18+
@property (nonatomic, strong) NSString *title;
19+
@property (nonatomic, strong) NSString *text;
20+
21+
@property (nonatomic, strong) id<JSONAPIResource> publisher;
22+
@property (nonatomic, strong) NSArray *attachments;
23+
24+
@end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// NewsFeedPostResource.m
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 13.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "NewsFeedPostResource.h"
10+
11+
#import "JSONAPIPropertyDescriptor.h"
12+
#import "JSONAPIResourceDescriptor.h"
13+
#import "NSDateFormatter+JSONAPIDateFormatter.h"
14+
15+
@implementation NewsFeedPostResource
16+
17+
static JSONAPIResourceDescriptor *__descriptor = nil;
18+
19+
+ (JSONAPIResourceDescriptor*)descriptor {
20+
static dispatch_once_t onceToken;
21+
dispatch_once(&onceToken, ^{
22+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"NewsFeedPost"];
23+
24+
[__descriptor setIdProperty:@"ID"];
25+
26+
[__descriptor addProperty:@"createdAt"
27+
withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"createdAt" withFormat:[NSDateFormatter RFC3339DateFormatter]]];
28+
[__descriptor addProperty:@"title"];
29+
[__descriptor addProperty:@"text"];
30+
31+
[__descriptor hasOne:nil withName:@"publisher"];
32+
[__descriptor hasMany:nil withName:@"attachments"];
33+
});
34+
35+
return __descriptor;
36+
}
37+
38+
@end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// SocialCommunityResource.h
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 14.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResourceBase.h"
10+
11+
@interface SocialCommunityResource : JSONAPIResourceBase
12+
13+
@property (nonatomic, strong) NSString *title;
14+
@property (nonatomic, strong) NSString *homePage;
15+
16+
@end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// SocialCommunityResource.m
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 14.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "SocialCommunityResource.h"
10+
11+
#import "JSONAPIPropertyDescriptor.h"
12+
#import "JSONAPIResourceDescriptor.h"
13+
14+
@implementation SocialCommunityResource
15+
16+
static JSONAPIResourceDescriptor *__descriptor = nil;
17+
18+
+ (JSONAPIResourceDescriptor*)descriptor {
19+
static dispatch_once_t onceToken;
20+
dispatch_once(&onceToken, ^{
21+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"SocialCommunity"];
22+
23+
[__descriptor setIdProperty:@"ID"];
24+
25+
[__descriptor addProperty:@"title"];
26+
[__descriptor addProperty:@"homePage"];
27+
});
28+
29+
return __descriptor;
30+
}
31+
32+
@end

Project/JSONAPI/UserResource.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// UserResource.h
3+
// JSONAPI
4+
//
5+
// Created by Rafael Kayumov on 13.12.15.
6+
// Copyright © 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResourceBase.h"
10+
11+
@interface UserResource : JSONAPIResourceBase
12+
13+
@property (nonatomic, strong) NSString *name;
14+
@property (nonatomic, strong) NSString *email;
15+
16+
@end

0 commit comments

Comments
 (0)