Play框架JSON读取:如何读取String或Int?

问题描述 投票:7回答:3

rest api的JS客户端可以将int和string作为某个字段的值发送。

{
   field1: "123",
   field2: "456"
}

{
   field1: 123,
   field2: 456
}

以下是应该转换json请求正文的case类的play动作:

  case class Dto(field1: Int, field2: Int)
  object Dto {
    implicit val reads = Json.reads[Dto]
  } 

  def create = Action.async(BodyParsers.parse.json) { implicit request =>
    request.body.validate[Dto].map {
      dto => someService.doStuff(dto).map(result => Ok(Json.toJson(result)))
    }.recoverTotal {
      e => jsErrorToBadRequest(e)
    }
  }

如果我发送带有int值的json值,它可以正常工作。但是如果field1或field2是字符串(“123”,“456”),则它会失败,因为request.body.validate需要Int。

但问题是JS客户端从输入字段发送值,输入字段转换为字符串。

处理整数或字符串的最佳方法是什么? (所以这个动作应该在两种情况下都将json转换为dto)

json scala playframework playframework-2.2
3个回答
6
投票

你也可以定义一个更宽容的Reads[Int]。并用它来定义你的Reads[Dto]

1)定义更宽容的Reads[Int]

  import play.api.data.validation.ValidationError
  import play.api.libs.json._
  import scala.util.{Success, Try}

  // Define a more tolerant Reads[Int]
  val readIntFromString: Reads[Int] = implicitly[Reads[String]]
      .map(x => Try(x.toInt))
      .collect (ValidationError(Seq("Parsing error"))){
          case Success(a) => a
      }

 val readInt: Reads[Int] = implicitly[Reads[Int]].orElse(readIntFromString)

例子:

readInt.reads(JsNumber(1))
// JsSuccess(1,)

readInt.reads(JsString("1"))
//  JsSuccess(1,)

readInt.reads(JsString("1x"))
// JsError(List((,List(ValidationError(List(Parsing error),WrappedArray())))

2)使用你更宽容的Reads[Int]来定义你的Reads[Dto]

implicit val DtoReads = 
    (JsPath \ "field1").read[Int](readInt) and 
    (JsPath \ "field2").read[Int](readInt)

编辑:与millhouse的解决方案的差异:

  • 如果field1是一个字符串而field2是一个int这个解决方案你会得到一个JsSuccessJsError与millhouse的解决方案
  • 如果此解决方案的两个字段都无效,您将获得包含每个字段一个错误的JsError。使用millhouse的解决方案,您将收到第一个错误。

2
投票

你需要为你的Reads定制Dto实现 - 即Reads[Dto]。我总是喜欢从Json.reads[Dto]获得的“内置”(宏生成)开始 - 然后从那里开始;例如。:

object Dto {
  val basicReads = Json.reads[Dto]

  implicit val typeCorrectingReads = new Reads[Dto]{

    def reads(json: JsValue): JsResult[Dto] = {

      def readAsInteger(fieldName:String):JsResult[Int] = {
        (json \ fieldName).validate[String].flatMap { s =>
          // We've got a String, but it might not be convertible to an int...
          Try(s.toInt).map(JsSuccess(_)).getOrElse {
            JsError(JsPath \ fieldName, s"Couldn't convert string $s to an integer")
          }
        }
      }

      basicReads.reads(json).orElse {
        for {
          f1 <- readAsInteger("field1")
          f2 <- readAsInteger("field2")
        } yield {
          Dto(f1, f2)
        }
      }
    }
  }
}

通过这种方式,你得到basicReads在“快乐的情况下”做的工作。如果它没有成功,我们会尝试将字段视为String实例,然后再尝试转换为Int

请注意,尽管如此,我们仍然在由“其他人”创建的JsResult范围内工作,因此我们将快速失败。


1
投票

使用or组合器(此处记录为https://www.playframework.com/documentation/2.6.x/ScalaJsonCombinators)和少量读取功能实际上非常容易。

case class Customer(name: String, number: Int)

object Customer {
  val readIntFromString: Reads[Int] = implicitly[Reads[String]]
    .map(x => x.toInt)

  import play.api.libs.functional.syntax._
  import play.api.libs.json._

  implicit val routeReads: Reads[Customer] =
    ((__ \ "name").read[String] and
      ((__ \ "number").read[Int] or
        (__ \ "number").read[Int](readIntFromString)))(Customer.apply _)
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.