Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Nested generic type Issue #20

Closed
brianyu0717 opened this issue Dec 11, 2016 · 2 comments
Closed

Nested generic type Issue #20

brianyu0717 opened this issue Dec 11, 2016 · 2 comments

Comments

@brianyu0717
Copy link

brianyu0717 commented Dec 11, 2016

Hi, just noticed an issue while using nested types e.g.

data class User(val id: String)

val a = gson.fromJson<List<List<User>>>("[\n" +
            "  [\n" +
            "    {\n" +
            "      \"id\": \"u-abcd\"\n" +
            "    }\n" +
            "  ]\n" +
            "]\n")

Expecting a to be of type ArrayList<ArrayList<User>>> however I'm getting ArrayList<ArrayList<LinkedTreeMap<etc... as per Gson>>> It looks like it's not getting past the 'first' level generic type.

@SalomonBrys
Copy link
Owner

This has to do with the fact that, in kotlin, List<T> is actually List<out T>, meaning it has declaration site variance.

  • In Java new TypeToken<List<User>>(){}.getType() returns java.util.List<com.you.User>
  • In Kotlin, object : TypeToken<List<User>>(){}.type returns java.util.List<? extends com.you.User>

This will be corrected in Kotson 2.5.0, thanks for the report ;)

@SalomonBrys
Copy link
Owner

Fixed in 2.5.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants