简体   繁体   中英

Map JSON to nested case class play framework

I need to receive Json data and bind it to a case class that contains other case class in parameters.

I don't even know what to write in the controller, there's no help in the documentation neither, I've look everywhere else and didn't find any answer.

Here are the case classes:

case class InfoForm(nomEntreprise: String, siren: String, dateCreation: String, entreeRelation: String, secteur: String, cotationBDF: String, montantPrivileges: String, fcc: String, ca: String, resultatBrut: String, ebe: String, totalBilan: String, fp: String)

    object InfoForm {
      implicit val format = Json.format[InfoForm]
    }

    case class Associate(tiersAssoc: String, nom: String, prenom: String, birthday: Date)

    object Associate {
      implicit val assocFormat = Json.format[Associate]
    }

    case class AssociateForm(nbAssoc: Int, cotation: String, assoc: Seq[Associate], fccAssociate: String, ficp: String)

    object AssociateForm {
      implicit val format = Json.format[AssociateForm]
    }

    case class OperationForm(mntAcquisition: String, codePostal: String, typePret: String, mntFinance: String, mensualite: String, loyerPrevisionnel: String)

    object OperationForm {
      implicit val format = Json.format[OperationForm]
    }

    case class CompanyForm(sci: Boolean, infoForm: InfoForm, associateForm: AssociateForm, operationForm: OperationForm)

    object CompanyForm {
      implicit val format = Json.format[CompanyForm]
    }

Json:

{
    "sci": true,
    "nomEntreprise": "nom entreprise",
    "siren": "siren",
    "dateCreation": "1977-04-22T01:00:00-05:00",
    "entreeRelation": "1977-04-22T01:00:00-05:00",
    "secteur": "un secteur",
    "cotationBDF": "cotation",
    "montantPrivileges": "montant",
    "fcc": "fcc",
    "ca": "ca c est un option attention",
    "resultatBrut": "resultat",
    "ebe": "ebe",
    "totalBilan": "totalBilan",
    "fp": "fp",
    "nbAssoc": 1,
    "cotation": "une chaine",
    "assoc": [
        {
            "tiersAssoc": "une chaine",
            "nom": "name",
            "prenom": "prenom",
            "birthday":"1977-04-22T01:00:00-05:00"
        }
    ],
    "fccAssociate": "une autre chaine",
    "ficp": "encore une autre chaine",
    "mntAcquisition": "montant acquisition",
    "codePostal": "code postal",
    "typePret": "typePret",
    "mntFinance": "montant finance",
    "mensualite": "string",
    "loyerPrevisionnel": "derniere !"
}

And here's what I've tried so far in the controller :

def setCompanyForm(id: String) = {
    Errors.collect {
      silhouette.SecuredAction.async { implicit request =>
        Errors.handle {
          val companyForm = request.body.asJson
          companyForm match {
            case Some(json) => println(Json.fromJson[CompanyForm](json))
            case None => println("rien")
          }

          Future.successful(Ok(""))
        }
      }
    }
  }

There is absolutely no log when I print.

According to the title I try to help you with the Json Mapping:

I created a ScalaFiddle , so you can try it yourself.

That I could start I replaced the Date with a simple String, because you missed to have a formatter for Date (and it is not clear what Date you have).

So running this is not a success, because of the wrong Type:

Json.parse(json).validate[CompanyForm]

After fixing that everything works as expected: ScalaFiddle v.2

Json.parse(json).validate[InfoForm]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM