Using the following models as examples, what are the best practices of handling polymorphism within JSONModel?
@interface GameModel : JSONModel
@property (nonatomic, assign) long id;
@property (nonatomic, assign) NSArray<GameEventModel> *events;
/*
...
*/
@end
@interface GameEventModel : JSONModel
@property (nonatomic, assign) long long timestamp;
/*
...
*/
@end
@interface GameTouchEventModel : GameEventModel
@property (nonatomic, assign) CGPoint point;
/*
...
*/
@end
When GameModel is initiated with a JSON string of {id:1, events:[{point:{x:1, y:1}, timestamp:...}]}
JSONModel will use the GameEventModel
and ignore the point
property.
Would it be better to use a generic GameEventModel
which contains a type
property and info
property such as...
@interface GameTouchEventModel : GameEventModel
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSDictionary *info;
@end
And therefore the model could accept JSON as {id:1, events:[{ type:"GameTouchEventModel", info:{ point:{x:1, y:1}, timestamp:... } }]}
The problem with this approach is harder to read code and no compiler warnings/errors amongst others.
Is there no way to use polymorphic models in JSONModel?