7

I'm using play.api.libs.json._ library. I have this kind of Scala class. I need to read / write this class in Json format. As there is no implicit reader/ writer for Timestamp. I have to provide my own. I tried couple ways unfortunately none of them worked. Could you please suggest me how it is done? Thanks in advance!

case class Event(id: Long, startTime: Option[java.sql.Timestamp] = None, endTime: Option[java.sql.Timestamp] = None)

I would like to POST / GET in following Json format

{
  "id": 1,
  "startTime": "2011-10-02 18:48:05.123456",
  "endTime": "2011-10-02 20:48:05.123456"
}
acjay
  • 34,571
  • 6
  • 57
  • 100
masiboo
  • 4,537
  • 9
  • 75
  • 136

2 Answers2

10

just add before Json Reader or Json Format for Event class

import play.api.libs.json.Json._
import play.api.libs.json._ 

def timestampToDateTime(t: Timestamp): DateTime = new DateTime(t.getTime)

def dateTimeToTimestamp(dt: DateTime): Timestamp = new Timestamp(dt.getMillis)

implicit val timestampFormat = new Format[Timestamp] {

    def writes(t: Timestamp): JsValue = toJson(timestampToDateTime(t))

    def reads(json: JsValue): JsResult[Timestamp] = fromJson[DateTime](json).map(dateTimeToTimestamp)

  }
Lucky Libora
  • 140
  • 1
  • 3
  • toJson, timestampToDateTime, fromJson, dateTimeToTimestamp undefined compiler error. Do I need to import some additional library? – masiboo Mar 03 '15 at 07:27
  • sorry, I forgot to add implementation for this functions – Lucky Libora Mar 03 '15 at 08:42
  • 2
    dateTimeToTimestamp takes DateTime parameter. So how to call def reads(json: JsValue): JsResult[Timestamp] = fromJson[DateTime](json).map(dateTimeToTimestamp)? Still toJson, fromJson undefined. – masiboo Mar 04 '15 at 11:21
  • dont forget to add import play.api.libs.json.Json._ or add Json. before toJson, fromJson – Lucky Libora Mar 04 '15 at 16:01
  • to parse Timestamp you can just use Json.fromJson[Timestamp](json). timestampFormat will be used implicitly – Lucky Libora Mar 04 '15 at 16:02
  • It seems ur answer is converting between DateTime and Timestamp. But my question was how to convert json which comes as String. So how to convert json/String to Timestamp. I dont need DateTime. Coz play provides DateTime implicit reader and writer. Please update ur answer if u know how to do it. Thanks! – masiboo Mar 05 '15 at 08:42
  • I also needed to import `play.api.libs.json.JodaReads._` and `play.api.libs.json.JodaWrites._` – lfk Oct 22 '18 at 23:38
  • 2
    which DateTime class does this use? – Simon Mar 26 '20 at 14:40
5

What I did code for a personal project:

implicit object timestampFormat extends Format[Timestamp] {
  val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
  def reads(json: JsValue) = {
    val str = json.as[String]
    JsSuccess(new Timestamp(format.parse(str).getTime))
  }
  def writes(ts: Timestamp) = JsString(format.format(ts))
}

And don't forget to import this:

import java.sql.Timestamp
import java.text.SimpleDateFormat
import play.api.Play.current
import play.api.libs.json._

It respects Javascript dates standard.

Source: https://github.com/BinaryBrain/Gamers/blob/master/server/play/app/models/Package.scala

Binary Brain
  • 1,170
  • 8
  • 20