From 4d0a95044a982ddda3a61e8a246eb2ff75e38bf0 Mon Sep 17 00:00:00 2001 From: V-Ken Chin Date: Thu, 27 Oct 2016 15:15:21 +1100 Subject: [PATCH 1/3] Change JSONAPIResource protocol func name `-(id)ID;` will be genereted to `func id() -> Any?` in Swift 3.0 which conflicts with internal function --- Classes/JSONAPI.m | 6 +++--- Classes/JSONAPIResource.h | 2 +- Classes/JSONAPIResourceParser.m | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Classes/JSONAPI.m b/Classes/JSONAPI.m index 6026dc1..ed672ae 100644 --- a/Classes/JSONAPI.m +++ b/Classes/JSONAPI.m @@ -125,7 +125,7 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary { JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType:data[@"type"]]; NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy; - typeDict[resource.ID] = resource; + typeDict[resource.iD] = resource; includedResources[desc.type] = typeDict; } @@ -136,7 +136,7 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary { JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType:data[@"type"]]; NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy; - typeDict[resource.ID] = resource; + typeDict[resource.iD] = resource; includedResources[desc.type] = typeDict; } @@ -190,7 +190,7 @@ - (NSDictionary *)mapIncludedResources:(NSArray *)relatedResources forResource:( for (NSObject *linked in relatedResources) { JSONAPIResourceDescriptor *desc = [[linked class] descriptor]; NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy; - typeDict[linked.ID] = resource; + typeDict[linked.iD] = resource; includedResources[desc.type] = typeDict; } return includedResources; diff --git a/Classes/JSONAPIResource.h b/Classes/JSONAPIResource.h index bd38b1e..e67daf7 100644 --- a/Classes/JSONAPIResource.h +++ b/Classes/JSONAPIResource.h @@ -115,7 +115,7 @@ * * @return The record identifier for a resource instance. */ -- (id)ID; +- (id)iD; /** * Set the API record identifier for a resource instance. Required for resources that come diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index be4c087..2a87c62 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -309,7 +309,7 @@ + (void)link:(NSObject *)resource withIncluded:(JSONAPI*)jsonAP NSObject *res = obj; id includedValue = included[[[res.class descriptor] type]]; if (includedValue) { - id v = includedValue[res.ID]; + id v = includedValue[res.iD]; if (v != nil) { matched[idx] = v; } @@ -324,7 +324,7 @@ + (void)link:(NSObject *)resource withIncluded:(JSONAPI*)jsonAP id res = value; id includedValue = included[[[res.class descriptor] type]]; if (includedValue) { - id v = included[[[res.class descriptor] type]][res.ID]; + id v = included[[[res.class descriptor] type]][res.iD]; if (v != nil) { [resource setValue:v forKey:key]; } @@ -371,10 +371,10 @@ + (NSDictionary*)link:(NSObject *)resource from:(NSObject Date: Tue, 15 Nov 2016 14:16:28 +1100 Subject: [PATCH 2/3] Fix cases where jsonName is used instead of property key --- Classes/JSONAPIResourceParser.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index 2a87c62..365c16c 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -118,7 +118,7 @@ + (NSDictionary*)dictionaryFor:(NSObject *)resource { } for (id valueElement in valueArray) { - [dictionaryArray addObject:[self link:valueElement from:resource withKey:[property jsonName]]]; + [dictionaryArray addObject:[self link:valueElement from:resource withKey:key]]; } NSDictionary *dataDictionary = @{@"data" : dictionaryArray}; @@ -144,7 +144,7 @@ + (NSDictionary*)dictionaryFor:(NSObject *)resource { } NSObject *attribute = value; - [linkage setValue:[self link:attribute from:resource withKey:[property jsonName]] forKey:[property jsonName]]; + [linkage setValue:[self link:attribute from:resource withKey:key] forKey:[property jsonName]]; } else { format = [property formatter]; if (format) { From 7c446a9a605a93dd9c535e8d4741fe2dc8322241 Mon Sep 17 00:00:00 2001 From: V-Ken Chin Date: Wed, 11 Apr 2018 22:19:44 +1000 Subject: [PATCH 3/3] JSONAPIResourceFactory protocol for object creation --- Classes/JSONAPIResourceFactory.h | 16 ++++++++++++++++ Classes/JSONAPIResourceParser.m | 8 +++++++- Project/JSONAPI.xcodeproj/project.pbxproj | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 Classes/JSONAPIResourceFactory.h diff --git a/Classes/JSONAPIResourceFactory.h b/Classes/JSONAPIResourceFactory.h new file mode 100644 index 0000000..0d191fe --- /dev/null +++ b/Classes/JSONAPIResourceFactory.h @@ -0,0 +1,16 @@ +// +// JSONAPIResourceFactory.h +// JSONAPI +// +// Created by V-Ken Chin on 11/4/18. +// Copyright © 2018 Josh Holtz. All rights reserved. +// + +#ifndef JSONAPIResourceFactory_h +#define JSONAPIResourceFactory_h + +#import "JSONAPIResource.h" +@protocol JSONAPIResourceFactory ++ (id)resourceObjectFor:(NSDictionary *)dictionary; +@end +#endif /* JSONAPIResourceFactory_h */ diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index 365c16c..b796d04 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -10,6 +10,7 @@ #import "JSONAPI.h" #import "JSONAPIResourceDescriptor.h" #import "JSONAPIPropertyDescriptor.h" +#import "JSONAPIResourceFactory.h" #pragma mark - JSONAPIResourceParser @@ -56,7 +57,12 @@ @implementation JSONAPIResourceParser NSString *type = dictionary[@"type"] ?: @""; JSONAPIResourceDescriptor *descriptor = [JSONAPIResourceDescriptor forLinkedType:type]; - NSObject *resource = [[[descriptor resourceClass] alloc] init]; + NSObject *resource; + if ([[descriptor resourceClass] conformsToProtocol:@protocol(JSONAPIResourceFactory)]) { + resource = [[descriptor resourceClass] resourceObjectFor:dictionary]; + } else { + resource = [[[descriptor resourceClass] alloc] init]; + } [self set:resource withDictionary:dictionary]; return resource; diff --git a/Project/JSONAPI.xcodeproj/project.pbxproj b/Project/JSONAPI.xcodeproj/project.pbxproj index 11cfa41..9865f96 100644 --- a/Project/JSONAPI.xcodeproj/project.pbxproj +++ b/Project/JSONAPI.xcodeproj/project.pbxproj @@ -130,6 +130,7 @@ 78A5C1D21C1E14D6008C8632 /* MediaResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MediaResource.m; sourceTree = ""; }; 78A5C1D41C1E154C008C8632 /* WebPageResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebPageResource.h; sourceTree = ""; }; 78A5C1D51C1E154C008C8632 /* WebPageResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebPageResource.m; sourceTree = ""; }; + 86CE9D90207E168300D76280 /* JSONAPIResourceFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceFactory.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -268,6 +269,7 @@ 685481E91AE4161900D3A633 /* JSONAPIPropertyDescriptor.h */, 685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */, 033A6C5D18695CD2001CF9FA /* JSONAPIResource.h */, + 86CE9D90207E168300D76280 /* JSONAPIResourceFactory.h */, 03FBD8DD1AB8DF8E00789DF3 /* JSONAPIErrorResource.h */, 03FBD8DE1AB8DF8E00789DF3 /* JSONAPIErrorResource.m */, 685481ED1AE419CB00D3A633 /* JSONAPIResourceDescriptor.h */,