2014-10-15 6 views
9

मैं JSON पढ़ने के माध्यम से ऑब्जेक्ट का निर्माण करते समय निरंतर मान का उपयोग करना चाहता हूं।स्कैला प्ले जेएसओएन में लगातार मूल्य

उदाहरण के लिए वर्ग होगा:

case class UserInfo(
    userId: Long = -1, 
    firstName: Option[String] = None, 
    lastName: Option[String] = None 
) 

और पढ़ने होगा:

implicit val userRead: Reads[UserInfo] = (
     (JsPath \ "userId").read[Long] and 
     (JsPath \ "firstName").readNullable[String] and 
     (JsPath \ "lastName").readNullable[String] 
    )(UserInfo.apply _) 

लेकिन मैं JSON ऑब्जेक्ट में userId के लिए मान निर्दिष्ट करने के लिए नहीं करना चाहती। मैं रीड कोडिंग के बारे में कैसे जाउंगा ताकि -1 का मान हमेशा JSIn ऑब्जेक्ट में निर्दिष्ट किए बिना UserInfo ऑब्जेक्ट में बनाया गया हो?

उत्तर

9

उपयोग Reads.pure

implicit val userRead: Reads[UserInfo] = (
    Reads.pure(-1L) and 
    (JsPath \ "firstName").readNullable[String] and 
    (JsPath \ "lastName").readNullable[String] 
)(UserInfo.apply _) 
0

धन्यवाद!

implicit val userRead: Reads[UserInfo] = (
    Reads.pure(-1:Long) and 
    (JsPath \ "firstName").readNullable[String] and 
    (JsPath \ "lastName").readNullable[String] 
)(UserInfo.apply _) 
:

मैं एक मामूली परिवर्तन करने के लिए यह एक लंबे समय के लिए मजबूर किया था

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