Izaac Brånn
Izaac Brånn

Reputation: 94

Retrofit - Json response can contain different fields depending on type

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?

Upvotes: 1

Views: 834

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44844

As per https://www.baeldung.com/retrofit

Retrofit works by modeling over a base URL and by making interfaces return the entities from the REST endpoint.

For simplicity purposes we're going to take a small part of the JSON by modeling our User class that is going to take the values when we have received them:

public class User {
    private String login;
    private long id;
    private String url;
    // ...

    // standard getters an setters

}

We can see that we're only taking a subset of properties for this example. Retrofit won't complain about missing properties – since it only maps what we need, it won't even complain if we were to add properties that are not in the JSON.

Upvotes: 1

Related Questions