From 99eb4e4f23721a2dc9c5d3d14ca59b2053eb5c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0pan=C4=9Bl?= Date: Mon, 30 Jul 2018 14:01:06 +0200 Subject: [PATCH] Demonstrate the problem (https://github.com/FasterXML/jackson-module-scala/issues/366) --- .gitignore | 4 +++ build.sbt | 14 +++++++++ project/build.properties | 1 + src/main/scala/mypackage/Main.scala | 49 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 .gitignore create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 src/main/scala/mypackage/Main.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9f72d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea +/target +/project/target + diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..d6f25a3 --- /dev/null +++ b/build.sbt @@ -0,0 +1,14 @@ +name := "JacksonTypeAlias" + +version := "0.1" + +scalaVersion := "2.11.12" + + +val jacksonVersion = "2.9.6" + +libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion + +libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion + +libraryDependencies += "com.fasterxml.jackson.dataformat" % "jackson-dataformat-yaml" % jacksonVersion diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..16eecf5 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.1.6 \ No newline at end of file diff --git a/src/main/scala/mypackage/Main.scala b/src/main/scala/mypackage/Main.scala new file mode 100644 index 0000000..2b48638 --- /dev/null +++ b/src/main/scala/mypackage/Main.scala @@ -0,0 +1,49 @@ +package mypackage + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.scala.DefaultScalaModule +import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper + +case class XY(x: Double = 0, y: Double = 0) + +trait Abstract { + type Vector2f + + trait ConstructVector2f { + def apply(x: Double, y: Double): Vector2f + } + implicit val Vector2f: ConstructVector2f +} + +object Concrete extends Abstract { + type Vector2f = XY + object Vector2f extends ConstructVector2f { + def apply(x: Double, y: Double) = XY(x, y) + } +} + +object Abstract { + val Link: Abstract = Concrete +} + +import Abstract.Link.Vector2f + +case class Container(from: Vector2f) + +object Main extends App { + + val mapper = new ObjectMapper with ScalaObjectMapper + + mapper.registerModule(DefaultScalaModule) + + val input = Container(Vector2f(0,0)) + + val out = mapper.writeValueAsString(input) + + val loaded = mapper.readValue[Container](out) + + val xy: Vector2f = loaded.from + + println(loaded) // prints "Container(Map(x -> 0.0, y -> 0.0))" instead of "Container(XY(0.0,0.0))" + assert(xy.isInstanceOf[XY]) // fails +} \ No newline at end of file