https://kotlinlang.org logo
Title
k

Kevin Schmeichel

06/24/2019, 6:26 PM
why do you have an explicit
IntDeserializer()
? maybe try removing that line, gson can deserialize Ints automatically.
t

tjohnn

06/24/2019, 6:30 PM
It doesn't deserialize empty string for me, eg sending
ageGroupId:""
gives me
NumberFormatException: empty String
which I wanted to handle explicitly. Is there a way to handle that without adapter
Adapter is not resolving the issue for me either
k

Kevin Schmeichel

06/24/2019, 6:41 PM
you can't catch the exception in your adaptor?
like this:
t

tjohnn

06/24/2019, 6:56 PM
Yes I am doing something like that but the adapter doesn’t seem to be called, so it’s still using the default deserializer
k

Kevin Schmeichel

06/24/2019, 7:08 PM
ok, so you just need to figure out how to properly register the adaptor. did you try
registerTypeAdapterFactory(...)
?
or at the bottom of the stackoverflow, maybe you need something like:
registerTypeAdapter(java.lang.Integer::class.java, adapter)
t

tjohnn

06/24/2019, 7:13 PM
Yes I am doing it like this:
install(ContentNegotiation){
                register(ContentType.Application.Json, GsonConverter(GsonBuilder().apply {
                    setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)

                    registerTypeAdapter(DateTime::class.java, DateTimeDeserializer())
                    registerTypeAdapter(Int::class.java, IntDeserializer())
                }.create()))
            }
k

Kevin Schmeichel

06/24/2019, 7:14 PM
maybe you need
java.lang.Integer::class.java
instead?
t

tjohnn

06/24/2019, 7:15 PM
Hmm.. I’d try that. Thanks
k

Kevin Schmeichel

06/24/2019, 7:15 PM
or try the Factory method like in the stackoverflow?
t

tjohnn

06/24/2019, 9:44 PM
java.lang.Integer::class.java
works fine thanks
Is that because gson doesn't recognize kotlin
Int
or what?
k

Kevin Schmeichel

06/24/2019, 10:03 PM
yeah, that's my guess