如何使用 spray-json 库将 toJson 应用于案例实例 class

How to apply toJson to instance of a case class using spray-json library

Registration.scala

package model

import akka.actor.Actor
import spray.json._
import DefaultJsonProtocol._

case class Registration(
  system: String, 
  identity: String)

object RegistrationProtocol extends DefaultJsonProtocol {
  implicit val adsRegistrationFormat = jsonFormat2(Registration)
}

RegistrationService.scala

import akka.actor.{Props, ActorLogging, Actor}
import model.Registration

object RegistrationsService {
  case class PostRegistrationMessage(registration: Registration)

  def props(property: String) = Props(classOf[RegistrationsService], property)
}

class RegistrationsService(property: String) extends Actor with ActorLogging {
  import RegistrationsService._

  def receive = {
    case PostRegistrationMessage(registration) => {
      import model.RegistrationProtocol._

      val json = registration.toJson

    }
  }
}

任何人都可以帮助我理解为什么会因编译错误 "value toJson is not a member of model.Registration" 而失败以及如何修复它。它如果在上面代码的最后一行失败,即“val json = registration.toJson

你也需要在 RegistrationService.scala 中导入 spray 提供的隐式操作

  import model.RegistrationProtocol._
  import spray.json._