This is fork of EasyMapping - flexible and easy way of JSON mapping.
It turns out, that almost all popular libraries for JSON mapping SLOW. The main reason is often trips to database during lookup of existing objects. So we decided to take already existing flexible solution and improve overall performance.
#Podfile
pod 'FastEasyMapping'
or add as a static library.
Supose you have these classes:
@interface Person : NSManagedObject
@property (nonatomic, retain) NSNumber *personID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) Car *car;
@property (nonatomic, retain) NSSet *phones;
@end
@interface Car : NSManagedObject
@property (nonatomic, retain) NSNumber *carID;
@property (nonatomic, retain) NSString *model;
@property (nonatomic, retain) NSString *year;
@property (nonatomic, retain) NSDate *createdAt;
@property (nonatomic, retain) Person *person;
@end
@interface Phone : NSManagedObject
@property (nonatomic, retain) NSNumber *phoneID;
@property (nonatomic, retain) NSString *ddi;
@property (nonatomic, retain) NSString *ddd;
@property (nonatomic, retain) NSString *number;
@property (nonatomic, retain) Person *person;
@end
Mapping can be described in next way:
@implementation MappingProvider
+ (FEMManagedObjectMapping *)personMapping {
return [FEMManagedObjectMapping mappingForEntityName:@"Person" configuration:^(FEMManagedObjectMapping *mapping) {
[mapping setPrimaryKey:@"personID"]; // object uniquing
[mapping addAttributesFromDictionary:@{@"personID": @"id"}];
[mapping addAttributesFromArray:@[@"name", @"email", @"gender"]];
[mapping addRelationshipMapping:[self carMapping] forProperty:@"car" keyPath:@"car"];
[mapping addToManyRelationshipMapping:[self phoneMapping] forProperty:@"phones" keyPath:@"phones"];
}];
}
+ (FEMManagedObjectMapping *)carMapping {
return [FEMManagedObjectMapping mappingForEntityName:@"Car" configuration:^(FEMManagedObjectMapping *mapping) {
[mapping setPrimaryKey:@"carID"];
[mapping addAttributesFromArray:@[@"model", @"year"]];
}];
}
+ (FEMManagedObjectMapping *)phoneMapping {
return [FEMManagedObjectMapping mappingForEntityName:@"Phone" configuration:^(FEMManagedObjectMapping *mapping) {
[mapping addAttributesFromDictionary:@{@"phoneID" : @"id"}];
[mapping addAttributesFromArray:@[@"number", @"ddd", @"ddi"]];
}];
}
@end
Converting a NSDictionary or NSArray to a object class or collection now becomes easy:
Person *person = [FEMManagedObjectDeserializer deserializeObjectExternalRepresentation:externalRepresentation
usingMapping:[MappingProvider personMapping]
context:context];
NSArray *cars = [FEMManagedObjectDeserializer deserializeCollectionExternalRepresentation:externalRepresentation
usingMapping:[MappingProvider carMapping]
context:moc];
Filling an existent object:
Person *person = // fetch somehow;
FEMManagedObjectMapping *mapping = [MappingProvider personMapping];
[FEMManagedObjectDeserializer fillObject:person fromExternalRepresentation:externalRepresentation usingMapping:mapping];
Now relationship can use one of three predefined assignment policies: FEMAssignmentPolicyAssign
, FEMAssignmentPolicyMerge
and FEMAssignmentPolicyReplace
.
If you are using NSObject use FEMObjectMapping
instead of FEMManagedObjectMapping
and FEMObjectDeserializer
instead of FEMManagedObjectDeserializer
.
For both NSManagedObject and NSObject serialization to JSON looks the same:
NSDictionary *representation = [FEMSerializer serializeObject:car usingMapping:[MappingProvider carMapping]];
NSArray *collectionRepresentation = [FEMSerializer serializeCollection:cars usingMapping:[MappingProvider carMapping]];
- Rename FEMAttributeMapping to FEMAttribute, FEMRelationshipMapping to FEMRelationship
- Shorten FEMMapping mutation methods
- Added equality check before objects removal in FEMAssignmentPolicyObjectReplace
- Fixed minor issues
- Add synchronization to FEMManagedObjectDeserializer
- Minor refactoring
- Fixed minor naming issues
- Update null-relationship handling in Managed Object Deserializer & Cache handling of nil-relationship
- Fix handling of nil-relationship data during deserialization
- Remove compiler warnings
- Set deployment target to 6.0
- Fix missing cache for + [FEMManagedObjectDeserializer fillObject:fromExternalRepresentation:usingMapping:]
- Update hanlding of nil relationships in assignment policies
- Add assignment policy support for FEMManagedObjectDeserializer: Assign, Merge, Replace
- Cover FEMCache by tests
- Improved types introspection by @advantis
- Renamed to FastEasyMapping
- Fixed serialization of BOOL properties on 64 bits
- Fixed caching behaviour for new objects
- Added managed objects cache for deserialization
- Special thanks to lucasmedeirosleite for amazing framework.
Read out blogpost about FastEasyMapping.