Mongo DB DAO和Spray JSON:

时间:2017-01-20 11:20:00

标签: json mongodb scala spray-json

我的Mongo DB抽象Dao定义如下

    abstract class DbMongoDAO1[K, T <: Keyable[K]] (implicit val manifestT: Manifest[T], val manifestK: Manifest[K])
  extends DbDAO[K, T]
    with DbDAOExtensions[K, T]
    with MongoConnection2
    with JsonDbImplicits
{

  val thisClass = manifestT.runtimeClass
  val simpleName = thisClass.getSimpleName

  lazy val collection = db.getCollection(s"${DbMongoDAO1.tablePrefix}$simpleName")

  override def insertNew(r:T): Result[String,T] = {
    val json: String = r.toJson.compactPrint

    collection.insertOne(Document(json))

    KO("Not Implemented")
  }
}

在将案例类转换为JSON时,我在以下代码行中收到错误。

  

错误:(31,26)值toJson不是类型参数T

的成员      

val json:String = r.toJson.compactPrint

val json: String = r.toJson.compactPrint

特征JsonDbImplicits如下

trait JsonDbImplicits extends DefaultJsonProtocol
  with SprayJsonSupport with JodaImplicits {

  implicit val json_UserEmail:RootJsonFormat[UserEmail] = jsonFormat5(UserEmail)

  implicit val json_UserProfile:RootJsonFormat[UserProfile] = jsonFormat13(UserProfile)

  implicit val json_UserSession:RootJsonFormat[UserSession] = jsonFormat5(UserSession)

}

案例类UserEmail和UserProfile定义如下

case class UserEmail
(
  // it is the full email address
  @Key("_id") id: String
  , account_id: String
  , active: Boolean = false
  , ts_created: DateTime = now
  , ts_updated: DateTime = now
) extends Keyable[String]

trait DbUserEmail extends DbMongoDAO1[String,UserEmail]

case class UserProfile
(
  // id is the same as AccountId
  @Key("_id") id: String = UUID.randomUUID().toString
  , gender: Option[String] = None
  , first_name: Option[String] = Some("")
  , last_name: Option[String] = Some("")
  , yob: Option[Int] = None
  , kids: Option[Int] = None
  , income: Option[Int] = None
  , postcode: Option[String] = None
  , location: Option[Boolean] = Some(true)
  , opt_in: Option[Boolean] = Some(true)
  , third_party: Option[Boolean] = Some(true)
  , ts_created: DateTime = now
  , ts_updated: DateTime = now

) extends Keyable[String]

trait DbUserProfile extends DbMongoDAO1[String,UserProfile]

我错过了什么?

0 个答案:

没有答案