Skip to content

Commit bf218bd

Browse files
author
Josh Holtz
committed
Merge branch 'master' of github.com:joshdholtz/jsonapi-ios
2 parents 1d5a319 + 8dc1dd2 commit bf218bd

File tree

1 file changed

+141
-4
lines changed

1 file changed

+141
-4
lines changed

README.md

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44

55
A library for loading data from a [JSON API](http://jsonapi.org) datasource. Parses JSON API data into models with support for auto-linking of resources and custom model classes.
66

7+
### Updates
8+
9+
Version | Changes
10+
--- | ---
11+
**0.1.2** | `JSONAPIResource` IDs can either be numbers or strings (thanks [danylhebreux](https://github.com/danylhebreux)); `JSONAPIResource` subclass can have mappings defined to set JSON values into properties automatically - [more info](#resource-mappings)
12+
**0.1.1** | Fixed linked resources with links so they actually link to other linked resources
13+
**0.1.0** | Initial release
14+
715
### Features
816
- Parses datasource into manageable objects of `JSONAPIResource`
917
- Auto-links resources with custom link mapping definitions using `JSONAPIResourceLinker` (ex: link 'book' to 'books', link 'person' to 'people')
1018
- Allows resource types to be created into subclasses of `JSONAPIResource` using `JSONAPIResourceModeler`
19+
- Set mapping for `JSONAPIResource` subclass to set JSON values into properties
1120

1221
## Installation
1322

@@ -27,7 +36,28 @@ it simply add the following line to your Podfile:
2736
`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`, `linked`, and `(NSArray*)resourcesForKey:(NSString*)key`.
2837

2938
### JSONAPIResource
30-
`JSONAPIResource` is an object that holds data for each resource in a JSON API document. This objects holds the "id", "href", and "links" as properties but also the rest of the object as an NSDictionary that can be accessed through `(id)objectForKey:(NSString*)key`. There is also a method for retrieving linked resources from the JSON API document by using `(id)linkedResourceForKey:(NSString*)key`
39+
`JSONAPIResource` is an object that holds data for each resource in a JSON API document. This objects holds the "id", "href", and "links" as properties but also the rest of the object as an NSDictionary that can be accessed through `(id)objectForKey:(NSString*)key`. There is also a method for retrieving linked resources from the JSON API document by using `(id)linkedResourceForKey:(NSString*)key`.
40+
41+
#### Resource mappings
42+
`(NSDictionary*)mapKeysToProperties` can be overwritten to define a dictionary mapping of JSON keys to map into properties of a subclassed JSONAPIResource. Use a "links." prefix on the JSON key to map a linked JSONAPIResource model or array of JSONAPIResource models
43+
44+
````objc
45+
46+
@implementation ASubclassedResource
47+
48+
- (NSDictionary *)mapKeysToProperties {
49+
// Maps values in JSON key 'first_name' to 'firstName' property
50+
// Maps linked resource in JSON key 'author' to 'author' property
51+
// Maps linked resource in JSON key 'comments' to 'comments' property
52+
return @{
53+
@"first_name" : @"firstName",
54+
@"links.author" : @"author",
55+
@"links.comments" : @"comments"
56+
};
57+
58+
@end
59+
60+
````
3161
3262
### JSONAPIResourceLinker
3363
`JSONAPIResourceLinker` is used for configuring the type of 'links' resources to 'linked' resources.
@@ -168,6 +198,111 @@ for (PostResource *post in posts) {
168198
169199
```` objc
170200
201+
@interface PeopleResource : JSONAPIResource
202+
203+
- (NSString*)name;
204+
205+
@end
206+
207+
@implementation PeopleResource
208+
209+
- (NSString *)name {
210+
return [self objectForKey:@"name"];
211+
}
212+
213+
@end
214+
215+
````
216+
217+
### Parsing - Using linked resources, subclassed JSONAPIResource classes, and model mappings
218+
This example shows how a response can be mapped directly into properties of a sublcasses JSONAPIResource
219+
220+
```` objc
221+
222+
NSString *json = @"{\"posts\":[{\"id\":1,\"name\":\"A post!\",\"links\":{\"author\":9,\"comments\":[2,3]}},{\"id\":2,\"name\":\"Another post!\",\"links\":{\"author\":10,\"comments\":[3,4]}}],\"linked\":{\"people\":[{\"id\":9,\"name\":\"Josh Holtz\"},{\"id\":10,\"name\":\"Bandit the Cat\"}],\"comments\":[{\"id\":2,\"text\":\"Omg this post is awesome\"},{ \"id\":3,\"text\":\"Omg this post is awesomer\"},{ \"id\":4,\"text\":\"Meeeehhhhh\"}]}}";
223+
224+
// Links "author" resource to "people" linked resources
225+
[JSONAPIResourceLinker link:@"author" toLinkedType:@"people"];
226+
227+
//
228+
[JSONAPIResourceModeler useResource:[PeopleResource class] toLinkedType:@"people"];
229+
[JSONAPIResourceModeler useResource:[PostResource class] toLinkedType:@"posts"];
230+
[JSONAPIResourceModeler useResource:[CommentResource class] toLinkedType:@"comments"];
231+
232+
// Parses JSON string into JSONAPI object
233+
JSONAPI *jsonApi = [JSONAPI JSONAPIWithString:json];
234+
235+
// Gets posts from JSONAPI that will be an array of PostResource objects
236+
NSArray *posts = [jsonApi resourcesForKey:@"posts"];
237+
238+
// Parsing using JSONAPI and modeled resources (PostResource, PeopleResource, CommentResource
239+
for (PostResource *post in posts) {
240+
241+
PeopleResource *author = post.author;
242+
NSLog(@"\"%@\" by %@", post.name, author.name);
243+
244+
NSArray *comments = post.comments;
245+
for (CommentResource *comment in comments) {
246+
NSLog(@"\t%@", comment.text);
247+
}
248+
}
249+
250+
````
251+
252+
#### PostResource.h, PostResource.m
253+
254+
```` objc
255+
256+
@interface PostResource : JSONAPIResource
257+
258+
- (PeopleResource*)author;
259+
- (NSArray*)comments; // Array of CommentResource
260+
- (NSString*)name;
261+
262+
@end
263+
264+
@implementation PostResource
265+
266+
- (NSDictionary *)mapKeysToProperties {
267+
// Maps values in JSON key 'name' to 'name' property
268+
// Maps linked resource in JSON key 'author' to 'author' property
269+
// Maps linked resource in JSON key 'comments' to 'comments' property
270+
return @{
271+
@"name" : @"name",
272+
@"links.author" : @"author",
273+
@"links.comments" : @"comments"
274+
};
275+
276+
@end
277+
278+
````
279+
280+
#### PeopleResource.h, PeopleResource.m
281+
282+
```` objc
283+
284+
@interface PeopleResource : JSONAPIResource
285+
286+
- (NSString*)name;
287+
288+
@end
289+
290+
@implementation PeopleResource
291+
292+
- (NSDictionary *)mapKeysToProperties {
293+
// Maps values in JSON key 'name' to 'name' property
294+
return @{
295+
@"name" : @"name"
296+
};
297+
298+
@end
299+
300+
````
301+
302+
#### CommentResource.h, CommentResource.m
303+
304+
```` objc
305+
171306
@interface CommentResource : JSONAPIResource
172307
173308
- (NSString*)text;
@@ -176,9 +311,11 @@ for (PostResource *post in posts) {
176311
177312
@implementation CommentResource
178313
179-
- (NSString *)text {
180-
return [self objectForKey:@"text"];
181-
}
314+
- (NSDictionary *)mapKeysToProperties {
315+
// Maps values in JSON key 'text' to 'text' property
316+
return @{
317+
@"text" : @"text"
318+
};
182319
183320
@end
184321

0 commit comments

Comments
 (0)