spray-json无法为Class找到JsonWriter或JsonFormat类型

时间:2016-04-07 11:50:10

标签: scala spray-json

我仍然得到同样的错误,我已经定义了marshaller(并导入了它);当函数是多态的时,似乎case类条目不在上下文中。这会引发Cannot find JsonWriter or JsonFormat type class for Case Class。有没有理由为什么spray-json找不到case类的隐式编组器(即使在定义时)是上下文中的这种情况类?链接到marshaller

import spray.json._
import queue.MedusaJsonProtocol._

object MysqlDb {
 ...
}

case class UserDbEntry(
  id: Int,
  username: String,
  countryId: Int,
  created: LocalDateTime
)

trait MysqlDb {
  implicit lazy val pool = MysqlDb.pool
}

trait HydraMapperT extends MysqlDb {
  val FetchAllSql: String
  def fetchAll(currentDate: String): Future[List[HydraDbRow]]

  def getJson[T](row: T): String
}

object UserHydraDbMapper extends HydraMapperT {
  override val FetchAllSql = "SELECT * FROM user WHERE created >= ?"

  override def fetchAll(currentDate: String): Future[List[UserDbEntry]] = {
    pool.sendPreparedStatement(FetchAllSql, Array(currentDate)).map { queryResult =>
      queryResult.rows match {
        case Some(rows) =>
          rows.toList map (x => rowToModel(x))
        case None => List()
      }
    }
  }

  override def getJson[UserDbEntry](row: UserDbEntry): String = {
      HydraQueueMessage(
        tableType = HydraTableName.UserTable,
        payload = row.toJson.toString()
      ).toJson.toString()
  }

  private def rowToModel(row: RowData): UserDbEntry = {
    UserDbEntry (
      id        = row("id").asInstanceOf[Int],
      username  = row("username").asInstanceOf[String],
      countryId = row("country_id").asInstanceOf[Int],
      created   = row("created").asInstanceOf[LocalDateTime]
    )
  }
}

payload = row.toJson.toString()无法找到UserDbEntry的编组

1 个答案:

答案 0 :(得分:1)

您已在本地定义UserDbEntry,并且该类型没有JSON编组器。添加以下内容:

 implicit val userDbEntryFormat = Json.format[UserDbEntry]

我不确定如何调用row.toJson给定UserDbEntry是本地案例类。那里必定有一个宏,但很明显它不在本地UserDbEntry的范围内。

修改

现在我看到your Gist,看起来你有一个包依赖问题。按照设计,它将是循环的。您已在package com.at.medusa.core.queue中定义了JSON编组程序,导入UserDbEntry,这取决于package com.at.medusa.core.queue进行编组。