Context:
// JSON
"Name_Field" : {
"param_1":"value_1",
"param_2":"value_2"
}
// Class
class Field {
name
param1
param2
}
// Mapping Functionality
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[mapping addAttributeMappingsFromDictionary:@{
@"(name).param_1":@"param1",
@"(name).param_2":@"param2"
}];
Problem:
I am currently working with the above JSON / Class / Mapping code. I have been using this for a while and everything has been working as expected.
Today I have run into the scenario where the key in the JSON contains parenthesis and causes the mapping to fail. Is there a way I can get this to work?
Thanks!
Example:
"Name (haha this will break)" : {
"param_1":"value_1",
"param_2":"value_2"
}
Solution:
Update the
RKStringByReplacingUnderscoresWithBracesmethod inRKPropertyMappingto not replace the parenthesis with braces and then be sure to use braces in your attribute mappings.Explanation:
https://github.com/RestKit/RestKit/wiki/Object-mapping#handling-dynamic-nesting-attributes
In the above RestKit documentation when you are attempting to map a key to property you are instructed to use
addAttributeMappingFromKeyOfRepresentationToAttributeand then map the nested keys using parentheses to denote your property followed by a '.' and your nested key.In RestKit after it performs the map from the
addAttributeMappingFromKeyOfRepresentationToAttributeattribute mapping it loops through all of your defined attribute mappings to replace your placeholder with the actual value from the key so the additional mapping operations can take place. During this process it creates a newRKPropertyMappingobject and sets it's sourceKeyPath and destinationKeyPath. The property setters for these properties take the value and replace the parenthesis with braces and then RestKit does not find the incorrect mappings for the value with braces.Example Context:
Example Mapping:
Example Mapping With Parenthesis: