这是一个用于从JSON API数据源加载数据的库。可以将JSON API数据解析成具有属性和其他资源链接的模型。
NSDictionary *json = [self responseFromAPIRequest];
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
ArticleResource *article = jsonAPI.resource;
NSLog(@"Title: %@", article.title);
有关如何使用所有功能的一些完整示例,请参阅测试 - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m
版本 | 变更 |
---|---|
1.0.7 | 向JSONAPIResource 和JSONAPIResourceBase 添加了meta 和setMeta ,(https://github.com/joshdholtz/jsonapi-ios/pull/43) |
1.0.6 | 改进了资源解析,并添加了对selfLinks 的解析(https://github.com/joshdholtz/jsonapi-ios/pull/35)。感谢artcom的帮助!另外,不再需要在每个资源中定义setIdProperty 和setSelfLinkProperty (在JSONAPIResourceDescriptor 初始化中自动映射) |
1.0.5 | 根据JSON API v1.0修复了1对多关系的序列化问题(https://github.com/joshdholtz/jsonapi-ios/pull/34)。感谢RafaelKayumov的帮助! |
1.0.4 | 根据JSON API v1.0添加了对空一对一关系的支持(https://github.com/joshdholtz/jsonapi-ios/pull/33)。感谢RafaelKayumov的帮助! |
1.0.3 | 添加了映射不同类型对象的能力(https://github.com/joshdholtz/jsonapi-ios/pull/32)。感谢ealeksandrov的帮助! |
1.0.2 | 只是做一些错误修复。再次感谢christianklotz的帮助! |
1.0.1 | 现在在解析JSON时安全地检查了NSNull 。感谢christianklotz所做的这次修复! |
1.0.0 | 团队做得好!我们现在遵循了JSON API 1.0最终规范 。现在资源使用JSONAPIResourceDescriptor 进行更加明确的定义。非常感谢jkarmstr做了所有艰苦的工作。还要感谢edopelawi、BenjaminDigeon和christianklotz测试时的错误修复! |
1.0.0-rc1 | 重写了JSONAPI 和JSONAPIResource 的核心,并将所有单元测试重新编写以满足JSON API规范1.0.0-rc3的要求。移除了JSONAPIResourceLinker ,添加了JSONAPIErrorResource 。 |
0.2.0 | 向JSONAPIResource 添加了NSCopying 和NSCoded 协议;添加了JSONAPIResourceFormatter 来格式化映射前的值 - 更多信息 |
0.1.2 |
JSONAPIResource 的ID可以是数字或字符串(感谢danylhebreux);JSONAPIResource 的子类可以定义映射,以自动将JSON值设置到属性中 - 更多信息 |
0.1.1 | 修复了带有链接的关联资源,使其实际链接到其他关联资源 |
0.1.0 | 初始发布 |
JSONAPIResource
JSONAPIResource
子类设置映射,以将JSON值和关系设置到属性中克隆仓库,并将“Classes”目录中的.h和.m文件复制到您的项目中。
有关如何使用所有功能的一些完整示例,请参阅测试 - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m
JSONAPI
将JSON API文档解析并验证为一个可用的对象。该对象将响应作为NSDictionary持有,但提供了方法来适应JSON API格式,如meta
、errors
、linked
、resources
和includedResources
。
对象协议,可对JSON API进行序列化。当为JSON-API开发模型类时,建议类从JSONAPIResourceBase
派生,但这不是必需的。可以通过实现此协议使现有的模型类适应JSON-API。
JSONAPIResourceBase
是一个对象(该对象被继承),该对象包含JSON API文档中每个资源的数据。该对象将"id"作为ID
以及自链接作为selfLink
持有,还包括由描述符(见下文)定义的属性和关系
使用+ (JSONAPIResourceDescriptor*)descriptor
应该被覆写以定义将JSON键和关系映射到JSONAPIResource子类属性中的描述符
NSDictionary *json = [self responseFromAPIRequest];
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
ArticleResource *article = jsonAPI.resource;
NSLog(@"Title: %@", article.title);
NSLog(@"Author: %@ %@", article.author.firstName, article.author.lastName);
NSLog(@"Comment Count: %ld", article.comments.count);
@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 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
@interface PeopleResource : JSONAPIResourceBase
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *twitter;
@end
@implementation PeopleResource
static JSONAPIResourceDescriptor *__descriptor = nil;
+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"people"];
[__descriptor addProperty:@"firstName" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"first-name"]];
[__descriptor addProperty:@"lastName" withJsonName:@"last-name"];
[__descriptor addProperty:@"twitter"];
});
return __descriptor;
}
@end
@interface CommentResource : JSONAPIResourceBase
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) PeopleResource *author;
@end
@implementation CommentResource
static JSONAPIResourceDescriptor *__descriptor = nil;
+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"comments"];
[__descriptor addProperty:@"text" withJsonName:@"body"];
[__descriptor hasOne:[PeopleResource class] withName:@"author"];
});
return __descriptor;
}
@end
有时您可能有一些需要映射比NSDictionary
更具体的资源部分。以下是如何将NSDictionary
映射到非JSONAPIResource模型的示例。
我们实际上是在创建一个属性描述符,它映射到资源上的一个私有属性。然后我们重写该属性的setter,并在那里进行自定义映射。
"attributes":{
"title": "Something something blah blah blah"
"image": {
"large": "http://someimageurl.com/large",
"medium": "http://someimageurl.com/medium",
"small": "http://someimageurl.com/small"
}
}
@interface ArticleResource : JSONAPIResourceBase
@property (nonatomic, strong) NSString *title;
// The properties we are pulling out of a a "images" dictionary
@property (nonatomic, storng) NSString *largeImageUrl;
@property (nonatomic, storng) NSString *mediumImageUrl;
@property (nonatomic, storng) NSString *smallImageUrl;
@end
@interface ArticleResource()
// Private variable used to store raw NSDictionary
// We will override the setter and set our custom properties there
@property (nonatomic, strong) NSDictionary *rawImage;
@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 addProperty:@"title"];
[__descriptor addProperty:@"rawImage" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"image"]];
});
return __descriptor;
}
- (void)setRawImage:(NSDictionary*)rawImage {
_rawImage = rawImage;
// Pulling the large, medium, and small urls out when
// this property gets set by the JSON API parser
_largeImageUrl = _rawImage[@"large"];
_mediumImageUrl = _rawImage[@"medium"];
_smallImageUrl = _rawImage[@"small"];
}
@end
Josh Holtz, [email protected], @joshdholtz
JSONAPI 在MIT许可下可用。有关更多信息,请参阅LICENSE文件。