Why I get “Application does not take parameters” using JSON Read with Play framework 2.3?

限于喜欢 提交于 2020-01-03 13:02:13

问题


I wanna write JSON validation for a few Scala model classes in Play framework 2.3x. I'm using JSON Reads to do that following the instructions (https://playframework.com/documentation/2.3.x/ScalaJsonCombinators). But I get "Application does not take parameters" error and I don't know how to fix this.

Here is my code.

package models

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
import reactivemongo.bson.BSONObjectID
import java.util.Date

case class ArtifactModel(
                          _id: BSONObjectID,
                          name: String,
                          createdAt: Date,
                          updatedAt: Date,
                          attributes: List[AttributeModel],
                          stateModels: List[StateModel])

case class AttributeModel(
                           name: String,
                           comment: String)

case class StateModel(
                       name: String,
                       comment: String)

object ArtifactModel {
  implicit val artifactModelReads: Reads[ArtifactModel] = (
      (__ \ "_id").readNullable[String] ~
      (__ \ "name").read[String] ~
      (__ \ "createdAt").readNullable[Long] ~
      (__ \ "updatedAt").readNullable[Long] ~
      (__ \ "attributes").read[List[AttributeModel]] ~
      (__ \ "stateModels").read[List[StateModel]]
    )(ArtifactModel) // here is the error: "Application does not take parameters"


  implicit val attributeModelReads: Reads[AttributeModel] = (
      (__ \ "name").read[String] ~
      (__ \ "comment").read[String]
    )(AttributeModel)

  implicit val stateModelReads: Reads[StateModel] = (
      (__ \ "name").read[String] ~
      (__ \ "comment").read[String]
    )(StateModel)
}

Can you help me? Any solution or suggestions for JSON validation in Scala/Play are appreciated.


回答1:


The types of the Reads object are not the same as those the apply method takes. E.g., readNullable[String] results Option[String], not String. Same for the BSONObjectId and the Date. This compiles, but you probably need to use some maps:

  implicit val artifactModelReads: Reads[ArtifactModel] = (
(__ \ "_id").read[BSONObjectID] ~
  (__ \ "name").read[String] ~
  (__ \ "createdAt").read[Date] ~
  (__ \ "updatedAt").read[Date] ~
  (__ \ "attributes").read[List[AttributeModel]] ~
  (__ \ "stateModels").read[List[StateModel]]
)(ArtifactModel.apply _)

You can after a read, like so (CONVERT_TO_DATE is imaginary):

  implicit val artifactModelReads: Reads[ArtifactModel] = (
(__ \ "_id").read[BSONObjectID] ~
  (__ \ "name").read[String] ~
  (__ \ "createdAt").read[String].map( s=>CONVERT_TO_DATE(s) ) ~
  (__ \ "updatedAt").read[Date] ~
  (__ \ "attributes").read[List[AttributeModel]] ~
  (__ \ "stateModels").read[List[StateModel]]
)(ArtifactModel.apply _)


来源:https://stackoverflow.com/questions/29361994/why-i-get-application-does-not-take-parameters-using-json-read-with-play-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!