https://kotlinlang.org logo
Title
k

KV

07/15/2021, 9:17 AM
Hello, How to implement this below classes in better way? Class 1 has - DataResponse, Meta Response and List<IncludedResponse>
@Serializable
data class ApiResponse<T>(
    @SerialName("data") val data: DataResponse<T>,
    @SerialName("meta") val meta: MetaResponse?,
    @SerialName("included") val included: List<IncludedResponse>? = null
)
Class 2 has - List<DataResponse>, MetaResponse, List<IncludedResponse>
@Serializable
data class ListApiResponse<T>(
    @SerialName("data") val data: List<DataResponse<T>>,
    @SerialName("meta") val meta: MetaResponse?,
    @SerialName("included") val included: List<IncludedResponse>? = null
)
In both the class 
List<IncludedResponse>?
 & 
MetaResponse
  both are common only uncommon thing is 
DataResponse
 & 
List<DataResponse>
 .
d

diesieben07

07/15/2021, 9:30 AM
How about this (leaving out the serialization annotations):
data class ApiResponse<T>(val data: T, val meta: MetaResponse?, val included: List<IncludedResponse>?)

typealias PlainApiResponse<T> = ApiResponse<DataResponse<T>>
typealias ListApiResponse<T> = ApiResponse<List<DataResponse<T>>>
1
👍 1
k

KV

07/15/2021, 9:36 AM
Let me try out this
Yeah looks perfect 👍
t

Tobias Suchalla

07/15/2021, 10:04 AM
btw, if your variable name matches the @SerialName, you can omit the annotation.
1