Skip to content

Instantly share code, notes, and snippets.

@ubourdon
Last active September 3, 2020 14:02
Show Gist options
  • Save ubourdon/ef3e73c0523b60ee0e7bdf3e682b8b6c to your computer and use it in GitHub Desktop.
Save ubourdon/ef3e73c0523b60ee0e7bdf3e682b8b6c to your computer and use it in GitHub Desktop.
object A {
sealed trait LotLinkType
object LotLinkType {
case object Tenant extends LotLinkType
case object Owner extends LotLinkType
case object Landlord extends LotLinkType
}
implicit val TenantJsonFmt = jsonSerializeCaseObject(Tenant)
implicit val OwnerJsonFmt = jsonSerializeCaseObject(Owner)
implicit val LandlordJsonFmt = jsonSerializeCaseObject(Landlord)
implicit val LotLinkTypeJsonFmt = new Format[LotLinkType] {
override def reads(json: JsValue): JsResult[LotLinkType] =
__.read[Tenant.type].map(x => x: LotLinkType)
.orElse(__.read[Owner.type].map(x => x: LotLinkType))
.orElse(__.read[Landlord.type].map(x => x: LotLinkType)).reads(json)
override def writes(o: LotLinkType): JsValue = o match {
case Tenant => Json.toJson(Tenant)
case Owner => Json.toJson(Owner)
case Landlord => Json.toJson(Landlord)
}
}
}
object B {
import PatrimonyContactEventModelJsonFmt.LotLinkTypeJsonFmt
Json.toJson(Tenant).validate[LotLinkType]
Json.toJson(Owner).validate[LotLinkType]
Json.toJson(Landlord).validate[LotLinkType]
}
@ubourdon
Copy link
Author

ubourdon commented Sep 3, 2020

if i cast Tenant with LotLinkType
Json.toJson(Tenant: LotLinkType).validate[LotLinkType] that compil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment