brunow / BWObjectSerializer

Small library that transform an object into a dictionary that can be send to a web service.

Home Page:https://twitter.com/brunowernimont

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BWObjectSerializer

Small library that transform an object into a dictionary that can be send to a web service.

Work well with string, number and date others data must be converted. By default date are parsed with the rails date time format.

An object

Item *item = [[Item alloc] init];
item.title = @"Title";
item.itemID = [NSNumber numberWithInt:15];
item.createdAt = [NSDate date];

Serializing mapping

[BWObjectSerializerMapping mappingForObject:[Item class] block:^(BWObjectSerializerMapping *serializer) {
    [serializer mapKeyPath:@"title" toAttribute:@"title"];
    [serializer mapKeyPath:@"itemID" toAttribute:@"id"];
    
    [serializer mapKeyPath:@"imagePath" toAttribute:@"image_url" valueBlock:^id(id value, id object) {
        return @"custom value";
    }];
    
    [serializer mapKeyPath:@"createdAt" toAttribute:@"created_at" withDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    
    [[BWObjectSerializer shared] registerSerializer:serializer withRootKeyPath:@"item"];
}];

Serialize the object

NSDictionary *dict = [[BWObjectSerializer shared] serializeObject:item];

Or

NSDictionary *dict [[BWObjectSerializer shared] serializeObject:item withMapping:mapping];

Will generate this dictionary:

{
		item =     {
    		"created_at" = "2012-07-20T21:56:26Z";
    		"image_url" = "custom value";
    		title = Title;
		};
}

Installation

Copy BWObjectSerializer dir into your project.

Or with CocoaPods

pod 'BWObjectSerializer', :git => "https://github.com/brunow/BWObjectSerializer.git", :tag => "0.1.1"

ARC

BWObjectSerializer is ARC only.

Contact

Bruno Wernimont

Bitdeli Badge

About

Small library that transform an object into a dictionary that can be send to a web service.

https://twitter.com/brunowernimont

License:Apache License 2.0


Languages

Language:Objective-C 99.6%Language:Ruby 0.3%Language:Shell 0.1%Language:C++ 0.1%