Alternative keys for the same property
Opened this issue · 1 comments
xiaolinghuEr commented
Your JSON dictionary:
{
"name": "ObjectMapper",
}
{
"userName" : "John Doe"
}
Your model:
class User : Mappable{
var userName : String?
func mapping(map: Map) {
userName <- map["userName"]
}
}
What you expected:
Is there any way to let ObjectMapper support multiple keys for the same property in the Model ?
Because the server APIs of different teams may return different fields to represent the same property in App. So there is a strong demand to support this scene. YYModel of Objective-C also support this function.
func mapping(map: Map) {
userName <- map["userName", "user_name"]
}
gcharita commented
@xiaolinghuEr you can work your way around this by using something like this in your implementation of mapping(map:)
function:
func mapping(map: Map) {
userName <- map["userName"]
if userName == nil {
userName <- map["user_name"]
}
}