简体   繁体   English

如何在播放 json 中创建一个空的 JsResult?

[英]How to create an empty JsResult in play json?

I am writing an implicit reads returning JsResult[Seq[T]] based on a condition:我正在编写一个基于条件返回JsResult[Seq[T]]的隐式读取:

some_condition match{
     case Some(value) => // JsResult[Seq[T]]
     case _ => // returning an empty JsResult here of similar type?
}

What would be the way for return such JsResult ?返回此类JsResult的方式是什么?

You will need to use the method validate , which returns JsResult .您将需要使用返回JsResult的方法validate There are 2 case classes that inherits JsResult :有 2 个案例类继承JsResult

  1. JsSuccess
  2. JsError

Therefore you can consider the following example:因此,您可以考虑以下示例:

case class T()
implicit val reads = Json.reads[T]
val jsValue: JsValue = ...

jsValue.validate[Seq[T]] match {
  case JsSuccess(t, path) =>
    println(t)
  case JsError(errors) =>
    println(errors)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM