I'm using RestKit http://restkit.org for iOS. I have an object and an object mapping defined and can use that to send and receive data with the server. However for my mapped objects I'd like to the -description method to return the JSON mapping for logging to the console.
How to map the object to a string?
The object mapping has now changed substantially as of RestKit ObjectMapping 2.0 in the newer versions of Reskit, and @bradgonesurfing's answer won't work in these newer versions.
You now need to use RKObjectParameterization to perform the object serialization as follows:
RKObjectMapping *itemMapping = [RKObjectMaping mappingForClass:[Item class]];
/* Your object mapping definition for the Item class goes here */
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:itemMapping.inverseMapping objectClass:[Item class] rootKeyPath:nil method:RKRequestMethodPOST];
NSDictionary *dict = [RKObjectParameterization parametersWithObject:item requestDescriptor:requestDescriptor error:nil];
NSData *jsonData = [RKMIMETypeSerialization dataFromObject:dict MIMEType:RKMIMETypeJSON error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With