Pergunta

I'm using lift-json (lift 2.6-M3) and trying to handle json data which has epoch values like

"updatetime": 1249409679,

Of course lift-json can treat them as JInt values and extract objects as Long fields, but is there a way to treat them as Date (or DateTime etc) objects?

Added 5/16/2014: The following code seems to work.

implicit val formats = DefaultFormats + new Serializer[java.util.Date] {
    def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), java.util.Date] = {
        case (t, JInt(num)) if (t.clazz == classOf[java.util.Date]) => new java.util.Date(num.toLong * 1000)
    }
    def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
        case x if (x.getClass == classOf[java.util.Date]) => new JInt(x.asInstanceOf[java.util.Date].getTime / 1000)
    }
}
Foi útil?

Solução

lift-json can map from/to (case) classes) and if it doesn't already have a Formatter to go from epoch number to Date you could write your own formatter, have a look at the documentation here: https://www.assembla.com/wiki/show/liftweb/JSON_Support

and specifically at the sources for the formatters

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top