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

CsvMapper does not serialize / de-serialize Scala Option correctly #76

Closed
mlvn23 opened this issue Mar 23, 2018 · 3 comments
Closed

CsvMapper does not serialize / de-serialize Scala Option correctly #76

mlvn23 opened this issue Mar 23, 2018 · 3 comments

Comments

@mlvn23
Copy link

mlvn23 commented Mar 23, 2018

I'm not sure if this is a CsvMapper issue or jackson-scala-module issue. I'm using version 2.9.4 for everything, but I also tried it with 2.7.x. The following code works with the ObjectMapper but not with the CsvMapper:

        case class Record(key: String, value: Option[Double])

        val mapper = new CsvMapper with ScalaObjectMapper
        mapper.registerModule(DefaultScalaModule)

        val cls = classOf[Record]
        val schema = mapper.schemaFor(cls).withHeader
        val writer = mapper.writer(schema)
        val reader = mapper.readerFor(cls).`with`(schema)

        val goodCsv = writer.writeValueAsString(Record("good", Some(10.0)))
        val badCsv = writer.writeValueAsString(Record("bad", None))

        println(goodCsv)
        // key,value
        // good,10.0

        println(badCsv)
        // key,value
        // bad,

        println(reader.readValue[Record](goodCsv)) // Record(good,Some(10.0))
        println(reader.readValue[Record](badCsv))  // Record(bad,Some())  -> it deserialized into Option[String]

        // fails: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
        val d: Double = reader.readValue[Record](badCsv).value.get 

@cowtowncoder
Copy link
Member

If it can be reproduced with Java type like AtomicReference, could be CSV module.
Otherwise Scala; and that is in general more likely since at this point Scala module is without owner and not actively maintained.

@mlvn23
Copy link
Author

mlvn23 commented Mar 23, 2018

Using AtomicReference with Scala also de-serialized it into an AtomicReference[String] instead of AtomicReference[Double]. I tried it in Java and it correctly de-serialized into AtomicReference[Double]. I'll raise an issue on the Scala module.

@cowtowncoder
Copy link
Member

Thank you.

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