-
-
Notifications
You must be signed in to change notification settings - Fork 142
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
Unable to deserialize to a List[SomeClass] #107
Comments
Your "json" string is overquoted. Scala raw strings have three leading quote marks, not four. Your string is thus being seen by Jackson as a JSON String rather than a JSON Array. |
Whoops, you are right. I fixed my test cases and it looks like the test case that uses case class InListCaseClass(@JsonProperty("id") id: Int)
val list = List(InListCaseClass(2), InListCaseClass(5))
val json = """[{"id":2},{"id":5}]"""
// JsonMappingException: Can not construct instance of scala.collection.immutable.List, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: java.io.StringReader@1beb518; line: 1, column: 1]
it should "deserialize a List of a case class using JavaType" in {
val paramTypes = Array(mapper.getTypeFactory().constructSimpleType(classOf[InListCaseClass],
Array.empty[JavaType]))
val listType = mapper.getTypeFactory.constructSimpleType(classOf[List[InListCaseClass]], paramTypes)
val result: List[InListCaseClass] = mapper.readValue(json, listType)
result should equal(list)
} |
Fair enough. // EDITED to add missing body to anonymous subclass
val listType = new TypeReference[List[InListCaseClass]] {}
val result: List[InListCaseClass] = mapper.readValue(json, listType) |
In my project I'm working with a library called retrofit for making REST calls and it wraps the Jackson usage via a converter. The class I'm seeing the error in is https://github.com/square/retrofit/blob/master/retrofit-converters/jackson/src/main/java/retrofit/converter/JacksonConverter.java where the JavaType object is similar to my example above (List[SomeClass]). |
The only way that what retrofit is doing will work is if the val listType = new TypeReference[List[InListCaseClass]] {}
jacksonConverter.fromBody(body, listType.getType()) |
It looks like Retrofit is using reflectcion via |
OK. I just got confirmation that Your own test case needs to either look more similar to Retrofit and use a |
Thanks for all the info. Based on everything you have explained I have been able to modify my tests and confirm that this appears to be a retrofit bug due to using |
That seems to be the right approach. The message I got from Jackson is "defaultInstance is really.... like a hack for unit tests ... it's static singleton, so can't register Scala (et al) stuff." Good luck, and let the Retrofit team know they can ask questions on the Jackson mailing list if they want more explanation. |
So far so good. The only additional comment for one of code snippets from above is that method Instead, |
I was having trouble deserializing a scala List of a case class in a project. I wrote a couple simple tests below that reproduce the issue with slightly different errors.
The text was updated successfully, but these errors were encountered: