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 serialize Option[Foo] if Foo is a trait #150

Closed
miracle2121 opened this issue Jun 3, 2014 · 2 comments
Closed

Cannot serialize Option[Foo] if Foo is a trait #150

miracle2121 opened this issue Jun 3, 2014 · 2 comments

Comments

@miracle2121
Copy link

I've encountered an issue where I cannot serialize an Option wrapping a trait. I'm using version 2.2.2. Is this behavior expected?

import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.databind.ObjectMapper

trait MyTrait {
  def myval: String
}

case class MyImpl(myval: String) extends MyTrait

case class Wrapper1(
  mytrait: Option[MyTrait]
)

case class Wrapper2(
  myimpl: Option[MyImpl]
)

case class Wrapper3(
  mytraits: Option[List[MyTrait]]
)

val obj1 = Wrapper1(
  mytrait = Some(
    MyImpl(
      myval = "MyVal1"
    )
  )
)

val obj2 = Wrapper2(
  myimpl = Some(
    MyImpl(
      myval = "MyVal2"
    )
  )
)

val obj3 = Wrapper3(
  mytraits = Some(
    List(
      MyImpl(
        myval = "MyVal3"
      )
    )
  )
)

val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)

mapper.writeValueAsString(obj1) // throws exception below
mapper.writeValueAsString(obj2) // works as expected
mapper.writeValueAsString(obj3) // works as expected

This is the stack trace:

java.lang.InternalError: Malformed class name
    at java.lang.Class.getSimpleName(Class.java:1133)
    at com.fasterxml.jackson.databind.JsonMappingException$Reference.toString(JsonMappingException.java:108)
    at com.fasterxml.jackson.databind.JsonMappingException._appendPathDesc(JsonMappingException.java:364)
    at com.fasterxml.jackson.databind.JsonMappingException.getPathReference(JsonMappingException.java:267)
    at com.fasterxml.jackson.databind.JsonMappingException._buildMessage(JsonMappingException.java:340)
    at com.fasterxml.jackson.databind.JsonMappingException.getMessage(JsonMappingException.java:321)
    at com.fasterxml.jackson.databind.JsonMappingException.toString(JsonMappingException.java:348)
    at java.lang.String.valueOf(String.java:2826)
    at java.io.PrintWriter.println(PrintWriter.java:710)
    at java.lang.Throwable.printStackTrace(Throwable.java:509)
    at scala.tools.nsc.util.package$$anonfun$stackTraceString$1.apply(package.scala:84)
    at scala.tools.nsc.util.package$$anonfun$stackTraceString$1.apply(package.scala:84)
    at scala.tools.nsc.util.package$.stringFromWriter(package.scala:73)
    at scala.tools.nsc.util.package$.stackTraceString(package.scala:84)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint$$anonfun$bindError$2.apply(IMain.scala:714)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint$$anonfun$bindError$2.apply(IMain.scala:714)
    at scala.tools.nsc.interpreter.ReplConfig$$anonfun$logAndDiscard$1.applyOrElse(ReplConfig.scala:46)
    at scala.tools.nsc.interpreter.ReplConfig$$anonfun$logAndDiscard$1.applyOrElse(ReplConfig.scala:41)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)
    at scala.tools.nsc.interpreter.IMain.scala$tools$nsc$interpreter$IMain$$withLastExceptionLock(IMain.scala:250)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.bindError(IMain.scala:711)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:984)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:900)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
    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 sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
    at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
    at sbt.Logger$$anon$4.apply(Logger.scala:90)
    at sbt.TrapExit$App.run(TrapExit.scala:244)
    at java.lang.Thread.run(Thread.java:662)

@christophercurrie
Copy link
Member

Not expected; I'll try and track down what's causing this for the next release.

@christophercurrie
Copy link
Member

I can reproduce this error, but only (a) on 2.2.x, (b) using Scala 2.10, (c) in paste mode on the REPL. Running your example as a unit test on 2.2.2 using Scala 2.10 doesn't trigger the error, nor does using the REPL on 2.3+. Given that 2.3 is at this point over a year old, I'd recommend upgrading, if the REPL is an important part of your workflow.

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