serializing objects to json with play.api.libs.json

前端 未结 2 495
陌清茗
陌清茗 2021-02-01 07:34

I\'m trying to serialize some relatively simple models into json. For example, I\'d like to get the json representation of:

case class User(val id: Long, val fir         


        
2条回答
  •  梦如初夏
    2021-02-01 08:02

    Thanks to the fact User is a case class you could also do something like this:

    implicit val userImplicitWrites = Json.writes[User]
    val jsUserValue = Json.toJson(userObject)
    

    without writing your own Format[User]. You could do the same with reads:

    implicit val userImplicitReads = Json.reads[User]
    

    I haven't found it in the docs, here is the link to the api: http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.libs.json.Json$

提交回复
热议问题