2015-03-31 5 views
5

मैं प्ले फ्रेमवर्क 2.3x में कुछ स्कैला मॉडल कक्षाओं के लिए जेएसओएन सत्यापन लिखना चाहता हूं। मैं निर्देशों का पालन करने के लिए जेएसओएन रीड का उपयोग कर रहा हूं (https://playframework.com/documentation/2.3.x/ScalaJsonCombinators)। लेकिन मुझे लगता है कि "एप्लिकेशन पैरामीटर नहीं लेता है" त्रुटि और मुझे नहीं पता कि इसे कैसे ठीक किया जाए।प्ले फ्रेमवर्क 2.3 के साथ जेएसओएन रीड का उपयोग करके मुझे "पैरामीटर नहीं लेते हैं" क्यों मिलता है?

मेरा कोड यहां है।

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

क्या आप मेरी मदद कर सकते हैं? स्कैला/प्ले में जेएसओएन सत्यापन के लिए कोई समाधान या सुझाव सराहना की जाती है।

उत्तर

8

रीड ऑब्जेक्ट के प्रकार वही नहीं हैं जो लागू विधि लेते हैं। उदा।, readNullable[String] परिणाम Option[String], String नहीं। BSONObjectId और Date के लिए वही। यह संकलित है, लेकिन आप शायद कुछ नक्शे उपयोग करने की आवश्यकता:

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

आप पढ़ने के बाद, इसलिए जैसे (CONVERT_TO_DATE काल्पनिक है) कर सकते हैं:

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

मैं पहले से ही है कि कोशिश लेकिन इसके लिए काम नहीं करता है मुझे। त्रुटि वही है। मैं उलझन में हूं। – Raysmond

+0

को संकलित करने और उत्तर को अपडेट करने के लिए चीज़ मिल गई। –

+0

बहुत बहुत धन्यवाद। यह इस तरह से काम करता है। – Raysmond

संबंधित मुद्दे