Before I thank you for your response.
Today in the afternoon I think I found an alternative, I do not know if it would be the best, I put the code
data class PercentResponse (
var orderId: Int = 0,
var type: Int = 0,
var note: String? = "",
var percent: TypePercent? = null)
data class TypePercent (
var type1: ArrayList<Type1> = ArrayList(),
var type2: ArrayList<ItemType2> = ArrayList()
)
data class Type1 (
val category: String = "",
val items: ArrayList<ItemType1> = ArrayList()
)
class PercentDeserializer : JsonDeserializer<PercentResponse> {
// @Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): PercentInspectionResponse {
val jsonObject = json.asJsonObject
val gson = Gson()
val percent = PercentResponse()
percent.orderId = jsonObject.get("orderId").asInt
percent.type = jsonObject.get("type").asInt
val noteString = jsonObject.get("note")
if (!noteString.isJsonNull) {
percent.note = noteString.asString
}
val typePercent= TypePercentInspection()
val type = jsonObject.get("type").asInt
if (1 == type) {
val percentTypes = jsonObject.getAsJsonArray("percent")
for (percent in percentTypes) {
val percentClass= gson.fromJson(percent, Type1::class.java)
typePercent.type1.add(percentClass)
}
}
if (2 == type) {
val percentTypes = jsonObject.getAsJsonArray("percent")
for (percent in percentTypes) {
val percentClass= gson.fromJson(percent, ItemType2::class.java)
typePercent.type2.add(percentClass)
}
}
percent.percent = typePercent
return percent
}
}
and that register it to the Gson
@Provides
@Singleton
internal fun provideRetrofit(): Retrofit {
val percentDeserializer =
GsonBuilder().registerTypeAdapter(PercentResponse::class.java, PercentDeserializer()).create()
return Retrofit.Builder()
.baseUrl(API_URL)
// .addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(percentDeserializer))
.client(createClient())
.build()
}
So I end up doing it, I think it looks like the one option you recommended me, right?