Coder Social home page Coder Social logo

fasteasymapping's Introduction

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

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.5.1

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.

fasteasymapping's People

Contributors

dmytrozavgorodniy avatar

Watchers

Chew Chit Siang avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.