I'm currently trying to simplify my Obj-C Model but it seems the json data makes it a bit more complex than I thought. I can't get the array data to convert to some booleans.
As far as I know it's impossible to use the Frameworks JSONKeyPathsByPropertyKey to define that the key is something like this: @"gsRightIPServerStart", @"rights.name['RIGHT_IP_SERVERSTART'].granted" or at least the posebility to access the serializing dicitonary within the used class eg. my FNPServer class.
This is a part of the JSON response I have to use:
...,
isowner": false,
"rights":
[
{
"name": "RIGHT_IP_SERVERSTART",
"granted": true
},
{
"name": "RIGHT_IP_SERVERREMOVE",
"granted": true
},
{
"name": "RIGHT_IP_SWITCHCONFIGMODE",
"granted": true
},
{
"name": "RIGHT_IP_SERVERCREATE",
"granted": true
}
]}
It's an simple Array that could be easily represented as Boolean. In this case some NSNumbers (CoreData):
@interface FNPServer : MTLModel <MTLJSONSerializing>
...
@property (nonatomic, strong) NSNumber * gsRightIPServerStart;
@property (nonatomic, strong) NSNumber * gsRightIPServerStop;
@property (nonatomic, strong) NSNumber * gsRightIPServerRestart;
@property (nonatomic, strong) NSNumber * gsRightIPServerModify;
@end
I can't find a solution to get the source dict while or after generating this Object to handle the conversion by myself.
I don't want to make serveral different classes and check the property to generate an array full of full fledged Objects for a simple yes or no question. It should work without "help from outside" otherwise i have to check a bunch of requests if the data is present or not.
Thanks
You cannot directly give the keys required to JSONKeyPathsByPropertyKey in your case. But you can set 'rights' as the JSON Key Path for the properties that need the values within 'rights' array. Then write custom 'propertyNameJSONTransformer' methods within FNPServer.
Here is an example where I consider that 'responseDictionary' contains 'rights' key.
[MTLJSONAdapter modelOfClass:FNPServer.class fromJSONDictionary:responseDictionary error:&error];
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"gsRightIPServerStart" : @"rights",
...
};
}
+ (NSValueTransformer *)gsRightIPServerStartJSONTransformer {
return [MTLValueTransformer transformerUsingForwardBlock:^id(NSArray *rights, BOOL *success, NSError *__autoreleasing *error) {
// here you have the 'rights' as an array
// you can get the right value for gsRightIPServerStart within 'rights'
// then return it by converting to an NSNumber
}];
}