我正在尝试编写一个函数来编写可能具有Infinity值的双精度(在JSON中不存在)。
以下是我想要实现的一些例子:
OpenQuery
到目前为止,我已经创建了一个增强的JsPath类,并添加了名为Input: Double.PositiveInfinity
Output:
{
"positiveInfinty": true,
"negativeInfinty": false,
"value": null
}
Input: 12.3
Output:
{
"positiveInfinty": false,
"negativeInfinty": false,
"value": 12.3
}
的函数:
writeInfinite
代码全部编译,这是我正在使用的测试:
case class EnhancedJsPath(jsPath: JsPath) {
private def infinityObject(pos: Boolean, neg: Boolean, value: Option[Double]): JsObject = Json.obj(
"positiveInfinity" -> pos,
"negativeInfinity" -> neg,
"value" -> value
)
def writeInfinite: OWrites[Double] = OWrites[Double] { d =>
if (d == Double.PositiveInfinity) { infinityObject(true, false, None) }
else if (d == Double.NegativeInfinity) { infinityObject(false, true, None) }
else { infinityObject(false, false, Some(d)) }
}
}
object EnhancedJsPath {
implicit def jsPathToEnhancedJsPath(jsPath: JsPath): EnhancedJsPath = EnhancedJsPath(jsPath)
}
测试失败,因为case class Dummy(id: String, value: Double)
object Dummy {
implicit val writes: Writes[Dummy] = (
(JsPath \ "id").write[String] and
(JsPath \ "value").writeInfinite
)(unlift(Dummy.unapply))
}
test("EnhancedJsPath.writesInfinite should set positive infinity property") {
val d = Dummy("123", Double.PositiveInfinity, Some(Double.PositiveInfinity))
val result = Json.toJson(d)
val expected = Json.obj(
"id" -> "123",
"value" -> Json.obj(
"positiveInfinity" -> true,
"negativeInfinity" -> false,
"value" -> JsNull
)
)
assert(result == expected)
}
的值为:
result
而不是:
{
"id": "123",
"positiveInfinity": true,
"negativeInfinity": false,
"value": null
}
我无法弄清楚如何修改我的{
"id": "123",
"value": {
"positiveInfinity": true,
"negativeInfinity": false,
"value": null
}
}
以尊重路径。
答案 0 :(得分:3)
查看JsPath#write[A]
或JsPath#writeNullable[A]
有助于:
def write[T](implicit w: Writes[T]): OWrites[T] = Writes.at[T](this)(w)
Writes.at
接受JsPath
,这就是write
和writeNullable
保留所调用路径的方式。您需要做的就是用writeInfinite
包装当前的jsPath
实现,并传递def writeInfinite: OWrites[Double] = Writes.at(jsPath)(OWrites[Double] { d =>
if (d == Double.PositiveInfinity) infinityObject(true, false, None)
else if (d == Double.NegativeInfinity) infinityObject(false, true, None)
else infinityObject(false, false, Some(d))
})
值:
scala> Json.toJson(d)
res5: play.api.libs.json.JsValue = {"id":"123","value":{"positiveInfinity":true,"negativeInfinity":false,"value":null}}
它有效:
appendChild