Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #70

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Realm+JSON/RLMObject+JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +30,9 @@
- (void)performInTransaction:(void (^)())transaction;
- (void)removeFromRealm;

+ (NSDictionary *)defaultInboundMappingForType:(RLMPropertyMapping)mappingType;
+ (NSDictionary *)defaultOutboundMappingForType:(RLMPropertyMapping)mappingType;

@end

@interface RLMArray (JSON)
Expand Down
37 changes: 22 additions & 15 deletions Realm+JSON/RLMObject+JSON.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
}
Expand All @@ -299,7 +306,7 @@ + (NSDictionary *)mc_inboundMapping {
mapping = MCValueFromInvocation(self, selector);
}
else {
mapping = [self mc_defaultInboundMapping];
mapping = [self defaultInboundMappingForType:RLMPropertyMappingCamelToSnakeCase];
}
mappingForClassName[[self className]] = mapping;
}
Expand All @@ -319,7 +326,7 @@ + (NSDictionary *)mc_outboundMapping {
mapping = MCValueFromInvocation(self, selector);
}
else {
mapping = [self mc_defaultOutboundMapping];
mapping = [self defaultOutboundMappingForType:RLMPropertyMappingCamelToSnakeCase];
}
mappingForClassName[[self className]] = mapping;
}
Expand Down