Skip to content

Instantly share code, notes, and snippets.

@felipehummel
Created June 5, 2014 14:48
Show Gist options
  • Save felipehummel/766c065ee14c39789736 to your computer and use it in GitHub Desktop.
Save felipehummel/766c065ee14c39789736 to your computer and use it in GitHub Desktop.
json4s problem
object Test {
def main(args: Array[String]): Unit = {
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{ read, write, writePretty }
sealed trait Test
case object TestA extends Test
case object TestB extends Test
case object TestC extends Test
class TestSerializer extends CustomSerializer[Test](format => (
{
case JString("TestA") => TestA
case JString("TestB") => TestB
case JString("TestC") => TestC
},
{
case x: Test => JString(x.toString)
}
))
implicit val jsonFormats =
Serialization.formats(NoTypeHints) + new TestSerializer
case class MyCaseClass(test: Test)
case class MyTest(int: Int, test: Test)
println(write(TestA)) // Works
println(write(Map("what" -> TestA))) // Works
println(write(Map("what" -> MyCaseClass(TestA)))) // Fails with Malformed class name
println(write(MyCaseClass(TestA))) // Fails with Malformed class name
println(write(MyCaseClass(TestB))) // Fails with Malformed class name
println(write(MyTest(10, TestB))) // Fails with Malformed class name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment