如何使用Play框架将字符串读作JSON中的长传

时间:2015-07-14 18:49:42

标签: json scala playframework

我在编写从客户端传递到服务器的JSON读取时遇到问题。当客户端应该是Numbers时,客户端总是将字符串作为字符串发送。当我尝试将它们读入Scala对象时,这会导致问题。我的case类期望它们是Long以与DB匹配,但我不确定如何读取String为Long。我尝试在employer_id上使用.readNullable [Long]但是它只返回它预期为jsnumber的验证错误。

Play-Scala 2.4.1

Scala 2.11.7

implicit val person_reads: Reads[Person] = (
    Reads.pure(-1L) and
    Reads.pure(None) and
    (JsPath \ "person" \ "given_name").readNullable[String](minLength[String](1)) and
    (JsPath \ "person" \ "surname").readNullable[String](minLength[String](1)) and
    (JsPath \ "person" \ "city").readNullable[String] and
    (JsPath \ "person" \ "state").readNullable[String] and
    (JsPath \ "person" \ "county").readNullable[String] and
    (JsPath \ "person" \ "zip").readNullable[String] and
    (JsPath \ "person" \ "country").readNullable[String] and
    (JsPath \ "person" \ "email").readNullable[String](email) and
    (JsPath \ "person" \ "phone").readNullable[String] and
    (JsPath \ "person" \ "employer_id").readNullable[String] and
    Reads.pure(Set[Long]()) and
    Reads.pure("") and
    Reads.pure("")
)(Person.apply _)

case class Person(
    id:Long, 
    facebook_id:Option[Long], 
    given_name:Option[String], 
    surname:Option[String], 
    city:Option[String], 
    state:Option[String], 
    county:Option[String], 
    zip:Option[String], 
    country:Option[String], 
    email:Option[String], 
    phone:Option[String], 
    employer_id:Option[Long], 
    people_connection_ids:Set[Long], 
    added:String, 
    modified:String
)

示例JSON POST / PUT

{
  "person": {
    "id": 78447,
    "facebook_id": 12345678987654321,
    "given_name": "Jon",
    "surname": "Smith",
    "city": "",
    "state": "",
    "county": "",
    "zip": "",
    "country": "",
    "email": "",
    "phone": "",
    "added": "",
    "modified": "",
    "employer_id": "1289592", <- This one gets passed as a string instead of number
    "people_connection_ids": [
      73
    ]
  }
}

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

您可以map将读取的字符串视为数字。

(__ \ "id").read[String].map[Long](_.toLong)

答案 1 :(得分:2)

扩展@cchantep,在您的情况下,

  (JsPath \ "person" \ "employer_id").readNullable[String].map(_.map{_.toLong}) and

答案 2 :(得分:0)

以下是使用play.api.libs.json

的答案
import play.api.libs.json._

val js = """
       | {
       |   "person": {
       |     "id": 78447,
       |     "facebook_id": 12345678987654321,
       |     "given_name": "Jon",
       |     "surname": "Smith",
       |     "city": "",
       |     "state": "",
       |     "county": "",
       |     "zip": "",
       |     "country": "",
       |     "email": "",
       |     "phone": "",
       |     "added": "",
       |     "modified": "",
       |     "employer_id": "1289592",
       |     "people_connection_ids": [
       |       73
       |     ]
       |   }
       | }""".stripMargin

val json = Json.parse(js)

(json \ "person" \ "employer_id").asOpt[String].map(f => java.lang.Long.valueOf(f))

输出

res0: Option[Long] = Some(1289592)