Skip to content

Commit

Permalink
Merge pull request #34 from gazsp/master
Browse files Browse the repository at this point in the history
Transform values before trying anything else
  • Loading branch information
matthewcheok committed May 23, 2015
2 parents 81d1ec4 + 360ad8d commit 14a9c19
Showing 1 changed file with 40 additions and 52 deletions.
92 changes: 40 additions & 52 deletions Realm+JSON/RLMObject+JSON.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ @implementation RLMObject (JSON)
+ (NSArray *)createOrUpdateInRealm:(RLMRealm *)realm withJSONArray:(NSArray *)array {
NSInteger count = array.count;
NSMutableArray *result = [NSMutableArray array];

for (NSInteger index=0; index*kCreateBatchSize<count; index++) {
NSInteger size = MIN(kCreateBatchSize, count-index*kCreateBatchSize);
@autoreleasepool {
Expand All @@ -72,7 +72,7 @@ + (NSArray *)createOrUpdateInRealm:(RLMRealm *)realm withJSONArray:(NSArray *)ar
}
}
}

return [result copy];
}

Expand Down Expand Up @@ -159,66 +159,57 @@ - (void)removeFromRealm {
+ (id)mc_createObjectFromJSONDictionary:(NSDictionary *)dictionary {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSDictionary *mapping = [[self class] mc_inboundMapping];

for (NSString *dictionaryKeyPath in mapping) {
NSString *objectKeyPath = mapping[dictionaryKeyPath];

id value = [dictionary valueForKeyPath:dictionaryKeyPath];

if (value) {

Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath];

if ([propertyClass isSubclassOfClass:[RLMObject class]]) {
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];
if (transformer) {
value = [transformer transformedValue:value];
}
else if ([propertyClass isSubclassOfClass:[RLMObject class]]) {
if (!value || [value isEqual:[NSNull null]]) {
continue;
}

if ([value isKindOfClass:[NSDictionary class]]) {
value = [propertyClass mc_createObjectFromJSONDictionary:value];
} else {
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];

if (transformer) {
value = [transformer transformedValue:value];
}
}
if ([value isKindOfClass:[NSDictionary class]]) {
value = [propertyClass mc_createObjectFromJSONDictionary:value];
}
}
else if ([propertyClass isSubclassOfClass:[RLMArray class]]) {
RLMProperty *property = [self mc_propertyForPropertyKey:objectKeyPath];
Class elementClass = [RLMSchema classForString: property.objectClassName];
NSMutableArray *array = [NSMutableArray array];
for (id item in(NSArray*) value) {
[array addObject:[elementClass mc_createObjectFromJSONDictionary:item]];
}
value = [array copy];
RLMProperty *property = [self mc_propertyForPropertyKey:objectKeyPath];
Class elementClass = [RLMSchema classForString: property.objectClassName];

NSMutableArray *array = [NSMutableArray array];
for (id item in(NSArray*) value) {
[array addObject:[elementClass mc_createObjectFromJSONDictionary:item]];
}
value = [array copy];
}
else {
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];

if (transformer) {
value = [transformer transformedValue:value];
if ([objectKeyPath isEqualToString:@"self"]) {
return value;
}

NSArray *keyPathComponents = [objectKeyPath componentsSeparatedByString:@"."];
id currentDictionary = result;
for (NSString *component in keyPathComponents) {
if ([currentDictionary valueForKey:component] == nil) {
[currentDictionary setValue:[NSMutableDictionary dictionary] forKey:component];
}
currentDictionary = [currentDictionary valueForKey:component];
}

if ([objectKeyPath isEqualToString:@"self"]) {
return value;
}

NSArray *keyPathComponents = [objectKeyPath componentsSeparatedByString:@"."];
id currentDictionary = result;
for (NSString *component in keyPathComponents) {
if ([currentDictionary valueForKey:component] == nil) {
[currentDictionary setValue:[NSMutableDictionary dictionary] forKey:component];
}
currentDictionary = [currentDictionary valueForKey:component];
}

[result setValue:value forKeyPath:objectKeyPath];
}
}
return [result copy];

return [result copy];
}

- (id)mc_createJSONDictionary {
Expand All @@ -232,7 +223,11 @@ - (id)mc_createJSONDictionary {
if (value) {
Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath];

if ([propertyClass isSubclassOfClass:[RLMObject class]]) {
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];
if (transformer) {
value = [transformer reverseTransformedValue:value];
}
else if ([propertyClass isSubclassOfClass:[RLMObject class]]) {
value = [value mc_createJSONDictionary];
}
else if ([propertyClass isSubclassOfClass:[RLMArray class]]) {
Expand All @@ -242,13 +237,6 @@ - (id)mc_createJSONDictionary {
}
value = [array copy];
}
else {
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];

if (transformer) {
value = [transformer reverseTransformedValue:value];
}
}

if ([dictionaryKeyPath isEqualToString:@"self"]) {
return value;
Expand All @@ -274,7 +262,7 @@ - (id)mc_createJSONDictionary {

+ (NSDictionary *)mc_defaultInboundMapping {
RLMObjectSchema *schema = [self sharedSchema];

NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (RLMProperty *property in schema.properties) {
result[[property.name camelToSnakeCase]] = property.name;
Expand All @@ -286,11 +274,11 @@ + (NSDictionary *)mc_defaultInboundMapping {

+ (NSDictionary *)mc_defaultOutboundMapping {
RLMObjectSchema *schema = [self sharedSchema];

NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (RLMProperty *property in schema.properties) {
result[property.name] = [property.name camelToSnakeCase];

}

return [result copy];
Expand Down

0 comments on commit 14a9c19

Please sign in to comment.