Maks-Jago / FastEasyMapping

Tool to serialize & deserialize JSON fast

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FastEasyMapping

Build Status

Note

This is fork of EasyMapping - flexible and easy way of JSON mapping.

Reason to be

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.

FastEasyMapping

Setup

Cocoapods:

#Podfile

pod 'FastEasyMapping'

or add as a static library.

Usage

Deserialization. NSManagedObject

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 addAttributeMappingDictionary:@{@"personID": @"id"}];
		[mapping addAttributeMappingFromArray:@[@"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 addAttributeMappingFromArray:@[@"model", @"year"]];
	}];
}

+ (FEMManagedObjectMapping *)phoneMapping {
	return [FEMManagedObjectMapping mappingForEntityName:@"Phone" configuration:^(FEMManagedObjectMapping *mapping) {
		[mapping addAttributeMappingDictionary:@{@"phoneID" : @"id"}];
		[mapping addAttributeMappingFromArray:@[@"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];

Assignment Policy

Now relationship can use one of three predefined assignment policies: FEMAssignmentPolicyAssign, FEMAssignmentPolicyMerge and FEMAssignmentPolicyReplace.

Deserialization. NSObject

If you are using NSObject use FEMObjectMapping instead of FEMManagedObjectMapping and FEMObjectDeserializer instead of FEMManagedObjectDeserializer.

Serialization

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]];

Changelog

0.4.1

  • Resolves: #19, #18, #16, #12
  • Add Specs for AttributeMapping

0.3.7

  • Added equality check before objects removal in FEMAssignmentPolicyObjectReplace
  • Fixed minor issues

0.3.7

0.3.3

0.3.2

0.3.1

  • Set deployment target to 6.0
  • Fix missing cache for + [FEMManagedObjectDeserializer fillObject:fromExternalRepresentation:usingMapping:]
  • Update hanlding of nil relationships in assignment policies

0.3.0

  • Add assignment policy support for FEMManagedObjectDeserializer: Assign, Merge, Replace
  • Cover FEMCache by tests

0.2.1

  • Improved types introspection by @advantis

0.2.0

  • Renamed to FastEasyMapping

0.1.2

  • Fixed serialization of BOOL properties on 64 bits

0.1.1

  • Fixed caching behaviour for new objects

0.1

  • Added managed objects cache for deserialization

Thanks

Extra

Read out blogpost about FastEasyMapping.

About

Tool to serialize & deserialize JSON fast

License:Other