• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Objective-C
  • License
    Apache License 2.0
  • Created over 12 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small library that parse almost automatically JSON and map it to any object, works with NSManagedObject.

BWObjectMapping

Small library that parse almost automatically JSON and map it to any object, works with NSManagedObject.

Extremely easy to use

My object

@interface User : NSObject

@property (nonatomic, strong) NSNumber *userID;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSDate *createdAt;

@end

My json

{
    "id": 14,
    "first_name": "Wernimont",
    "created_at": "2013-12-12T14:11:10Z"
}
User *user = [[BWObjectMapper shared] objectFromJSON:JSON withObjectClass:[User class]];

That's it ! JSON will be magically serialized as a user object.

Example object interface

@interface User : NSObject

@property (nonatomic, strong) NSNumber *userID;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSDate *createdAt;

@end

Mapping

[BWObjectMapping mappingForObject:[User class] block:^(BWObjectMapping *mapping) {
	[mapping mapPrimaryKeyAttribute:@"id" toAttribute:@"userID"];
	[mapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
	[mapping mapAttributeFromArray:@[@"name"]];
	[mapping mapAttributeFromDictionary:@{@"created_at" : @"createdAt"}];    
  
	[[BWObjectMapper shared] registerMapping:mapping withRootKeyPath:@"user"];
}];

At the last line we register the mapping and give a root key path. You don't need to have one, but if not the mapper will not be able to guess which mapping class to use.

Magic mapping

By default if you follow the following naming convention you don't need to manually set the mapping

json key -> Model attribute name

name -> name

id -> postID

user_name -> userName

Above example become

[BWObjectMapping mappingForObject:[User class] block:^(BWObjectMapping *mapping) {
	[mapping mapPrimaryKeyAttribute:@"id" toAttribute:@"userID"];
	[mapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
	[mapping mapAttributeFromArray:@[@"name"]];
	[mapping mapAttributeFromDictionary:@{@"created_at" : @"createdAt"}];    
  
	[[BWObjectMapper shared] registerMapping:mapping withRootKeyPath:@"user"];
}];

Or even shorter

[[BWObjectMapper shared] registerMappingForClass:[User class] withRootKeyPath:@"user"];

Object creation

[[BWObjectMapper shared] objectWithBlock:^id(Class objectClass, NSString *primaryKey, id primaryKeyValue, id JSON, id userInfo) {
	return [[objectClass alloc] init];
}];

The json

{
	"user": [{
		"id": 1,
		"first_name": "Bruno",
		"created_at": "2012-08-10T06:12:28Z"
	}]
}

Default parsing date format is Rails format.

Map json to object

NSArray *objects = [[BWObjectMapper shared] objectsFromJSON:JSON];

Because the JSON contain a root key path, the mapping automatically discover.

All mapping methods

- (NSArray *)objectsFromJSON:(id)JSON withMapping:(BWObjectMapping *)mapping;

- (NSArray *)objectsFromJSON:(id)JSON withObjectClass:(Class)objectClass;

- (NSArray *)objectsFromJSON:(id)JSON;

- (id)objectFromJSON:(id)JSON withMapping:(BWObjectMapping *)mapping;

- (id)objectFromJSON:(id)JSON withMapping:(BWObjectMapping *)mapping existingObject:(id)object;

- (id)objectFromJSON:(id)JSON withObjectClass:(Class)objectClass;

- (id)objectFromJSON:(id)JSON withObjectClass:(Class)objectClass existingObject:(id)object;

- (id)objectFromJSON:(id)JSON;

- (id)objectFromJSON:(id)JSON existingObject:(id)object;

Note about date format

If you don't use Rails date format you have two options:

  1. Specify global date format

    [objectMapping mapKeyPath:@"created_at" toAttribute:@"createdAt" dateFormat:@""];

  2. Custom date format on each attribute.

    [[BWObjectMapper shared] setDefaultDateFormat:@""];

Handling relation

Let's suppose you have a JSON like this:

{
    "model": "HB20",
    "year": "2013",
    "engine": {
        "type": "v8"
    },
    "wheels": [
        {
            "id": "123123123",
            "type": "16"
        },
        {
            "id": "1234",
            "type": "17"
        }
    ]
}
  • First define your models:
@interface Car : NSObject

@property (nonatomic, copy)   NSString *model;
@property (nonatomic, copy)   NSString *year;
@property (nonatomic, strong) Engine   *engine;
@property (nonatomic, strong) NSArray  *wheels;

@end

@interface Engine : NSObject

@property (nonatomic, copy) NSString *type;

@end

@interface Wheel : NSObject

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *size;

@end
  • After this what you need to do is define their mappings in somewhere like this:
#import "MappingProvider.h"
#import "Car.h"
#import "Engine.h"
#import "Wheel.h"

@implementation MappingProvider

+ (BWObjectMapping *)carMapping
{
    return [BWObjectMapping mappingForObject:[Car class] block:^(BWObjectMapping *mapping) {
        [mapping mapAttributeFromArray:@[@"model", @"year"]];
        [mapping hasOneWithRelationMapping:[self engineMapping] fromKeyPath:@"engine"];
        [mapping hasManyWithRelationMapping:[self wheelMapping] fromKeyPath:@"wheels"];
    }];
}

+ (BWObjectMapping *)engineMapping
{
    return [BWObjectMapping mappingForObject:[Engine class] block:^(BWObjectMapping *mapping) {
        [mapping mapAttributeFromArray:@[@"type"]];
    }];
}

+ (BWObjectMapping *)wheelMapping
{
    return [BWObjectMapping mappingForObject:[Wheel class] block:^(BWObjectMapping *mapping) {
        [mapping mapAttributeFromArray:@[@"size"]];
        [mapping mapAttributeFromDictionary:@{
            @"id": @"identifier"
         }];
    }];
}
  • And to instanciate the root object you can do this:
Car *car = [[BWObjectMapper shared] objectFromJSON:carJSON withMapping:[MappingProvider carMapping]];

Installation

Manual

Copy BWObjectMapper dir into your project.

CocoaPods

pod 'BWObjectMapping', :git => "https://github.com/brunow/BWObjectMapping.git", :tag => "0.5.1"

Carthage

github "brunow/BWObjectMapping" ~> 0.5.1

ARC

BWObjectMapper is ARC only.

Thanks

Big thanks to lucasmedeirosleite that added hasMany and hasOne relation.

Contact

Bruno Wernimont

Bitdeli Badge

More Repositories

1

BWStatusBarOverlay

BWStatusBarOverlay is a custom status bar overlay window.
Objective-C
328
star
2

NoteItIOS

Ios markdown notebook
Objective-C
292
star
3

FormKit.m

Not maintained anymore
Objective-C
272
star
4

BaseKit

BaseKit is a set of IOS class to make your life easier.
Objective-C
265
star
5

BWTitlePagerView

Recreate the Twitter navigation controller pager.
Objective-C
114
star
6

TableKit.m

Create table view with blocks based syntax.
Objective-C
50
star
7

ITWLoadingPanel

Drop in classes for adding a download info panel
Objective-C
15
star
8

BWAFNetworking

Restfull http library inspired by the great RestKit and based on top of Afnetworking.
Objective-C
14
star
9

BWSelectViewController

Controller that show a list of items that can be selectable.
Objective-C
12
star
10

BWObjectSerializer

Small library that transform an object into a dictionary that can be send to a web service.
Objective-C
10
star
11

SwiftUILocalNotification

How to embed a SwiftUI view in Notification Content Extension
Swift
7
star
12

BWLongTextViewController

Simple IOS controller that show a full screen textView with keyboard
Objective-C
6
star
13

BWObjectRouter

Small library that generate route with static or dynamic data.
Objective-C
5
star
14

sammy-rails

This gem provides a generator to install Sammy and his plugin into your Rails 3 application.
Ruby
5
star
15

DroneVR

iOS Parrot bebop app
Swift
2
star
16

raicebook

This gem add a lot of usefull facebook helper for rails
Ruby
2
star
17

BWAttributedString

BWAttributedString
Objective-C
1
star
18

easymin

Ruby
1
star
19

ITWSyncController

ITWSyncController is a class that help you to manage app synchronization.
Objective-C
1
star
20

wadmin

Free html5 css3 admin template
1
star
21

rails-template

Rails template script for creating rails app
Ruby
1
star
22

TTRemoteDataExample

Demonstrate how to load image, XML and JSON with Three20 for the iPhone
Objective-C
1
star
23

bwafnetworking_server

Ruby
1
star
24

updated_after

Ruby
1
star
25

TTTableViewControllerExample

Simple example of table view controller with three20
Objective-C
1
star
26

TTPostControllerExample

Demonstrate how to play with TTPostController composer
Objective-C
1
star