Skip to content

Instantly share code, notes, and snippets.

@glidester
Created November 20, 2017 10:20
Show Gist options
  • Save glidester/82886bf6eef37a5b965e3d0f6673a213 to your computer and use it in GitHub Desktop.
Save glidester/82886bf6eef37a5b965e3d0f6673a213 to your computer and use it in GitHub Desktop.
Genson Issue with selectively including class meta data into only polymorphic classes
import com.owlike.genson.{ GensonBuilder, ScalaBundle, ScalaGenson }
import org.scalatest.{ Matchers, WordSpecLike }
trait GenericType {
def providerId: Int
}
class SpecialisedTypeA(
val providerId: Int,
val sourceEntityId: String
) extends GenericType
class SpecialisedTypeB(
val providerId: Int,
val flag: Boolean
) extends GenericType
case class SomeOtherStaticType(name: String, age: Int)
class GensonIssue extends WordSpecLike with Matchers {
val genson = new ScalaGenson(
new GensonBuilder()
.withBundle(ScalaBundle())
.useIndentation(true)
//.useRuntimeType(true)
.useClassMetadata(true)
.useClassMetadataWithStaticType(false)
//.addAlias("SpeTypA", classOf[SpecialisedTypeA])
.create()
)
val staticType = SomeOtherStaticType("test", 99)
val instanceOfAEntity = new SpecialisedTypeA(123, "one")
val instanceOfIEntity: GenericType = new SpecialisedTypeB(456, true)
"Genson" must {
"Serialize and Deserialize entity" in {
println(genson.toJson(staticType))
println(genson.toJson(instanceOfAEntity))
genson.fromJson[SomeOtherStaticType](genson.toJson(staticType)) shouldBe staticType
genson.fromJson[GenericType](genson.toJson(instanceOfAEntity)) shouldBe instanceOfAEntity
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment