Why am I getting “Application does not accept parameters” using JSON Read with Play Framework 2.3?

I want to write a JSON check for several classes of Scala models in Play Framework 2.3x. I use JSON Reads for this, following the instructions ( https://playframework.com/documentation/2.3.x/ScalaJsonCombinators ). But I get the error "Application does not accept parameters", and I do not know how to fix it.

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) } 

Could you help me? Any solutions or suggestions for checking JSON in Scala / Play are appreciated.

+5
source share
1 answer

The types of the Reads object do not match the types of the Apply method. For example, readNullable[String] results in Option[String] , not String . The same goes for BSONObjectId and 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 reading, for example, ( 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 _) 
+8
source

Source: https://habr.com/ru/post/1216504/


All Articles