A library for loading data from a JSON API datasource. Parses JSON API data into models with support for linking of properties and other resources.
Version | Changes |
---|---|
1.0.3 | Add ability to map different types of objects (joshdholtz#32). Thanks to ealeksandrov for helping! |
1.0.2 | Just some bug fixes. Thanks to christianklotz for helping again! |
1.0.1 | Now safely checks for NSNull in the parsed JSON. Thanks to christianklotz for that fix! |
1.0.0 | We did it team! We are at the JSON API 1.0 final spec. Resources now use JSONAPIResourceDescriptor for more explicit definitions. HUGE thanks to jkarmstr for doing all the dirty work. Also thanks to edopelawi , BenjaminDigeon, and christianklotz for some bug fixes! |
1.0.0-rc1 | Rewrote core of JSONAPI and JSONAPIResource and all unit tests to be up to spec with JSON API spec 1.0.0-rc3. Removed JSONAPIResourceLinker . Added JSONAPIErrorResource |
0.2.0 | Added NSCopying and NSCoded to JSONAPIResource ; Added JSONAPIResourceFormatter to format values before getting mapped - more info |
0.1.2 | JSONAPIResource IDs can either be numbers or strings (thanks danylhebreux); JSONAPIResource subclass can have mappings defined to set JSON values into properties automatically - more info |
0.1.1 | Fixed linked resources with links so they actually link to other linked resources |
0.1.0 | Initial release |
- Allows resource types to be created into subclasses of
JSONAPIResource
- Set mapping for
JSONAPIResource
subclass to set JSON values and relationships into properties
Clone the repository and drop in the .h and .m files from the "Classes" directory into your project.
JSONAPI is available through CocoaPods, to install it simply add the following line to your Podfile:
pod 'JSONAPI', '~> 1.0.0'
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
.
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)
+ (JSONAPIResourceDescriptor*)descriptor
should be overwritten to define descriptors for mapping of JSON keys and relationships into properties of a subclassed JSONAPIResource.
@interface ArticleResource : JSONAPIResourceBase
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) PeopleResource *author;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSArray *comments;
@end
@implementation ArticleResource
static JSONAPIResourceDescriptor *__descriptor = nil;
+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"articles"];
[__descriptor setIdProperty:@"ID"];
[__descriptor addProperty:@"title"];
[__descriptor addProperty:@"date"
withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"date" withFormat:[NSDateFormatter RFC3339DateFormatter]]];
[__descriptor hasOne:[PeopleResource class] withName:@"author"];
[__descriptor hasMany:[CommentResource class] withName:@"comments"];
});
return __descriptor;
}
@end
Josh Holtz, [email protected], @joshdholtz
JSONAPI is available under the MIT license. See the LICENSE file for more info.