我将 ReactiveMongo 0.11.11 用于 Play 2.5,并希望将 BSONDocument 转换为 JsObject。
对于大多数 BSON 数据类型(String、Int...),默认值完全可以让库完成这项工作。对于 BSON 类型 DateTime ( BSONDateTime
),JSON 属性的值没有给我所需的格式。
Date 的 JSON 值是一个 JsObject,$date
其值为属性名称和 UNIX 时间戳(以毫秒为单位):
{
"something": {
"$date": 1462288846873
}
}
我想要的 JSON 是这样的 Date 的字符串表示形式:
{
"something": "2016-05-03T15:20:46.873Z"
}
不幸的是,我不知道如何在不重写所有内容或更改库本身的代码的情况下覆盖默认行为。
这就是我认为它发生的地方(源代码):
val partialWrites: PartialFunction[BSONValue, JsValue] = {
case dt: BSONDateTime => Json.obj("$date" -> dt.value)
}
我的版本必须如下所示:
val partialWrites: PartialFunction[BSONValue, JsValue] = {
case dt: BSONDateTime =>
JsString(Instant.ofEpochMilli(dt.value).toString)
}
是否可以覆盖该位?
我创建了一个实验...
import java.time.Instant
import play.api.libs.json._
import reactivemongo.bson._
import reactivemongo.play.json.BSONFormats.BSONDocumentFormat
object Experiment {
// Original document (usually coming from the database)
val bson = BSONDocument(
"something" -> BSONDateTime(1462288846873L) // equals "2016-05-03T15:20:46.873Z"
)
// Reader: converts from BSONDateTime to JsString
implicit object BSONDateTimeToJsStringReader extends BSONReader[BSONDateTime, JsString] {
def read(bsonDate: BSONDateTime): JsString = {
JsString(Instant.ofEpochMilli(bsonDate.value).toString)
}
}
// Reader: converts from BSONDateTime to JsValue
implicit object BSONDateTimeToJsValueReader extends BSONReader[BSONDateTime, JsValue] {
def read(bsonDate: BSONDateTime): JsValue = {
JsString(Instant.ofEpochMilli(bsonDate.value).toString)
}
}
// Read and print specific property "something" using the `BSONReader`s above
def printJsDate = {
val jsStr: JsString = bson.getAs[JsString]("something").get
println(jsStr) // "2016-05-03T15:20:46.873Z"
val jsVal: JsValue = bson.getAs[JsValue]("something").get
println(jsVal) // "2016-05-03T15:20:46.873Z"
}
// Use ReactiveMongo's default format to convert a BSONDocument into a JsObject
def printAsJsonDefault = {
val json: JsObject = BSONDocumentFormat.writes(bson).as[JsObject]
println(json) // {"something":{"$date":1462288846873}}
// What I want: {"something":"2016-05-03T15:20:46.873Z"}
}
}
我想指出,当我将 BSONDocument 转换为 JsObject 时,BSONDateTime 到 JsValue 的转换应该始终有效,而不仅仅是在我手动选择特定的已知属性时。这意味着我的示例中的属性“某物”可以具有任何名称,也可以出现在子文档中。
顺便说一句:如果你想知道,我通常在我的 Play 项目中使用 BSON 集合,但我认为无论如何在这种情况下它不会有什么不同。
编辑
我试过提供一个Writes[BSONDateTime]
,但不幸的是它没有被使用,我仍然得到和以前一样的结果。代码:
import java.time.Instant
import play.api.libs.json._
import reactivemongo.bson.{BSONDocument, BSONDateTime}
object MyImplicits {
implicit val dateWrites = Writes[BSONDateTime] (bsonDate =>
JsString(Instant.ofEpochMilli(bsonDate.value).toString)
)
// I've tried this too:
// implicit val dateWrites = new Writes[BSONDateTime] {
// def writes(bsonDate: BSONDateTime) = JsString(Instant.ofEpochMilli(bsonDate.value).toString)
// }
}
object Experiment {
// Original document (usually coming from the database)
val bson = BSONDocument("something" -> BSONDateTime(1462288846873L))
// Use ReactiveMongo's default format to convert a BSONDocument into a JsObject
def printAsJson = {
import reactivemongo.play.json.BSONFormats.BSONDocumentFormat
import MyImplicits.dateWrites // import is ignored
val json: JsObject = BSONDocumentFormat.writes(bson).as[JsObject]
//val json: JsValue = Json.toJson(bson) // I've tried this too
println(json) // {"something":{"$date":1462288846873}}
}
}