7

How can I use Github Mantle to choose a property class based on another property in the same class? (or in the worse case another part of the JSON object).

For example if i have an object like this:

{
  "content": {"mention_text": "some text"},
  "created_at": 1411750819000,
  "id": 600,
  "type": "mention"
}

I want to make a transformer like this:

+(NSValueTransformer *)contentJSONTransformer {
    return [MTLValueTransformer transformerWithBlock:^id(NSDictionary* contentDict) {
          return [MTLJSONAdapter modelOfClass:ETMentionActivityContent.class fromJSONDictionary:contentDict error:nil];
    }];
}

But the dictionary passed to the transformer only includes the 'content' piece of the JSON, so I don't have access to the 'type' field. Is there anyway to access the rest of the object? Or somehow base the model class of 'content' on the 'type'?

I have previously been forced to do hack solutions like this:

+(NSValueTransformer *)contentJSONTransformer {
    return [MTLValueTransformer transformerWithBlock:^id(NSDictionary* contentDict) {
        if (contentDict[@"mention_text"]) {
            return [MTLJSONAdapter modelOfClass:ETMentionActivityContent.class fromJSONDictionary:contentDict error:nil];
        } else {
            return [MTLJSONAdapter modelOfClass:ETActivityContent.class fromJSONDictionary:contentDict error:nil];
        }
    }];
}
pj4533
  • 1,701
  • 4
  • 17
  • 38

2 Answers2

5

You can pass the type information by modifying the JSONKeyPathsByPropertyKey method:

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
        NSStringFromSelector(@selector(content)) : @[ @"type", @"content" ],
    };
}

Then in contentJSONTransformer, you can access the "type" property:

+ (NSValueTransformer *)contentJSONTransformer 
{
    return [MTLValueTransformer ...
        ...
        NSString *type = value[@"type"];
        id content = value[@"content"];
    ];
}
chourobin
  • 4,004
  • 4
  • 35
  • 48
0

I've had a similar issue, and I suspect my solution isn't much better than yours.

I have a common base class for my Mantle objects, and after each is constructed, I call a configure method to give them chance to set up properties that are dependent on more than one "base" (== JSON) property.

Like this:

+(id)entityWithDictionary:(NSDictionary*)dictionary {

    NSError* error = nil;
    Class derivedClass = [self classWithDictionary:dictionary];
    NSAssert(derivedClass,@"entityWithDictionary failed to make derivedClass");
    HVEntity* entity = [MTLJSONAdapter modelOfClass:derivedClass fromJSONDictionary:dictionary error:&error];
    NSAssert(entity,@"entityWithDictionary failed to make object");
    entity.raw = dictionary;
    [entity configureWithDictionary:dictionary]; // Give the new entity a chance to set up derived properties
    return entity;
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
Jane Sales
  • 13,526
  • 3
  • 52
  • 57