Hi, please I need help on getting gson to deserial...
# ktor
t
Hi, please I need help on getting gson to deserialize json request data. I have this adapter for
Int
type:
Copy code
class IntDeserializer : TypeAdapter<Int>() {

    @Throws(IOException::class)
    override fun read(reader: JsonReader): Int? {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull()
            return null
        }
        val stringValue = reader.nextString()
        return try {
            if(isNumber(stringValue)) stringValue.toInt()
            else 0
        } catch (e: NumberFormatException) {
            null
        }

    }

    @Throws(IOException::class)
    override fun write(writer: JsonWriter, value: Int?) {
        if (value == null) {
            writer.nullValue()
            return
        }
        writer.value(value)
    }
}
I have this request data class:
Copy code
data class ProductRequest (
    var name: String? = null,
    var description: String? = null,
    var categoryId: Int? = null,
    var id: Long? = null,
    var subCategoryId: Int? = null,
    var ageGroupId: Int? = null,
    var genderId: Int? = null
)
Calling
val params = call.receive<ProductRequest>()
inside route always give me exception
Caused by: java.lang.NumberFormatException: empty String
.