Custom JodaTime serializer using Play Framework's JSON library?

前端 未结 3 531
难免孤独
难免孤独 2020-12-28 08:35

How do I implement a custom JodaTime\'s DateTime serializer/deserializer for JSON? I\'m inclined to use the Play Framework\'s JSON library (2.1.1). There is a default DateTi

3条回答
  •  醉梦人生
    2020-12-28 09:23

    I use Play 2.3.7 and define in companion object implicit reads/writes with string pattern:

    case class User(username:String, birthday:org.joda.time.DateTime)
    
    object User {
      implicit val yourJodaDateReads = Reads.jodaDateReads("yyyy-MM-dd'T'HH:mm:ss'Z'")
      implicit val yourJodaDateWrites = Writes.jodaDateWrites("yyyy-MM-dd'T'HH:mm:ss'Z'")
      implicit val userFormat = Json.format[User]
    }
    

提交回复
热议问题