如何使用Argonaut为Scala中的选定特征创建DecodeJson?

时间:2018-09-25 14:46:30

标签: scala argonaut

我用某些案例类创建了密封特征,女巫对其进行了扩展。

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] = ???
}

0 个答案:

没有答案
相关问题