2014-10-15 4 views
9

JSON 읽기를 통해 개체를 만들 때 상수 값을 사용하고 싶습니다.스칼라에서 상수 값 사용 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를위한 값을 지정할 필요가 싶지 않아요. Read 값을 -1로 지정하면 읽는 JSON 객체에 -1을 지정하지 않고 UserInfo 객체에 -1 값이 항상 만들어 지도록 읽기를 코딩하는 방법은 무엇입니까?

답변

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

나는 긴 머리를 강제로 하나의 작은 변화를했습니다