我用某些案例类创建了密封特征,女巫对其进行了扩展。
sealed trait Notification
case class A (userId: Int, userName: String) extends Notification
case class B (companyId: Int, companyName: String, managerName: String) extends Notification
此后,我创建了Notifications对象。在这个对象中,我成功编写了EncodeJson [Notification]的方法,但是无法编写DecodeJson [Notification]的方法。
object Notification {
implicit def NotificationEncodeJson: EncodeJson[Notification] = {
EncodeJson((n: Notification) => n match {
case a: A =>
Json("userId" := a.userId, "userName" := a.userName)
case b: B =>
Json("companyId" := b.companyId, "companyName" := a.companyName, "managerName" := b.managerName)
implicit def NotificationDecodeJson: DecodeJson[Notification] = ???
}