Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe, not typed result of treeToValue method. #45

Closed
DmRomantsov opened this issue Oct 25, 2016 · 8 comments
Closed

Unsafe, not typed result of treeToValue method. #45

DmRomantsov opened this issue Oct 25, 2016 · 8 comments

Comments

@DmRomantsov
Copy link

DmRomantsov commented Oct 25, 2016

Sample code:
val listOfPojo:List<POJO> = jsonMapper.treeToValue(jsonNode)
Return List<LinkedHashMap> instead of List<POJO>. After iterration have java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to POJO.

Works OK:
val listOfPojo<POJO> = jsonMapper.readValue(jsonNode.toString())

@apatrida
Copy link
Member

What generics are you applying to List? Your code isn't valid.

And treeToValue takes a class which suffers from type erasure. It is better to use convertValue instead. or readValue(treeAsTokens(jsonNode))

@DmRomantsov
Copy link
Author

Sorry, I forgot about code style, so generics looks like tag and didn't appears in message. I fixed it.

@apatrida
Copy link
Member

yes, you can't use that method, it ignores the generics. if you look at the java method it passes in a class and not a type. so change to the other methods as I mentioned and they should work.

@apatrida
Copy link
Member

I can change the extension function in the future to do the work around since Kotlin version doesn't see the internal detail of type erasure because that parameter is hidden.

@apatrida
Copy link
Member

but for now that readValue work around is the same functionality

@DmRomantsov
Copy link
Author

Thank you, it would be great.

@apatrida
Copy link
Member

Let me know if the work around works for you, and I'll include it next time I do a round of dev.

@apatrida
Copy link
Member

apatrida commented Dec 8, 2016

closing, shouldn't be an issue.

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

No branches or pull requests

2 participants