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

Cannot deserialize Option[Int] when JodaModule is registered (ClassCastException: org.joda.time.Period) #33

Closed
lorrin opened this issue Jul 20, 2012 · 3 comments

Comments

@lorrin
Copy link

lorrin commented Jul 20, 2012

I'm having trouble deserializing to a bean that contains an Option[Int]. It seems that when the JodaModule is registered, the deserialization puts a Some[JodaPeriod] in as the value of the Option[Int] (!) which then causes a ClassCastException when I try to access it.

The following exception is thrown by the mapper2.readValue call in the sample code.

Exception in thread "main" java.lang.ClassCastException: org.joda.time.Period cannot be cast to java.lang.Integer
    at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
    at JodaTimeTest$.main(JodaTimeTest.scala:15)
    at JodaTimeTest.main(JodaTimeTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JacksonModule}

object JodaTimeTest {

  def main(args: Array[String]): Unit = {
    val mapper1 = new ObjectMapper()
    mapper1.registerModule(DefaultScalaModule)
    val int1 =  mapper1.readValue("""{"x":123}""",classOf[OptionalInt]).x.get

    val mapper2 = new ObjectMapper()
    mapper2.registerModule(DefaultScalaModule)
    mapper2.registerModule(new JodaModule())
    val int2 =  mapper2.readValue("""{"x":123}""",classOf[OptionalInt]).x.get
  }
}

case class OptionalInt(x: Option[Int])
@christophercurrie
Copy link
Member

Very strange, I'll try and take a look at this issue later this week.

christophercurrie added a commit that referenced this issue Sep 19, 2012
@christophercurrie
Copy link
Member

I added a test to try to reproduce this issue, but I'm not able to in my environment. Your file above compiles and runs fine for me, using scala 2.9.1.final on Java 1.6 on MacOS. Can you be more specific about your environment, that might indicate why this is failing for you?

@christophercurrie
Copy link
Member

I'm assuming this is no longer an issue, reopen in you're still having issues.

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