Marchalling and unmarchalling Timestamps

1,177 views
Skip to first unread message

Jacobus

unread,
Jul 18, 2013, 4:03:54 AM7/18/13
to spray...@googlegroups.com
Good morning,

I'm looking for advice on creating a marshaller and unmarshaller for a java.sql.Timestamp. I found this bit of code, which looks fine, bar the fact that you would be dealing with a specific field name, 'date' in this case. How can I go about making it a bit more generic, so that I can use the current object's member name, and not 'date' in this case.

implicit object TimestampFormat extends RootJsonFormat[Timestamp] { def write(obj: Timestamp) = { JsObject("date" -> JsNumber(obj.getTime)) } def read(json: JsValue) = { json.asJsObject().getFields("date") match { case Seq(JsNumber(time)) => new Timestamp(time.toLong) case _ => throw new DeserializationException("Date expected") } } }

Best regards,
Jacobus

Mathias

unread,
Jul 18, 2013, 4:05:41 AM7/18/13
to spray...@googlegroups.com
Jacobus,

what exactly do you want to marshal `java.sql.Timestamp` to?
Can you show an example?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jacobus

unread,
Jul 18, 2013, 4:17:55 AM7/18/13
to spray...@googlegroups.com
Hi there Mathias,

The object I'm trying to convert to json looks like this:

import java.sql.{ Timestamp }

case class UserTask(
  user_id: Long,
  label: String,
  created: Timestamp,
  completed: Timestamp)

But when I add it to my json protocol object:

object MyJsonProtocol extends DefaultJsonProtocol {
    implicit val jsonUserTask = jsonFormat4(UserTask)
}

I get an error:
could not find implicit value for evidence parameter of type com.dci.rest.MyJsonProtocol.JF[java.sql.Timestamp]

So I'm trying to find a generic solution, because I work with timestamps quite often. Up to now I have been avoiding the problem by passing time as strings or longs, but it's a pain in the neck.

Best regards,
Jacobus


You received this message because you are subscribed to a topic in the Google Groups "spray-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spray-user/nJBCaCiFvGo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spray-user+...@googlegroups.com.

Age Mooij

unread,
Jul 18, 2013, 4:24:32 AM7/18/13
to spray...@googlegroups.com
What you need is a non-root JsonFormat for purely serializing the timestamp value. This will then be picked up by the root format of your UserTask class.

This should work:
implicit object TimestampFormat extends JsonFormat[Timestamp] {
  def write(obj: Timestamp) = JsNumber(obj.getTime)
    
  def read(json: JsValue) = json match {
    case JsNumber(time) => new Timestamp(time.toLong)
    case _ => throw new DeserializationException("Date expected")
  }
}
Note the JsonFormat instead of RootJsonFormat.

Age

Mathias

unread,
Jul 18, 2013, 4:25:30 AM7/18/13
to spray...@googlegroups.com
Jacobus,

how about something like this:

implicit object TimestampJsonFormat extends JsonFormat[Timestamp] {
def write(x: Timestamp) = JsNumber(x.getTime)
def read(value: JsValue) = value match {
case JsNumber(x) => new TimeStamp(x.longValue)
case x => deserializationError("Expected Timestamp as JsNumber, but got " + x)
}
}

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Mathias

unread,
Jul 18, 2013, 4:26:46 AM7/18/13
to spray...@googlegroups.com
Damn, Age, you beat me by about 25 seconds! :)

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Age Mooij

unread,
Jul 18, 2013, 4:28:52 AM7/18/13
to spray...@googlegroups.com
First! ;)

Age

Jacobus

unread,
Jul 18, 2013, 4:32:14 AM7/18/13
to spray...@googlegroups.com
Thanks Age and Mathias, that seems to do the trick.

It's simply not fair that you guys are so darn clever :-p (I'm good at baking chocolate cookies though!)

Have a good one,
Jacobus

Mathias

unread,
Jul 18, 2013, 4:35:28 AM7/18/13
to spray...@googlegroups.com
> (I'm good at baking chocolate cookies though!)

I hope they conform to the spec!
We kind of get annoyed with people producing cookies that we have to relax the parser for! ;^)

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Reply all
Reply to author
Forward
0 new messages