【发布时间】:2020-11-23 17:09:04
【问题描述】:
当我尝试将我的查询包装在 BSONDocument 中并将我的 enumeratum 枚举作为它解说编译的值时。
例如,我的枚举:
sealed trait ProcessingStatus extends EnumEntry with UpperSnakecase
object ProcessingStatus extends Enum[ProcessingStatus] with ReactiveMongoBsonEnum[ProcessingStatus] {
val values: IndexedSeq[ProcessingStatus] = findValues
case object Processing extends ProcessingStatus
case object Done extends ProcessingStatus
}
我玩了解释如何序列化的 json 序列化程序:
object JsonSerialization {
import reactivemongo.api.bson._
implicit object ProcessingStatusReader extends BSONReader[ProcessingStatus] {
override def readTry(bson: BSONValue): Try[ProcessingStatus] = bson match {
case BSONString(s) => bson.asTry[ProcessingStatus]
case _ => Failure(new RuntimeException("String value expected"))
}
}
implicit object ProcessingStatusWriter extends BSONWriter[ProcessingStatus] {
override def writeTry(t: ProcessingStatus): Try[BSONString] = Try(BSONString(t.entryName))
}
//Report Serializers
implicit val ProcessingStatusFormat: Format[ProcessingStatus] = EnumFormats.formats(ProcessingStatus)
implicit val ReportFormat: OFormat[Report] = Json.format[Report]
}
现在在我的 dao 中无法编译:
import reactivemongo.play.json.compat.json2bson.{toDocumentReader, toDocumentWriter}
import serializers.JsonSerialization._
def findReport(reportId: String) = {
val test = BSONDocument("123" -> ProcessingStatus.Processing) // dosent compile
}
截图:
编译错误:
overloaded method apply with alternatives:
(elms: Iterable[(String, reactivemongo.api.bson.BSONValue)])reactivemongo.api.bson.BSONDocument <and>
(elms: reactivemongo.api.bson.ElementProducer*)reactivemongo.api.bson.BSONDocument
cannot be applied to ((String, enums.ProcessingStatus.Done.type))
val test = BSONDocument("status" -> ProcessingStatus.Done)
【问题讨论】:
-
它是如何工作的?
JsonSerialization不提供BSONWriter[ProcessingStatus.Processing],{toDocumentReader, toDocumentWriter}也不提供仅适用于BSONDocument/OFormat(而不是Format)。 -
@cchantep 编辑了 JsonSerialization(正如您在问题中所见)我的 BSONWriter/BSONReader,仍然是讲解员的工作。很难找到合适的例子,,,
-
“它不起作用”远不够具体。要获取有关错误的帮助,请粘贴错误详细信息。
-
@cchantep 我在BSONDocument无法编译的问题中写了,我不知道该怎么描述它,如果有帮助,我添加了一个截图。
标签: scala reactivemongo