mardi 29 septembre 2015

Getting Exception while converting case class to Json using Json4s

I am trying to convert a case class to Json String using Json4s. I am getting the exception

MappingException: Can't find ScalaSig for class java.lang.Object

This is happening if I am extending my case class with another trait only.

My code is as below:

trait Integration {
  val thirdpartyId: Option[Long]
}

trait HrIntegration extends Integration {
  override val thirdpartyId: Option[Long] = getValue
  def getValue = {
    Some(100L)
  }
}

case class Employee(id: Long, name: String, age: Long) extends HrIntegration


object Test extends App {
  import org.json4s.Extraction
  import org.json4s.jackson.JsonMethods._
  import org.json4s.DefaultFormats
  implicit lazy val serializerFormats = DefaultFormats
  val emp = Employee(1, "Yadu", 27)
  val jValue = Extraction.decompose(emp)
  val jsonString = compact(jValue)
  println(jsonString)
}

If I convert Option[Long] to Option[BigInt], it works fine. The same issue is with Option[Double] as well.

When I went through the stacktrace and subsequent googling, I found that the issue is with the reflection, due to scala version mismatch. So I added scala reflect library dependency as below:

"org.scala-lang" % "scala-reflect" % "2.11.7",
"org.scala-lang" % "scalap" % "2.11.7"

But even after that, I am getting the same error. I have fixed the issue for now by using BigInt and BigDecimal instead of Long and Double.

Can someone please help me to understand the issue and how I can fix it by using Long and Double itself.

Json4s Version : 3.2.11
Scala Version : 2.11.7





2 commentaires:

  1. I had a very similar problem. Mine was solved by changing the name of the 'id' field to something else.

    RépondreSupprimer
    Réponses
    1. Sorry about the terse comment...I fat-fingered the keyboard. Anyway, I don't have a good idea of why changing the name of the field had an effect, but 'id' seemed sort of risky when I chose it, and so it proved to be. I'd suggest trying to rename Employee#id to something else. Good luck!

      Supprimer