I wanted to see how a function similar to this `fr...
# announcements
a
I wanted to see how a function similar to this
fromJson
function behaves with a generic type and found this. Can anybody explain why that instance check is sometimes true and other times false? šŸ¤”
youtrack 1
g
@hho. I don’t think that it somehow related to Idea
@arekolek What is actually returned by fromJson in the last case?
h
I don't know whether it's IDEA or kotlinc – but calling
.first().first()
on a
List<List<Int>>
should result in an
Int
, shouldn't it?
g
@hho Yes, but looks that Idea shows correct warning in this case
a
to my amazement this
but…
without
first().first()
it’s this
g
ohh
so this is not a list of ints, but list of doubles? šŸ¤”
Just curious about toString result
instead of implicit toString, try to use
::class
a
I’m lost šŸ˜‚
g
so what is
::class
of first().first()?
n
i am guessing json actually supplies doubles, which would be weird but well.. json only has double/float
āž• 1
a
just added kotlin-reflect, need to sync
n
which lib is that fromJson() call from anyways ?
g
You don’t need kotlin-reflect for this
n
i have exlusively been suing kotlinx.serialization for my needs so i kind of lost the overview over other libs
a
Isn’t this fun
g
Yeah, json doesn’t have ints and this can be a problem
it actually can make sense, but I’m still not exactly sure how this works
n
wtf
g
in case of assignment Kotlin converts value for your explictly, because assign on local variable
n
i'd rather have the json deserialization fail and then be handed safe ints
g
But list actually contains doubles and Gson doesn’t convert them to int, because in Java you have implicit type conversion Double -> Int, but not in Kotlin
a
@gildor that’s what I was getting without kotlin-reflect
g
pretty tricky. One more thing why Gson is bad for Kotlin
šŸ‘ 1
@arekolek Right, sorry, you need
.javaClass
instead, without kotlin-reflect ::class can be used only for compile time known types
a
image.png
image.png
That may be because I’m using a scratch file, otherwise I’m not sure what’s up with that, but nevermind
n
.javaClass or ::class.java i think its the same
g
Nono, you need just
first().first().javaClass
a
Like NikkyAi says it seems
which lib is that fromJson() call from anyways ?
@Nikky it’s something my colleague came up with and I was wondering if it’s something that actually works in all cases
n
well... now i am curious.. is the lib on github ?
on first glance it reminded me of some json lib a friend of mine came up with for java.. called Jankson terrible name to google for
šŸ˜‚ 1
g
i’d rather have the json deserialization fail and then be handed safe ints
Deserialization shouldn’t fail in this case, it’s completely valid case. What Json mapper should do in this case is actually parse value as Int instead of default ā€œuniversalā€ double
b
Something inside
fromJson
is probably using unchecked generic operations to trick the compiler into thinking that something is an
Int
, that is actually a
double
at runtime
āž• 1
g
Int -> Double is safe operations, so Gson gust doesn’t convert it explicitly
n
i meant fail i case of there being a double, i misread the code and thought
,
was
.
a
well... now i am curious.. is the lib on github ?
It’s just a function internal to the project, the actual parsing is done by Gson
g
Json can contain only JS number which is value between -2^53 and 2^53
n
ell i am happy i do not use gson i guess.. this kind of uncertainty in the types would drive me crazy
well i'd argue JSON being limited by JS is silly and when you are only consuming this json with kotlin code on the jvm then there should at leeast be a option in eg. gson to actually use integers
g
Yes, of course, using integers is completely fine and yeah, this is not really good thing what Gson does and parse numbers always to double
k
Could you perhaps show what your
fromJson
function looks like. Like Bart said, if you’re implementing this based on erased types at runtime, then that might be the issue.
a
You can see the function in the first post
g
I don't see any issues here, just a way how Gson works Of course I may be wrong
a
I think you are right šŸ˜„
Although something else doesn’t work in the real function (it’s more complicated than the MWE I gave above), so I’m not 100% sure either šŸ˜„
k
Indeed. It’s a Gson issue. It appears it’s unable to handle nested parameterized types using the
object : TypeToken<T>() {}.type
notation. I was only able to make it work using explicitly:
TypeToken.getParameterized(List::class.java, TypeToken.getParameterized(List::class.java, Int::class.javaObjectType).type).type)
which is just too sad and ugly
a
@kingsley but apart from this double/int confusion, that function seems to work fine