2015-03-31 9 views
5

Voglio scrivere la convalida JSON per alcune classi di modelli Scala in Play framework 2.3x. Sto usando JSON Reads per farlo seguendo le istruzioni (https://playframework.com/documentation/2.3.x/ScalaJsonCombinators). Ma ottengo l'errore "L'applicazione non accetta parametri" e non so come risolvere questo problema.Perché ottengo "L'applicazione non accetta parametri" utilizzando JSON Read with Play framework 2.3?

Ecco il mio codice.

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

Potete aiutarmi? Le soluzioni o i suggerimenti per la convalida JSON in Scala/Play sono apprezzati.

risposta

8

I tipi dell'oggetto Reads non sono gli stessi di quelli utilizzati dal metodo apply. Ad es., readNullable[String] risultati Option[String], non String. Lo stesso vale per lo BSONObjectId e lo Date. Questo compila, ma probabilmente necessario utilizzare alcune mappe:

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

È possibile dopo una lettura, in questo modo (CONVERT_TO_DATE è immaginario):

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 _) 
+0

ho già provarci ma non funziona per me. L'errore è lo stesso. Non ho capito bene. – Raysmond

+0

Trova la cosa da compilare e aggiorna la risposta. –

+0

Grazie mille. Funziona in questo modo. – Raysmond