Reputation: 1046
I'm trying to serialize an NSArray of objects.
The object serializes as:
{"age":1,"name":"Foo"}
If I have an NSArray Containing these objects, it should serialize as:
[{"age":1,"name":"Bob"},{"age":4,"name":"Sally"},{"age":2,"name":"Jill"}]
However, when I serialize it directly via RestKit, I get the following:
{"age":[1,3,2],"name":["Sally","Jack","Bob"]}
When I inspect the RKMIMETypeSerialization, I see the following: (which would match the json output)
Parameters: {
age = (
1,
3,
2
);
name = (
Sally,
Jack,
Bob
);
}
I'm sure that I'm just doing something really silly, I have been playing with it can't figure it out.
Here is my mapping logic
+ (RKObjectMapping *)mapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Item class]];
[mapping addAttributeMappingsFromDictionary:@{
@"name" : @"name",
@"age" : @"age"
}];
return mapping;
}
And here is the logic doing the array serialization:
+ (NSString *)JSONStringFromArray:(NSArray *)array {
RKObjectMapping *mapping = [Item mapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
NSError *error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:array requestDescriptor:requestDescriptor error:&error];
// Serialize the object to JSON
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
length:[JSON length] encoding:NSUTF8StringEncoding];
return jsonString;
}
Here are test classes: Item.h
#import <Foundation/Foundation.h>
@interface Item : NSObject
@property(nonatomic, strong) NSString *name;
@property(nonatomic) int age;
+ (void)testJsonSerialization;
+ (void)testJsonArraySerialization;
+ (Item *)itemWithName:(NSString *)string age:(int)age;
@end
Item.m
#import "Item.h"
#import <RestKit.h>
@implementation Item
+ (RKObjectMapping *)mapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Item class]];
[mapping addAttributeMappingsFromDictionary:@{
@"name" : @"name",
@"age" : @"age"
}];
return mapping;
}
- (NSString *)JSONString {
RKObjectMapping *mapping = [Item mapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
NSError *error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:self requestDescriptor:requestDescriptor error:&error];
// Serialize the object to JSON
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
length:[JSON length] encoding:NSUTF8StringEncoding];
return jsonString;
}
+ (NSString *)JSONStringFromArray:(NSArray *)array {
RKObjectMapping *mapping = [Item mapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
NSError *error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:array requestDescriptor:requestDescriptor error:&error];
// Serialize the object to JSON
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
length:[JSON length] encoding:NSUTF8StringEncoding];
return jsonString;
}
+ (void)testJsonSerialization {
Item *item = [Item itemWithName:@"Foo" age:1];
NSString *itemJSON = [item JSONString];
NSLog(@"ItemJSON:\n%@\n\n", itemJSON);
}
+ (void)testJsonArraySerialization {
NSArray *items = @[[Item itemWithName:@"Sally" age:1], [Item itemWithName:@"Jack" age:3], [Item itemWithName:@"Bob" age:2]];
NSString *itemJSON = [Item JSONStringFromArray:items];
NSLog(@"ItemArrayJSON:\n%@\n\n", itemJSON);
}
+ (Item *)itemWithName:(NSString *)name age:(int)age {
Item *item = [[Item alloc] init];
item.name = name;
item.age = age;
return item;
}
@end
To execute Have RestKit pod installed and then issue the following:
[Item testJsonSerialization];
[Item testJsonArraySerialization];
Note, maybe I'm not configuring the mapping correctly for the array serialization. Though this mapper works perfectly for deserializing the targeted json text up above.
Upvotes: 0
Views: 813
Reputation: 119031
RKObjectParameterization
is expected to be used to serialise individual instances of the target of your object mapping and it statically serialises the instance into an NSMutableDictionary
. You can't write a mapping to use with RKObjectParameterization
that will serialise an array of objects to an array of dictionaries. You would need to do this yourself by looping over your array.
Upvotes: 1