Added helper functions to RKObjectSerializer to map local objects into json

This commit is contained in:
Duncan Lewis
2011-06-30 12:28:23 -04:00
parent 7053a8ca97
commit 403bf29f57
3 changed files with 43 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
//
// RKObjectMappingOperation.m
// RestKit

View File

@@ -26,6 +26,26 @@
+ (id)serializerWithObject:(id)object mapping:(RKObjectMapping*)mapping;
- (id)initWithObject:(id)object mapping:(RKObjectMapping*)mapping;
/**
Return a serialized representation of the source object by applying an object mapping
with a target object type of NSMutableDictionary. The serialized object will contain attributes
and relationships composed of simple KVC compliant Cocoa types.
*/
- (NSMutableDictionary*)serializedObject:(NSError**)error;
/**
Return a serialized representation of the source object by mapping it into a NSMutableDictionary and
then encoding it into the destination MIME Type via an instance of RKParser that is registered
for the specified MIME Type
*/
- (NSString*)serializedObjectForMIMEType:(NSString*)MIMEType error:(NSError**)error;
/**
Return a request serialization for the source object by mapping it to an NSMutableDictionary, encoding
the data via a parser into the specified MIME Type, and wrapping it into a serializable format that can
be used as the params of an RKRequest or RKObjectLoader
*/
- (id<RKRequestSerializable>)serializationForMIMEType:(NSString*)mimeType error:(NSError**)error;
@end

View File

@@ -43,7 +43,8 @@
[super dealloc];
}
- (id<RKRequestSerializable>)serializationForMIMEType:(NSString*)MIMEType error:(NSError**)error {
// Return it serialized into a dictionary
- (id)serializedObject:(NSError**)error {
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
RKObjectMappingOperation* operation = [RKObjectMappingOperation mappingOperationFromObject:_object toObject:dictionary withObjectMapping:_mapping];
operation.delegate = self;
@@ -59,16 +60,31 @@
dictionary = [NSMutableDictionary dictionaryWithObject:dictionary forKey:_mapping.rootKeyPath];
}
if ([MIMEType isEqualToString:RKMIMETypeFormURLEncoded]) {
// Dictionaries are natively RKRequestSerializable as Form Encoded
return dictionary;
} else {
return dictionary;
}
- (id)serializedObjectForMIMEType:(NSString*)MIMEType error:(NSError**)error {
// TODO: This will fail for form encoded...
id serializedObject = [self serializedObject:error];
if (serializedObject) {
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
NSString* string = [parser stringFromObject:dictionary error:error];
NSString* string = [parser stringFromObject:serializedObject error:error];
if (string == nil) {
return nil;
}
return string;
}
return nil;
}
- (id<RKRequestSerializable>)serializationForMIMEType:(NSString*)MIMEType error:(NSError**)error {
if ([MIMEType isEqualToString:RKMIMETypeFormURLEncoded]) {
// Dictionaries are natively RKRequestSerializable as Form Encoded
return [self serializedObject:error];
} else {
NSString* string = [self serializedObjectForMIMEType:MIMEType error:error];
NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [RKRequestSerialization serializationWithData:data MIMEType:MIMEType];
}