scala-ide / scala-worksheet

A Scala IDE plugin for a multi-line REPL (called worksheet)
96 stars 24 forks source link

Exception using jackson #217

Open migel opened 9 years ago

migel commented 9 years ago

HI, I've created a scala worksheet to test object updates with Jackson (v 2.5.1). We I try to update the object (d12 below) I get an exception. If I test it in a standalone test app it works.

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
import com.fasterxml.jackson.databind.ObjectReader

class DataA {
  @JsonProperty var vint: Int = 3
}

object jackson4 {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  val mapper = new ObjectMapper()                 //> mapper  : com.fasterxml.jackson.databind.ObjectMapper = com.fasterxml.jackso
                                                  //| n.databind.ObjectMapper@679e96f7
  mapper.registerModule(DefaultScalaModule)       //> res0: com.fasterxml.jackson.databind.ObjectMapper = com.fasterxml.jackson.da
                                                  //| tabind.ObjectMapper@679e96f7
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
                                                  //> res1: com.fasterxml.jackson.databind.ObjectMapper = com.fasterxml.jackson.da
                                                  //| tabind.ObjectMapper@679e96f7
  mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE)
                                                  //> res2: com.fasterxml.jackson.databind.ObjectMapper = com.fasterxml.jackson.da
                                                  //| tabind.ObjectMapper@679e96f7

  val d10 = new DataA                             //> d10  : DataA = DataA@fb9165a
  d10.vint = 5

  val d10Json = mapper.writeValueAsString(d10)    //> d10Json  : String = {
                                                  //|   "vint" : 5
                                                  //| }
  val d11 = mapper.readValue(d10Json, classOf[DataA])
                                                  //> d11  : DataA = DataA@3c22de9e

  val d11Json = mapper.writeValueAsString(d11)    //> d11Json  : String = {
                                                  //|   "vint" : 5
                                                  //| }

  val d12 = new DataA                             //> d12  : DataA = DataA@154575ea
  mapper.readerForUpdating[ObjectReader](d12).readValue(d10Json)
                                                  //> java.lang.ClassCastException: DataA cannot be cast to scala.runtime.Nothing$
                                                  //| 
                                                  //|   at jackson4$$anonfun$main$1.apply$mcV$sp(jackson4.scala:29)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
                                                  //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
                                                  //| orksheetSupport.scala:65)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
                                                  //| ksheetSupport.scala:75)
                                                  //|   at jackson4$.main(jackson4.scala:13)
                                                  //|   at jackson4.main(jackson4.scala)
}

I've found out that it works if I change the last line to:

mapper.readerForUpdating[ObjectReader](d12).readValue[DataA](d10Json)

It makes it work in the worksheet but it is not needed in the standalone app.