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

How to deserialize a json file to Map[String,String] in scala? #64

Closed
dineshbt opened this issue Mar 14, 2013 · 3 comments
Closed

How to deserialize a json file to Map[String,String] in scala? #64

dineshbt opened this issue Mar 14, 2013 · 3 comments

Comments

@dineshbt
Copy link

It works fine for me in java where as in scala i am getting the below exception. Please let me know how can i resolve this?

val jsonContent ="""{"test":"113123","myList":{"test2":"321323"}}"""
val mapData = mapper.readValue(jsonContent, classOf[Map[String,String]])

Error:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of scala.collection.immutable.Map, 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@603b1d04; line: 1, column: 1]

@christophercurrie
Copy link
Member

It looks like you didn't add the Scala module to the object mapper. Without the module, Jackson doesn't know how to deserialize the Scala collection class scala.collection.immutable.Map, because it doesn't implement any of the Java collection interfaces.

You can either deserialize to a Java collection:

val mapData = mapper.readValue(jsonContent, classOf[java.util.Map[String,String]])

or add the Scala module to the mapper:

mapper.registerModule(DefaultScalaModule)
val mapData = mapper.readValue(jsonContent, classOf[Map[String,String]])

@dineshbt
Copy link
Author

Thanks Christopher. It worked after registering the scalamodule to the mapper.
mapper.registerModule(DefaultScalaModule)

@sccu
Copy link

sccu commented May 20, 2016

Thanks christophercurrie. It helped me, too.

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

3 participants