0

I am using scala pickling library(0.9.0-snapshot) for Json serializing.

I would like to write a custom pickler and unpickler for serializing and deserializing sql.Timestamp fields as normal fields, I am able to serialize the time stamp field by overriding the pickle method in Spickler, I also want to desrialize a timestamp value in json string( string value) to timestamp field. Is it possible?

I mean,

case class T1(id: Long, name: String, time: Timestamp)

to json as

 {
      "tpe" : "T1",
      "id"  : "42",
      "name": "Name",
      "time": "2014-10-17 17:19:29.97"
  }

is possible, but unable to unpickle back to case class as

T1(42, "Name", new Timestamp(new Date().getTime)).pickle.value.unpickle[T1]
// Shows error scala.MatchError: 2014-10-17 17:19:29.97 (of class java.lang.String)

My current Implementation looks like this.

class TimestampPickler(implicit val format: PickleFormat) extends SPickler[Timestamp] with Unpickler[Timestamp] {

  private val stringUnpickler = implicitly[Unpickler[String]]    

  override def pickle(picklee: Timestamp, builder: PBuilder): Unit = {    
    builder.hintTag(FastTypeTag.String).beginEntry(picklee.toString).endEntry()    
  }

  override def unpickle(tag: => FastTypeTag[_], reader: PReader): Timestamp = {
       // ToDo
    }

}

  implicit def genTimestampPickler(implicit format: PickleFormat) = new TimestampPickler
S.Karthik
  • 1,389
  • 9
  • 21
  • I'm not sure quite what you're asking. Though I can tell you that your custom pickle method doesn't at all match the shape of the object you're trying to pickle. You've got to pickle each field that's necessary to pickle. E.g., https://github.com/scala/pickling/issues/33#issuecomment-25039017 – Heather Miller Oct 18 '14 at 16:48
  • @HeatherMiller, My requirement is simple, you can see the json string and case class T1 in my question. I want to convert that json to the case class object. Thats all. – S.Karthik Oct 20 '14 at 04:29

0 Answers0