使用 RestKit 序列化带有子对象的对象 [POSTing]
Posted
技术标签:
【中文标题】使用 RestKit 序列化带有子对象的对象 [POSTing]【英文标题】:using RestKit to serialize an object with a subobject [POSTing] 【发布时间】:2012-03-13 21:16:58 【问题描述】:我有以下对象设置:
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"];
但是当我尝试发送查询(使用几何对象)时。我一直 收到此错误:
收到错误 Error Domain=JKErrorDomain Code=-1 "无法序列化 对象类 CRGeoPoint。”
【问题讨论】:
【参考方案1】:入站和出站映射信息由RKObjectMappingProvider
分别处理。您已经在该代码中配置了入站映射(从您的服务器下载数据)。但是——RestKit 组中的as pointed out by Shane Zatezalo——您还需要为 RestKit 添加一个序列化映射,以了解如何将您的对象转换为 JSON(或您可能使用的任何其他序列化格式)。
查看对象序列化的Object Mapping tutorial's 部分。看看source for the mapping provider,让自己放心,这些东西是分开的。
【讨论】:
感谢指向文档的指针,您和 Shane 都部分正确。但是医生指出了完整答案的方法。我不仅需要创建逆映射,还需要指定用于关系的映射。 是的,我对此困惑了很长时间。最终我意识到我需要向映射提供者注册映射(由资源路径键入),并分别向路由器注册类资源路径。【参考方案2】:OP 的解决方案。
解决方案有两个。首先,我必须指定下面 Sixten 指出的逆映射,其次我必须指定用于 CRCriteria 对象上的 CRGeoPoint 类的映射。代码如下:
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]];
【讨论】:
以上是关于使用 RestKit 序列化带有子对象的对象 [POSTing]的主要内容,如果未能解决你的问题,请参考以下文章
如何将展平的对象序列化回服务器,在 RestKit 中未展平
如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?