以父键作为属性且键包含括号的 RestKit 映射

RestKit mapping with parent key as attribute and key contains parenthesis

上下文:

// 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"
}];

问题:

我目前正在使用上面的 JSON / Class / 映射代码。我已经使用了一段时间,一切都按预期工作。

今天我 运行 遇到 JSON 中的键包含括号并导致映射失败的场景。有什么办法可以让它发挥作用吗?

谢谢!

示例:

"Name (haha this will break)" : {
    "param_1":"value_1",
    "param_2":"value_2"
}

我认为 RestKit 感到困惑,因为它不知道在映射时使用哪个括号。所以我的猜测是用大括号替换它们:

[mapping addAttributeMappingsFromDictionary:@{
    @"{name}.param_1":@"param1",
    @"{name}.param_2":@"param2"
}];

让我知道这是否有效。

解法:

更新 RKPropertyMapping 中的 RKStringByReplacingUnderscoresWithBraces 方法,不要用大括号替换圆括号,然后确保在属性映射中使用大括号。

// RKPropertyMapping
static NSString *RKStringByReplacingUnderscoresWithBraces(NSString *string)
{
    return string;
    //return [[string stringByReplacingOccurrencesOfString:@"(" withString:@"{"] stringByReplacingOccurrencesOfString:@")" withString:@"}"];
}


// Attribute Mappings
[mapping addAttributeMappingsFromDictionary:@{
    @"{name}.param_1":@"param1",
    @"{name}.param_2":@"param2"
}];

解释:

https://github.com/RestKit/RestKit/wiki/Object-mapping#handling-dynamic-nesting-attributes

在上面的 RestKit 文档中,当您尝试将键映射到 属性 时,系统会指示您使用 addAttributeMappingFromKeyOfRepresentationToAttribute,然后使用括号映射嵌套键以表示您的 属性后跟一个 '.'和你的嵌套键。

在 RestKit 中,在从 addAttributeMappingFromKeyOfRepresentationToAttribute 属性映射执行映射后,它会循环遍历所有已定义的属性映射,以将占位符替换为键中的实际值,以便进行其他映射操作。在此过程中,它会创建一个新的 RKPropertyMapping 对象并设置它的 sourceKeyPath 和 destinationKeyPath。这些属性的 属性 setter 获取值并用大括号替换圆括号,然后 RestKit 找不到带有大括号的值的不正确映射。

示例上下文:

// JSON   
{ "blake": {
    "email": "blake@restkit.org",
    "favorite_animal": "Monkey"
    }
}

// Class
@interface User : NSObject
@property (nonatomic, copy) NSString* email
@property (nonatomic, copy) NSString* username;
@property (nonatomic, copy) NSString* favoriteAnimal;
@end

// Mapping
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ];
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"username"];
[mapping addAttributeMappingsFromDictionary:@{
    @"(username).email": @"email",
    @"(username).favorite_animal": @"favoriteAnimal"
}];

映射示例:

// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"


// 1. The attribute mapping has been performed for 'blake' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
      sourceKeyPath = @"{username}.email"
      destinationKeyPath = @"email"

      * replaces values *
      sourceKeyPath = @"blake.email"
      destinationKeyPath = @"email"

      * creates a new RKPropertyMapping object *
      RKPropertyMapping
        - sourceKeyPath = @"blake.email"
        - destinationKeyPath = @"email"

带括号的映射示例:

// JSON   
{ "blake (a.k.a GOAT)": {
    "email": "blake@restkit.org",
    "favorite_animal": "Monkey"
    }
}

// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"


// 1. The attribute mapping has been performed for 'blake (a.k.a GOAT)' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake (a.k.a GOAT)' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
      sourceKeyPath = @"{username}.email"
      destinationKeyPath = @"email"

      * replaces values *
      sourceKeyPath = @"blake (a.k.a GOAT).email"
      destinationKeyPath = @"email"

      * creates a new RKPropertyMapping object *
      RKPropertyMapping
        - sourceKeyPath = @"blake {a.k.a GOAT}.email" // DOES NOT MATCH
        - destinationKeyPath = @"email"