Skip to content

Commit 118d65e

Browse files
author
Julian Krumow
committed
Merge remote-tracking branch 'joshdholtz/master'
2 parents a12bbb0 + ccf872c commit 118d65e

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
121121
[dictionaryArray addObject:[self link:valueElement from:resource withKey:[property jsonName]]];
122122
}
123123

124-
[linkage setValue:dictionaryArray forKey:[property jsonName]];
125-
124+
NSDictionary *dataDictionary = @{@"data" : dictionaryArray};
125+
[linkage setValue:dataDictionary forKey:[property jsonName]];
126126
} else {
127127
NSFormatter *format = [property formatter];
128128

@@ -372,10 +372,15 @@ + (NSDictionary*)link:(NSObject <JSONAPIResource>*)resource from:(NSObject <JSON
372372
}
373373

374374
if (resource.ID) {
375-
[reference setValue:@{
376-
@"type" : descriptor.type,
377-
@"id" : resource.ID
378-
} forKey:@"data"];
375+
NSDictionary *referenceObject = @{
376+
@"type" : descriptor.type,
377+
@"id" : resource.ID
378+
};
379+
if ([[owner valueForKey:key] isKindOfClass:[NSArray class]]) {
380+
reference = referenceObject.mutableCopy;
381+
} else {
382+
[reference setValue:referenceObject forKey:@"data"];
383+
}
379384
}
380385

381386
return reference;

JSONAPI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "JSONAPI"
3-
s.version = "1.0.4"
3+
s.version = "1.0.5"
44
s.summary = "A library for loading data from a JSON API datasource."
55
s.description = <<-DESC
66
A library for loading data from a JSON API datasource. Parses JSON API data into models with support for auto-linking of resources and custom model classes.

Project/JSONAPITests/JSONAPITests.m

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,21 @@ - (void)testSerializeComplex {
152152
newAuthor.firstName = @"Karl";
153153
newAuthor.lastName = @"Armstrong";
154154

155-
CommentResource *newComment = [[CommentResource alloc] init];
156-
newComment.ID = [NSUUID UUID];
157-
newComment.author = newAuthor;
158-
newComment.text = @"First!";
155+
CommentResource *firstComment = [[CommentResource alloc] init];
156+
firstComment.ID = [NSUUID UUID];
157+
firstComment.author = newAuthor;
158+
firstComment.text = @"First!";
159+
160+
CommentResource *secondComment = [[CommentResource alloc] init];
161+
secondComment.ID = [NSUUID UUID];
162+
secondComment.author = newAuthor;
163+
secondComment.text = @"Second!";
159164

160165
ArticleResource *newArticle = [[ArticleResource alloc] init];
161166
newArticle.title = @"Title";
162167
newArticle.author = newAuthor;
163168
newArticle.date = [NSDate date];
164-
newArticle.comments = [[NSArray alloc] initWithObjects:newComment, nil];
169+
newArticle.comments = [[NSArray alloc] initWithObjects:firstComment, secondComment, nil];
165170

166171
NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newArticle];
167172
XCTAssertEqualObjects(json[@"type"], @"articles", @"Did not create Article!");
@@ -172,8 +177,8 @@ - (void)testSerializeComplex {
172177
XCTAssertNil(json[@"relationships"][@"author"][@"first-name"], @"Bad link!");
173178

174179
XCTAssertNotNil(json[@"relationships"][@"comments"], @"Did not create links!");
175-
XCTAssertTrue([json[@"relationships"][@"comments"] isKindOfClass:[NSArray class]], @"Comments should be array!.");
176-
XCTAssertEqual([json[@"relationships"][@"comments"] count], 1, @"Comments should have 1 element!.");
180+
XCTAssertTrue([json[@"relationships"][@"comments"][@"data"] isKindOfClass:[NSArray class]], @"Comments data should be array!.");
181+
XCTAssertEqual([json[@"relationships"][@"comments"][@"data"] count], 2, @"Comments should have 2 elements!.");
177182
}
178183

179184
- (void)testCreate {
@@ -227,11 +232,11 @@ - (void)testGenericSerialization {
227232
NSDictionary *serializedFirstPost = [JSONAPIResourceParser dictionaryFor:posts.firstObject];
228233
NSDictionary *serializedSecondPost = [JSONAPIResourceParser dictionaryFor:posts.lastObject];
229234

230-
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][0], @"Media attachment should not be nil");
231-
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][1], @"Web page url attachment should not be nil");
235+
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0], @"Media attachment should not be nil");
236+
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][1], @"Web page url attachment should not be nil");
232237

233-
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"id"], @15, @"Media id should be '15'");
234-
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"type"], @"Media", @"Media type should be 'Media'");
238+
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"id"], @15, @"Media id should be '15'");
239+
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"type"], @"Media", @"Media type should be 'Media'");
235240

236241
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"publisher"][@"data"][@"id"], @45, @"User id should be '45'");
237242
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"publisher"][@"data"][@"type"], @"User", @"User type should be 'User'");

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66

77
A library for loading data from a [JSON API](http://jsonapi.org) datasource. Parses JSON API data into models with support for linking of properties and other resources.
88

9+
### Quick Usage
10+
```objc
11+
NSDictionary *json = [self responseFromAPIRequest];
12+
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
13+
14+
ArticleResource *article = jsonAPI.resource;
15+
NSLog(@"Title: %@", article.title);
16+
```
17+
18+
For some full examples on how to use everything, please see the tests - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m
19+
920
### Updates
1021
1122
Version | Changes
1223
--- | ---
24+
**1.0.5** | Fix 1-to-many relationships serialization according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/34). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
1325
**1.0.4** | Add support for empty to-one relationship according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/33). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
1426
**1.0.3** | Add ability to map different types of objects (https://github.com/joshdholtz/jsonapi-ios/pull/32). Thanks to [ealeksandrov](https://github.com/ealeksandrov) for helping!
1527
**1.0.2** | Just some bug fixes. Thanks to [christianklotz](https://github.com/christianklotz) for helping again!
@@ -38,6 +50,7 @@ it simply add the following line to your Podfile:
3850
pod 'JSONAPI', '~> 1.0.0'
3951
4052
## Usage
53+
For some full examples on how to use everything, please see the tests - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m
4154
4255
### JSONAPI
4356
`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`.

0 commit comments

Comments
 (0)