Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
Demonstrate the problem (FasterXML/jackson-module-scala#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSpanel committed Jul 30, 2018
0 parents commit 99eb4e4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/target
/project/target

14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 1.1.6
49 changes: 49 additions & 0 deletions src/main/scala/mypackage/Main.scala
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 99eb4e4

Please sign in to comment.