J'essaie de sérialiser un objet NSArray.RestKit 0.20 Problèmes de sérialisation NSArray
L'objet sérialise comme:
{"age":1,"name":"Foo"}
Si j'ai un NSArray contenant ces objets, il faut sérialisation comme:
[{"age":1,"name":"Bob"},{"age":4,"name":"Sally"},{"age":2,"name":"Jill"}]
Cependant, quand je sérialiser directement via RestKit, je reçois le suivant:
{"age":[1,3,2],"name":["Sally","Jack","Bob"]}
Lorsque j'inspecte la RKMIMETypeSerialization, je vois le llowing: (ce qui correspondrait à la sortie JSON)
Parameters: {
age = (
1,
3,
2
);
name = (
Sally,
Jack,
Bob
);
}
Je suis sûr que je vais juste faire quelque chose de vraiment stupide, je joue avec elle ne peut pas comprendre.
Voici ma logique de cartographie
+ (RKObjectMapping *)mapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Item class]];
[mapping addAttributeMappingsFromDictionary:@{
@"name" : @"name",
@"age" : @"age"
}];
return mapping;
}
Et est ici la logique de faire la sérialisation tableau:
+ (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;
}
Voici les classes de test: 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
Pour exécuter pod RestKit avez installé puis exécutez la commande suivante:
[Item testJsonSerialization];
[Item testJsonArraySerialization];
Remarque, peut-être que je ne suis pas configurer le mappage correctement pour la sérialisation de tableau. Bien que ce mapper fonctionne parfaitement pour désérialiser le texte json ciblé ci-dessus.
Vérifiez la sortie de 'RKObjectParameterization parametersWithObject:', est-ce ce que vous attendez? – Wain
Les paramètres ne sont pas ce que j'aurais attendu, ils correspondent à peu près à la sortie de JSON. – GreenKiwi
Je m'attendrais à voir qu'il s'agissait d'un tableau d'objets, où chaque objet était un dictionnaire avec un ensemble de propriétés. – GreenKiwi