JsValue 有 BSONDocumentWriter 和 BSONDocumentReader 吗?

Is there a BSONDocumentWriter and BSONDocumentReader for JsValue?

我正在尝试将案例 class 写入 BSONCollection,但我找不到任何对 JsValue 的读取和写入。 JsValue 有 BSONDocumentWriter 和 BSONDocumentReader 吗? 哦,有什么关于如何写的建议吗? 这是我的情况 class: 规则:

case class Rule(_id: BSONObjectID,
                metadata: Metadata,
                conditions: List[Condition])

元数据:


case class Metadata(
                     name: String,
                     description: String,
                     active: Boolean,
      // Optional:
                     refId: Option[BSONObjectID], 
                     keys: Option[List[Key]],
                     createdAt: Option[Instant],
                     lastVersionExists: Option[String],
                     data: Option[JsValue],
                     externalId: Option[String] 
                   )

条件:

case class Condition(name: String,
                     `type`: LogicType,
                     data: JsValue)

并写入和读取:

implicit object BSONInstantHandler extends BSONHandler[BSONDateTime, Instant] {
    def read(bson: BSONDateTime): Instant = Instant.ofEpochMilli(bson.value)
    def write(date: Instant) = BSONDateTime(date.toEpochMilli)
  }

  object LogicTypeReader extends BSONReader[BSONString, LogicType] {
    override def read(bson: BSONString): LogicType = LogicType.namesToValuesMap(bson.value)
  }

  object LogicTypeWriter extends BSONWriter[LogicType, BSONString] {
    override def write(t: LogicType): BSONString = BSONString(t.entryName)
  }

  // Reads
  implicit def LogicTypeReads: BSONReader[BSONString, LogicType] = LogicTypeReads
  implicit val MetadataReader: BSONDocumentReader[Metadata] = Macros.reader[Metadata]
  implicit val KeyReader: BSONDocumentReader[Key] = Macros.reader[Key]
  implicit val ConditionReader: BSONDocumentReader[Condition] = Macros.reader[Condition]
  implicit val RuleReader: BSONDocumentReader[Rule] = Macros.reader[Rule]

  // Writes
  implicit def LogicTypeWrites: BSONWriter[LogicType, BSONString] = LogicTypeWrites
  implicit val MetadataWrites: BSONDocumentWriter[Metadata] = Macros.writer[Metadata]
  implicit val KeyWrites: BSONDocumentWriter[Key] = Macros.writer[Key]
  implicit val ConditionWrites: BSONDocumentWriter[Condition] = Macros.writer[Condition]
  implicit val RuleWrites: BSONDocumentWriter[Rule] = Macros.writer[Rule]

谢谢!

解决了

"org.reactivemongo" %% "play2-reactivemongo" % "0.18.8-play27",
import reactivemongo.play.json.BSONFormats
import scala.language.implicitConversions

  implicit object JsValueHandler extends BSONHandler[BSONValue, JsValue] {
    implicit override def read(bson: BSONValue): JsValue = BSONFormats.toJSON(bson)
    implicit override def write(j: JsValue): BSONValue = BSONFormats.toBSON(j).get
  }