Skip to content

Commit 2567863

Browse files
author
Josh Holtz
committed
Merge pull request #38 from joshdholtz/v1.0.6
Added setID and setSelfLink in init or property descriptor, bumped ve…
2 parents 063535e + 2b06d74 commit 2567863

File tree

6 files changed

+187
-22
lines changed

6 files changed

+187
-22
lines changed

Classes/JSONAPIResourceDescriptor.m

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ - (instancetype)initWithClass:(Class)resource forLinkedType:(NSString*)linkedTyp
4444
_type = linkedType;
4545
_resourceClass = resource;
4646
_properties = [[NSMutableDictionary alloc] init];
47+
48+
[self setIdProperty:@"ID"];
49+
[self setSelfLinkProperty:@"selfLink"];
4750
}
4851

4952
return self;

JSONAPI.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "JSONAPI"
3-
s.version = "1.0.5"
3+
s.version = "1.0.6"
44
s.summary = "A library for loading data from a JSON API datasource."
55
s.description = <<-DESC
66
A library for loading data from a JSON API datasource. Parses JSON API data into models with support for auto-linking of resources and custom model classes.

Project/JSONAPI/ArticleResource.m

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ + (JSONAPIResourceDescriptor*)descriptor {
2424
static dispatch_once_t onceToken;
2525
dispatch_once(&onceToken, ^{
2626
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"articles"];
27-
28-
[__descriptor setIdProperty:@"ID"];
29-
[__descriptor setSelfLinkProperty:@"selfLink"];
27+
28+
// These are set by default in the JSONAPIResourceDescriptor init
29+
// [__descriptor setIdProperty:@"ID"];
30+
// [__descriptor setSelfLinkProperty:@"selfLink"];
3031

3132
[__descriptor addProperty:@"title"];
3233
[__descriptor addProperty:@"date"

Project/JSONAPI/CommentResource.m

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ + (JSONAPIResourceDescriptor*)descriptor {
2222
static dispatch_once_t onceToken;
2323
dispatch_once(&onceToken, ^{
2424
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"comments"];
25-
26-
[__descriptor setIdProperty:@"ID"];
27-
2825
[__descriptor addProperty:@"text" withJsonName:@"body"];
29-
26+
3027
[__descriptor hasOne:[PeopleResource class] withName:@"author"];
3128
});
3229

Project/JSONAPI/PeopleResource.m

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ + (JSONAPIResourceDescriptor*)descriptor {
1919
static dispatch_once_t onceToken;
2020
dispatch_once(&onceToken, ^{
2121
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"people"];
22-
23-
[__descriptor setIdProperty:@"ID"];
24-
22+
2523
[__descriptor addProperty:@"firstName" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"first-name"]];
2624
[__descriptor addProperty:@"lastName" withJsonName:@"last-name"];
2725
[__descriptor addProperty:@"twitter"];

README.md

+177-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ For some full examples on how to use everything, please see the tests - https://
2121
2222
Version | Changes
2323
--- | ---
24-
**1.0.5** | Fix 1-to-many relationships serialization according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/34). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
25-
**1.0.4** | Add support for empty to-one relationship according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/33). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
24+
**1.0.6** | Improved resource parsing and added parsing of `selfLinks` (https://github.com/joshdholtz/jsonapi-ios/pull/35). Thanks to [ artcom](https://github.com/ artcom) for helping! Also removed the need to define `setIdProperty` and `setSelfLinkProperty` in every resource (automatically mapped in the init of `JSONAPIResourceDescriptor`)
25+
**1.0.5** | Fix 1-to-many relationships serialization according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/34). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
26+
**1.0.4** | Add support for empty to-one relationship according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/33). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
2627
**1.0.3** | Add ability to map different types of objects (https://github.com/joshdholtz/jsonapi-ios/pull/32). Thanks to [ealeksandrov](https://github.com/ealeksandrov) for helping!
2728
**1.0.2** | Just some bug fixes. Thanks to [christianklotz](https://github.com/christianklotz) for helping again!
2829
**1.0.1** | Now safely checks for `NSNull` in the parsed JSON. Thanks to [christianklotz](https://github.com/christianklotz) for that fix!
@@ -47,23 +48,40 @@ Clone the repository and drop in the .h and .m files from the "Classes" director
4748
JSONAPI is available through [CocoaPods](http://cocoapods.org), to install
4849
it simply add the following line to your Podfile:
4950
50-
pod 'JSONAPI', '~> 1.0.0'
51+
pod 'JSONAPI', '~> 1.0.6'
5152
52-
## Usage
53+
## Classes/protocols
5354
For some full examples on how to use everything, please see the tests - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m
5455
5556
### JSONAPI
5657
`JSONAPI` parses and validates a JSON API document into a usable object. This object holds the response as an NSDictionary but provides methods to accomdate the JSON API format such as `meta`, `errors`, `linked`, `resources`, and `includedResources`.
5758
5859
### JSONAPIResource
59-
`JSONAPIResource` is an object (that gets subclassed) that holds data for each resource in a JSON API document. This objects holds the "id" as `ID` and link for self as `selfLink` as well as attributes and relationships defined by descriptors (see below)
60+
Protocol of an object that is available for JSON API serialization. When developing model classes for use with JSON-API, it is suggested that classes be derived from `JSONAPIResourceBase`, but that is not required. An existing model class can be adapted for JSON-API by implementing this protocol.
6061
61-
#### Resource mappings (using descriptors)
62+
### JSONAPIResourceBase
63+
`JSONAPIResourceBase` is an object (that gets subclassed) that holds data for each resource in a JSON API document. This objects holds the "id" as `ID` and link for self as `selfLink` as well as attributes and relationships defined by descriptors (see below)
64+
65+
### JSONAPIResourceDescriptor
6266
`+ (JSONAPIResourceDescriptor*)descriptor` should be overwritten to define descriptors for mapping of JSON keys and relationships into properties of a subclassed JSONAPIResource.
6367
64-
##### Usage
68+
## Full Example
69+
70+
### ViewController.m
6571
66-
````objc
72+
```objc
73+
NSDictionary *json = [self responseFromAPIRequest];
74+
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
75+
76+
ArticleResource *article = jsonAPI.resource;
77+
NSLog(@"Title: %@", article.title);
78+
NSLog(@"Author: %@ %@", article.author.firstName, article.author.lastName);
79+
NSLog(@"Comment Count: %ld", article.comments.count);
80+
```
81+
82+
### ArticleResource.h
83+
84+
```objc
6785

6886
@interface ArticleResource : JSONAPIResourceBase
6987

@@ -73,7 +91,11 @@ For some full examples on how to use everything, please see the tests - https://
7391
@property (nonatomic, strong) NSArray *comments;
7492

7593
@end
94+
```
95+
96+
### ArticleResource.m
7697

98+
```objc
7799
@implementation ArticleResource
78100

79101
static JSONAPIResourceDescriptor *__descriptor = nil;
@@ -83,8 +105,6 @@ static JSONAPIResourceDescriptor *__descriptor = nil;
83105
dispatch_once(&onceToken, ^{
84106
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"articles"];
85107

86-
[__descriptor setIdProperty:@"ID"];
87-
88108
[__descriptor addProperty:@"title"];
89109
[__descriptor addProperty:@"date"
90110
withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"date" withFormat:[NSDateFormatter RFC3339DateFormatter]]];
@@ -98,7 +118,153 @@ static JSONAPIResourceDescriptor *__descriptor = nil;
98118

99119
@end
100120

101-
````
121+
```
122+
123+
### PeopleResource.h
124+
125+
```objc
126+
@interface PeopleResource : JSONAPIResourceBase
127+
128+
@property (nonatomic, strong) NSString *firstName;
129+
@property (nonatomic, strong) NSString *lastName;
130+
@property (nonatomic, strong) NSString *twitter;
131+
132+
@end
133+
```
134+
135+
### PeopleResource.m
136+
137+
```objc
138+
@implementation PeopleResource
139+
140+
static JSONAPIResourceDescriptor *__descriptor = nil;
141+
142+
+ (JSONAPIResourceDescriptor*)descriptor {
143+
static dispatch_once_t onceToken;
144+
dispatch_once(&onceToken, ^{
145+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"people"];
146+
147+
[__descriptor addProperty:@"firstName" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"first-name"]];
148+
[__descriptor addProperty:@"lastName" withJsonName:@"last-name"];
149+
[__descriptor addProperty:@"twitter"];
150+
});
151+
152+
return __descriptor;
153+
}
154+
155+
@end
156+
```
157+
158+
### CommentResource.h
159+
160+
```objc
161+
@interface CommentResource : JSONAPIResourceBase
162+
163+
@property (nonatomic, strong) NSString *text;
164+
@property (nonatomic, strong) PeopleResource *author;
165+
166+
@end
167+
```
168+
169+
### CommentResource.m
170+
171+
```objc
172+
@implementation CommentResource
173+
174+
static JSONAPIResourceDescriptor *__descriptor = nil;
175+
176+
+ (JSONAPIResourceDescriptor*)descriptor {
177+
static dispatch_once_t onceToken;
178+
dispatch_once(&onceToken, ^{
179+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"comments"];
180+
181+
[__descriptor addProperty:@"text" withJsonName:@"body"];
182+
183+
[__descriptor hasOne:[PeopleResource class] withName:@"author"];
184+
});
185+
186+
return __descriptor;
187+
}
188+
189+
@end
190+
```
191+
192+
## Advanced
193+
194+
### How to do custom "sub-resources" mappings
195+
Sometimes you may have parts of a resource that need to get mapped to something more specific than just an `NSDictionary`. Below is an example on how to map an `NSDictionary` to non-JSONAPIResource models.
196+
197+
We are essentially creating a property descriptor that maps to a private property on the resource. We then override that properties setter and do our custom mapping there.
198+
199+
#### Resource part of JSON API Response
200+
```js
201+
"attributes":{
202+
"title": "Something something blah blah blah"
203+
"image": {
204+
"large": "http://someimageurl.com/large",
205+
"medium": "http://someimageurl.com/medium",
206+
"small": "http://someimageurl.com/small"
207+
}
208+
}
209+
```
210+
211+
#### ArticleResource.h
212+
213+
```objc
214+
215+
@interface ArticleResource : JSONAPIResourceBase
216+
217+
@property (nonatomic, strong) NSString *title;
218+
219+
// The properties we are pulling out of a a "images" dictionary
220+
@property (nonatomic, storng) NSString *largeImageUrl;
221+
@property (nonatomic, storng) NSString *mediumImageUrl;
222+
@property (nonatomic, storng) NSString *smallImageUrl;
223+
224+
@end
225+
```
226+
227+
#### ArticleResource.m
228+
229+
```objc
230+
@interface ArticleResource()
231+
232+
// Private variable used to store raw NSDictionary
233+
// We will override the setter and set our custom properties there
234+
@property (nonatomic, strong) NSDictionary *rawImage;
235+
236+
@end
237+
238+
@implementation ArticleResource
239+
240+
static JSONAPIResourceDescriptor *__descriptor = nil;
241+
242+
+ (JSONAPIResourceDescriptor*)descriptor {
243+
static dispatch_once_t onceToken;
244+
dispatch_once(&onceToken, ^{
245+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"articles"];
246+
247+
[__descriptor addProperty:@"title"];
248+
[__descriptor addProperty:@"rawImage" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"image"]];
249+
250+
});
251+
252+
return __descriptor;
253+
}
254+
255+
- (void)setRawImage:(NSDictionary*)rawImage {
256+
_rawImage = rawImage;
257+
258+
// Pulling the large, medium, and small urls out when
259+
// this property gets set by the JSON API parser
260+
_largeImageUrl = _rawImage[@"large"];
261+
_mediumImageUrl = _rawImage[@"medium"];
262+
_smallImageUrl = _rawImage[@"small"];
263+
}
264+
265+
@end
266+
267+
```
102268
103269
## Author
104270

0 commit comments

Comments
 (0)