Search code examples
javakotlingsonjson-deserialization

Gson: Deserialize and "extend" a huge JSON


I have a huge Trello's JSON which a I need transform in another JSON with different structure. In order to do that I use Gson for deserializing and serializing in the output format but the input JSON has keys with just the ids values who actually are defined as elements in others JSON parts in a "decoupled" way. That structure makes difficult deserialice an link the objects without codify an specific and long deserializer. Any way to do this in an easy way?

Input JSON:

{
  "id": "5edf568cd37bf379d96640b3",
  "name": "Trello board",
  // A LOT ENTRIES...
  "lists": [{
      "id": "5edf568dabadcf538c546c3c",
      "name": "TO DO"
  },{
      "id": "5edf568dfd9cb03d1c346cbf",
      "name": "IN PROGRESS" 
  }],
  "cards": [{
    "desc": "Card 1"
    "idList": "5edf568dfd9cb03d1c346cbf", // This should be deserialized with the specific "lists" element
     // A LOT ENTRIES...
  },{
    "desc": "Card 2"
    "idList": "5edf568dfd9cb03d1c346cbf",
     // A LOT ENTRIES...
  }],
  // A LOT ENTRIES...
}

Current POJOs:

data class TrelloList(val id: String, val name: String)
data class Card(val desc: String, val idList: String)

Desired Card POJO

data class Card(val desc: String, val list: TrelloList)

How can I extend or convert that ids into the related objects without codify a complete custom deserializer?


Solution

  • At the end I had to write a custom deserializer. It's a pity Gson does not count with an structural Jsons adapter or something to make joins between elements with same ids.