trying to serialize a query with a subobject

164 views
Skip to first unread message

Warner Onstine

unread,
Mar 12, 2012, 12:39:40 PM3/12/12
to RestKit
So, I have a Criteria object for a query that I want to send to the
server as json. I have these 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."

I've tried mapping the root key on the geometry to criteria.geometry
with no luck. Any hints as to what I'm doing incorrectly?

Thanks!

-warner

Shane Zatezalo

unread,
Mar 12, 2012, 8:00:32 PM3/12/12
to res...@googlegroups.com
I don't know off hand if you have to declare a serializationMapping in your mapping provider or not, so I would try adding one:

    RKObjectMapping *geoPointSerializationMapping = [RKObjectMapping 
                                                   mappingForClass:[CRGeoPoint class]]; 
    [geoPointSerializationMapping mapKeyPath:@"latitude" 
                               toAttribute:@"latitude"]; 
    [geoPointSerializationMapping mapKeyPath:@"longitude" 
                               toAttribute:@"longitude"];
    [objectManager.mappingProvider 
     setSerializationMapping: geoPointSerializationMapping forClass:[CRGeoPoint class]];


Shane

Warner Onstine

unread,
Mar 12, 2012, 8:46:02 PM3/12/12
to res...@googlegroups.com
I thought that that is what this part does:

[geoPointMapping mapKeyPathsToAttributes:
@"longitude", @"longitude",
@"latitude", @"latitude",
nil];

-warner

Warner Onstine

unread,
Mar 14, 2012, 4:24:30 PM3/14/12
to res...@googlegroups.com
So, Shane had part of the answer but I managed to find the rest in the
docs thanks to Sixten.

Here is the solution to the problem I was having, hopefully it will
help someone else.

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

geoPointMapping.setDefaultValueForMissingAttributes = YES;

[objectManager.mappingProvider registerMapping:geoPointMapping
withRootKeyPath:@"geometry"];

// Build a serialization mapping by inverting our object mapping.
Includes attributes and relationships
RKObjectMapping* geoPointSerializationMapping = [geoPointMapping
inverseMapping];
// You can customize the mapping here as necessary --
adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider


setSerializationMapping:geoPointSerializationMapping
forClass:[CRGeoPoint class]];

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

criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
@"type", @"type",

@"fromDate", @"fromDate",
@"toDate", @"toDate",
@"radius", @"radius",
nil];

[criteriaMapping mapKeyPath:@"geometry" toRelationship:@"geometry"
withMapping:geoPointMapping];


[objectManager.mappingProvider registerMapping:criteriaMapping
withRootKeyPath:@"criteria"];

// Build a serialization mapping by inverting our object mapping.
Includes attributes and relationships
RKObjectMapping* criteriaSerializationMapping = [criteriaMapping
inverseMapping];
// You can customize the mapping here as necessary --
adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider
setSerializationMapping:criteriaSerializationMapping
forClass:[CRCriteria class]];

Reply all
Reply to author
Forward
0 new messages