Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Created September 2, 2011 20:05
Show Gist options
  • Save shyam-habarakada/1189737 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/1189737 to your computer and use it in GitHub Desktop.
mapping options

Response format A:

[
  {"id_str":"1","id":1,"last_name":"Johnson","date_of_birth":null,"display_name":"Stephanie Johnson","first_name":"Stephanie"},
  {"id_str":"2","id":2,"last_name":"Black","date_of_birth":null,"display_name":"Joe Black","first_name":"Joe"}
]

Response format B:

[
  {"patient":{"id_str":"1","id":1,"last_name":"Johnson","date_of_birth":null,"display_name":"Stephanie Johnson","first_name":"Stephanie"}},
  {"patient":{"id_str":"2","id":2,"last_name":"Black","date_of_birth":null,"display_name":"Joe Black","first_name":"Joe"}}
]

And the mappings are ...

    RKManagedObjectMapping *patientMapping = [RKManagedObjectMapping mappingForClass:[Patient class]];
    patientMapping.primaryKeyAttribute = @"id";
    [patientMapping mapKeyPathsToAttributes:@"id_str",@"id",
     @"first_name",@"firstname",
     @"last_name",@"lastname",
     nil];
    
    [objectManager.mappingProvider setMapping:patientMapping forKeyPath:@"patient"];
    
    RKManagedObjectMapping *patientSerializationMapping = (RKManagedObjectMapping *)[patientMapping inverseMapping];
    
    [objectManager.mappingProvider setSerializationMapping:patientSerializationMapping forClass:[Patient class]];
    
    [objectManager.router routeClass:[Patient class] toResourcePath:@"/patients" forMethod:RKRequestMethodPOST];

With format A, the mappings just worked. With format B, the extra level of encapsulation -- as in { "patient" : { "id" : 1, "first name" : "simon", … } } -- didn't quite work and I had to look for key paths like patient.id to get to the actual values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment