Search code examples
objective-cjsongithub-mantle

Deserialize Mantle object using two JSON formats


Is there a way to construct a mantle object using two different JSON objects? For example, say there is an Address class and the following two JSON formats:

{
    "street: "s",
    "city" : "city",
    "state" : "state",
    "zipcode" "12345"
}

{
    "street_one: "s1",
    "street_two: "s2",
    "apartment" : "apt 1",
    "city" : "city",
    "state" : "state",
    "zip" "12345"
}

[MTLJSONAdapter modelOfClass:[Address class] fromJSONDictionary:JSONDictionary error:&error];

Is there somewhere in MTLJSONAdapter to recognize two formats and assign or serialize properties based on them?

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    if (isJsonV2) {
        // do new mapping
    }
    else {
        // do original mapping
    }
}

Hope to do something like above, or anything that allows conditionally mapping to the object.


Solution

  • Mantle doesn't support this, but you could use a subclass for V2 which has the extra street entry, or use a protocol to encapsulate the shared behaviour.