I'm working on an API wrapper using retrofit but I'm having issues creating the returned object classes since some items from the same endpoint can contain different type of fields.
For example, the returning JSON can look like this for a basic item:
{
"name": "Box of Honed Splint Armor",
"description": "Double-click to unpack a full set of level 50 armor.",
"type": "Container",
"level": 0,
"rarity": "Masterwork",
"vendor_value": 86,
"game_types": [
"Wvw",
"Dungeon",
"Pve"
],
"flags": [],
"restrictions": [],
"id": 9000,
"chat_link": "[&AgEoIwAA]",
"icon": "https://render.guildwars2.com/file/72D04673660ECB7FD904680D487030A41106F952/63218.png",
"details": {
"type": "Default"
}
}
Or like this for a more detailed item:
{
"name": "Strong Soft Wood Longbow of Fire",
"description": "",
"type": "Weapon",
"level": 44,
"rarity": "Masterwork",
"vendor_value": 120,
"default_skin": "3942",
"game_types": [ "Activity", "Dungeon", "Pve", "Wvw" ],
"flags": [ "SoulBindOnUse" ],
"restrictions": [],
"id": 28445,
"chat_link":"[&AgEdbwAA]",
"icon": "https://render.guildwars2.com/file/C6110F52DF5AFE0F00A56F9E143E9732176DDDE9/65015.png",
"details": {
"type": "LongBow",
"damage_type": "Physical",
"min_power": 385,
"max_power": 452,
"defense": 0,
"infusion_slots": [],
"infix_upgrade": {
"attributes": [
{ "attribute": "Power", "modifier": 62 },
{ "attribute": "Precision", "modifier": 44 }
]
},
"suffix_item_id": 24547,
"secondary_suffix_item_id": ""
}
}
The details field can differ very much between different item types, and I'd prefer to have an easy way to access the fields corresponding to each type without the wrapper consumer has to type cast to a type-specific subclass of an empty base class like other wrappers require, like one of the best wrappers do.
Is there some way to hide fields depending on the type of the parent or something like that so I can include all possible fields in the item class?
Any suggestions?