Im trying to write a case class to BSONCollection, but i cannot find any Reads and Writes to JsValue. Is there a BSONDocumentWriter and BSONDocumentReader for JsValue? Oh any suggesting for how to write one? this is my case class: Rule:
case class Rule(_id: BSONObjectID,
metadata: Metadata,
conditions: List[Condition])
Metadata:
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]
)
Condition:
case class Condition(name: String,
`type`: LogicType,
data: JsValue)
and writes and reads:
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]
Thanks!
Solved it
"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
}