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/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 be4c087..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; @@ -118,7 +124,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 +150,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) { @@ -309,7 +315,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 +330,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 +377,10 @@ + (NSDictionary*)link:(NSObject *)resource from:(NSObject