Skip to content

Instantly share code, notes, and snippets.

@djamelz
Created September 29, 2016 12:28
Show Gist options
  • Save djamelz/f963cab45933d05b2e10d8680e4213b6 to your computer and use it in GitHub Desktop.
Save djamelz/f963cab45933d05b2e10d8680e4213b6 to your computer and use it in GitHub Desktop.
custom deserialization with json4s for date in string format to timestamp (long)
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.json4s._
import org.json4s.native.JsonMethods._
val datePattern = "yyyy-MM-dd hh:mm:ss.SSSSSS"
val s = """{"timestamp": "2016-09-29 11:31:13.247772", "domain": "d1", "filePath": "s3://..."}"""
case class Event(domain : String, filePath : String, timestamp : Long)
object StringToLong extends CustomSerializer[Long](format => (
{ case JString(x) => DateTime.parse(x, DateTimeFormat.forPattern(datePattern)).getMillis},
{ case x: Long => JInt(x) }))
implicit val formats = DefaultFormats + StringToLong
val event = parse(s).extract[Event]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment