diff --git a/Realm+JSON/RLMObject+JSON.h b/Realm+JSON/RLMObject+JSON.h index bbc05a2..6140910 100644 --- a/Realm+JSON/RLMObject+JSON.h +++ b/Realm+JSON/RLMObject+JSON.h @@ -10,6 +10,11 @@ #import "MCJSONDateTransformer.h" #import "MCJSONValueTransformer.h" +typedef NS_ENUM(NSUInteger, RLMPropertyMapping) { + RLMPropertyMappingCamelToSnakeCase = 0, + RLMPropertyMappingAsIs +}; + @interface RLMObject (JSON) + (NSArray *)createOrUpdateInRealm:(RLMRealm *)realm withJSONArray:(NSArray *)array; @@ -25,6 +30,9 @@ - (void)performInTransaction:(void (^)())transaction; - (void)removeFromRealm; ++ (NSDictionary *)defaultInboundMappingForType:(RLMPropertyMapping)mappingType; ++ (NSDictionary *)defaultOutboundMappingForType:(RLMPropertyMapping)mappingType; + @end @interface RLMArray (JSON) diff --git a/Realm+JSON/RLMObject+JSON.m b/Realm+JSON/RLMObject+JSON.m index 6f00f56..cb6411c 100644 --- a/Realm+JSON/RLMObject+JSON.m +++ b/Realm+JSON/RLMObject+JSON.m @@ -57,6 +57,7 @@ + (RLMObjectSchema *)sharedSchema; @implementation RLMObject (JSON) static NSInteger const kCreateBatchSize = 100; +//static + (NSArray *)createOrUpdateInRealm:(RLMRealm *)realm withJSONArray:(NSArray *)array { NSInteger count = array.count; @@ -165,7 +166,7 @@ + (id)mc_createObjectFromJSONDictionary:(NSDictionary *)dictionary { id value = [dictionary valueForKeyPath:dictionaryKeyPath]; - if (value) { + if (value && ![value isKindOfClass:[NSNull class]]) { Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath]; NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath]; @@ -260,26 +261,32 @@ - (id)mc_createJSONDictionary { #pragma mark - Properties -+ (NSDictionary *)mc_defaultInboundMapping { - RLMObjectSchema *schema = [self sharedSchema]; - - NSMutableDictionary *result = [NSMutableDictionary dictionary]; - for (RLMProperty *property in schema.properties) { - result[[property.name camelToSnakeCase]] = property.name; ++ (NSDictionary *)defaultInboundMappingForType:(RLMPropertyMapping)mappingType { + RLMObjectSchema *schema = [self sharedSchema]; + NSMutableDictionary *result = [NSMutableDictionary dictionary]; + for (RLMProperty *property in schema.properties) { + if (mappingType == RLMPropertyMappingCamelToSnakeCase) { + result[[property.name camelToSnakeCase]] = property.name; + } else { + result[property.name] = property.name; } + } return [result copy]; } -+ (NSDictionary *)mc_defaultOutboundMapping { - RLMObjectSchema *schema = [self sharedSchema]; - - NSMutableDictionary *result = [NSMutableDictionary dictionary]; - for (RLMProperty *property in schema.properties) { - result[property.name] = [property.name camelToSnakeCase]; ++ (NSDictionary *)defaultOutboundMappingForType:(RLMPropertyMapping)mappingType { + RLMObjectSchema *schema = [self sharedSchema]; + NSMutableDictionary *result = [NSMutableDictionary dictionary]; + for (RLMProperty *property in schema.properties) { + if (mappingType == RLMPropertyMappingCamelToSnakeCase) { + result[[property.name camelToSnakeCase]] = property.name; + } else { + result[property.name] = property.name; } + } return [result copy]; } @@ -299,7 +306,7 @@ + (NSDictionary *)mc_inboundMapping { mapping = MCValueFromInvocation(self, selector); } else { - mapping = [self mc_defaultInboundMapping]; + mapping = [self defaultInboundMappingForType:RLMPropertyMappingCamelToSnakeCase]; } mappingForClassName[[self className]] = mapping; } @@ -319,7 +326,7 @@ + (NSDictionary *)mc_outboundMapping { mapping = MCValueFromInvocation(self, selector); } else { - mapping = [self mc_defaultOutboundMapping]; + mapping = [self defaultOutboundMappingForType:RLMPropertyMappingCamelToSnakeCase]; } mappingForClassName[[self className]] = mapping; }