Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 547
Raysmond Avatar asked Mar 31 '15 06:03

Raysmond


1 Answers

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 _)
like image 98
Michael Bar-Sinai Avatar answered Nov 15 '22 03:11

Michael Bar-Sinai