Hello everyone, I have a class hierarchy like the ...
# serialization
e
Hello everyone, I have a class hierarchy like the following, and I have an API that return a list of
User
I can retrieved Users using two different end-points, The first one return
recent
users and the second one return
popular
users, for both popular and recent end-points the data returning by the API has the same structure except one thing The recent end-point return a json like this, that differ from the popular end-point at the recent field
Copy code
{
  "data": {
    "recent": { ... } // Here the difference
   }
}
And the popular end-point return a json like this, that differ from the recent end-point at the popular field
Copy code
{
  "data": {
    "popular": { ... } // Here the different 
   }
}
Here the class hierarchy
Copy code
@Serializable
data class ApiResponse(
  @SerialName("data")
  val apiData: Data
)

@Serializable
data class Data(
  @SerialName("recent") // My problem is here
  val page: Page
)

@Serializable
data class Page(
  val currentPage: Int,
  val from: Int,
  val perPage: Int,
  val results: List<User>,
)
For the
Data
class, there is a way to customize the behavior of the
SerialName
annotation to deal both with recent and popular name as the
Page
class has the same structure for recent and popular end-point without having to duplicate stuff ??
m
@Eric Ampire [MOD] you can add "popular" as second param to your
Data
data class and make them both default to null
depending on the API, appropriate field will be having your response data
j
there is an annotation to add multiple alternative names
1
e
What's the name of that annotation ?
j
JsonNames I think
r

https://youtu.be/698I_AH8h6s?t=326

e
Thanks @Ravi This is exactly what I was looking for.