Skip to content

Commit 7fe82e9

Browse files
author
Josh Holtz
committed
Update README.md
1 parent 50b19a9 commit 7fe82e9

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

README.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# JSONAPI - iOS
22

33
[![Build Status](https://travis-ci.org/joshdholtz/jsonapi-ios.png?branch=master)](https://travis-ci.org/joshdholtz/jsonapi-ios)
4+
![](https://cocoapod-badges.herokuapp.com/v/JSONAPI/badge.png)
45

56
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.
67

78
### Updates
89

910
Version | Changes
1011
--- | ---
12+
**0.1.3** | Added `NSCopying` and `NSCoded` to `JSONAPIResource`; Added `JSONAPIResourceFormatter` to format values before getting mapped - [more info](#formatter)
1113
**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)
1214
**0.1.1** | Fixed linked resources with links so they actually link to other linked resources
1315
**0.1.0** | Initial release
@@ -41,18 +43,34 @@ it simply add the following line to your Podfile:
4143
#### Resource mappings
4244
`(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
4345

46+
#### Formatter
47+
`JSONAPIResourceFormatter` is used to format values before getting mapped from `mapKeysToProperties`.
48+
49+
50+
4451
##### Usage
4552

4653
````objc
4754

55+
@interface ASubclassedResource
56+
57+
@property (nonatomic, strong) NSString *firstName;
58+
@property (nonatomic, strong) NSDate *date;
59+
@property (nonatomic, strong) NSArray *authors;
60+
@property (nonatomic, strong) NSArray *comments;
61+
62+
@end
63+
4864
@implementation ASubclassedResource
4965

5066
- (NSDictionary *)mapKeysToProperties {
5167
// Maps values in JSON key 'first_name' to 'firstName' property
68+
// Maps values in JSON key 'date' to 'date' property using the 'Date' formatter
5269
// Maps linked resource in JSON key 'author' to 'author' property
5370
// Maps linked resource in JSON key 'comments' to 'comments' property
5471
return @{
5572
@"first_name" : @"firstName",
73+
@"date" : @"Date:date",
5674
@"links.author" : @"author",
5775
@"links.comments" : @"comments"
5876
};
@@ -61,31 +79,24 @@ it simply add the following line to your Podfile:
6179

6280
````
6381
64-
##### Map values outside of `mapKeysToProperties` method
65-
If you need to map values that are a little odd, like mapping to enums or performing some sort of formatting before setting a property, you can override the `initWithDictionary` method and assign properties in there.
82+
##### Using `JSONAPIResourceFormatter` to format values
83+
Below is an example to register a "Date" function to format a date in a NSString object to an NSDate object before its mapped to the JSONAPIResource instance.
6684
6785
````objc
6886
69-
typedef enum {
70-
JESSE,
71-
CHESTER
72-
} Character;
73-
74-
@property (nonatomic, assign) Character character;
75-
76-
- (id)initWithDictionary:(NSDictionary *)dict withLinked:(NSDictionary *)linked {
77-
self = [super initWithDictionary:dict withLinked:linked];
78-
if (self) {
79-
// Do stuff in there
80-
NSString *tatoo = [self objectForKey:@"tatoo"];
81-
if ([someKey isEqualToString:@"dude"]) {
82-
character = JESSE;
83-
} else if ([someKey isEqualToString:@"sweet"]) { {
84-
character = CHESTER
85-
}
87+
[JSONAPIResourceFormatter registerFormat:@"Date" withBlock:^id(id jsonValue) {
88+
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
89+
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
90+
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
91+
92+
NSDate *date = nil;
93+
NSError *error = nil;
94+
if (![dateFormatter getObjectValue:&date forString:jsonValue range:nil error:&error]) {
95+
NSLog(@"Date '%@' could not be parsed: %@", jsonValue, error);
8696
}
87-
return self;
88-
}
97+
98+
return date;
99+
}];
89100
90101
````
91102

0 commit comments

Comments
 (0)