You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+141-4Lines changed: 141 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,10 +4,19 @@
4
4
5
5
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.
6
6
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
+
7
15
### Features
8
16
- Parses datasource into manageable objects of `JSONAPIResource`
9
17
- Auto-links resources with custom link mapping definitions using `JSONAPIResourceLinker` (ex: link 'book' to 'books', link 'person' to 'people')
10
18
- Allows resource types to be created into subclasses of `JSONAPIResource` using `JSONAPIResourceModeler`
19
+
- Set mapping for `JSONAPIResource` subclass to set JSON values into properties
11
20
12
21
## Installation
13
22
@@ -27,7 +36,28 @@ it simply add the following line to your Podfile:
27
36
`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`.
28
37
29
38
### 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
+
@implementationASubclassedResource
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
+
````
31
61
32
62
### JSONAPIResourceLinker
33
63
`JSONAPIResourceLinker` is used for configuring the type of 'links' resources to 'linked' resources.
@@ -168,6 +198,111 @@ for (PostResource *post in posts) {
168
198
169
199
```` objc
170
200
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
0 commit comments