Search code examples
iosrestkit

using RestKit to serialize an object with a subobject [POSTing]


I have the following objects setup:

RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]];

geoPointMapping.setDefaultValueForMissingAttributes = YES;
[geoPointMapping mapKeyPathsToAttributes:
    @"longitude", @"longitude",
    @"latitude", @"latitude",
    nil];
[objectManager.mappingProvider registerMapping:geoPointMapping withRootKeyPath:@"geometry"];

RKObjectMapping* criteriaMapping = [RKObjectMapping mappingForClass:[CRCriteria class]];

criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
    @"type", @"type",
    @"geometry", @"geometry",
    @"fromDate", @"fromDate",
    @"toDate", @"toDate",
    @"radius", @"radius",
    nil];
[objectManager.mappingProvider registerMapping:criteriaMapping withRootKeyPath:@"criteria"];

But when I try and send the query (with a geometry object). I keep getting this error:

error received Error Domain=JKErrorDomain Code=-1 "Unable to serialize object class CRGeoPoint."


Solution

  • Inbound and outbound mapping information is handled separately by RKObjectMappingProvider. You've configured the inbound mapping in that code (downloading the data from your server). But--as pointed out by Shane Zatezalo on the RestKit group--you also need to add a serialization mapping for RestKit to know how to turn your objects into JSON (or whatever other serialization format you might be using).

    Check out the Object Mapping tutorial's section on Object Serialization. And take a look at the source for the mapping provider to reassure yourself that these things are separate.