Greetings,
I want java Date object to be serialized using following format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" and read back from a string of the same format during deserialization
To achieve this I've written following custom serializer
What is working : serialization
What is not working : deserialization
import net.liftweb.json.JsonDSL._
import net.liftweb.json.JsonAST.{JObject, JField, JValue}
import net.liftweb.json._
import java.util.Date
import java.text.{ParseException, Format, SimpleDateFormat}
class DateSerializer extends Serializer[Date] {
private val DateClass = classOf[Date]
val dateTimeZoneFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") // example: 2014-06-11T13:13:57.884+0530
def deserialize(implicit format: Formats) : PartialFunction[(TypeInfo, JValue), Date] = {
case (TypeInfo(DateClass, _), json) => json match {
case JString(dateString) => {
try{
println("reached till parse") //this println is never called
dateTimeZoneFormat.parse(dateString)
}
catch {
case e: ParseException => throw new MappingException("Can't convert " + dateString + " to Date")
}
}
case x => throw new MappingException("Can't convert " + x + " to Date")
}
}
def serialize(implicit format: Formats) : PartialFunction[Any, JValue] = {
case x: Date => JString(dateTimeZoneFormat.format(x))
}
}
import net.liftweb.json._
import org.apache.commons.lang3.StringUtils
import net.liftweb.json.Extraction._
import scala.reflect.Manifest
object JsonUtil {
implicit val formats = DefaultFormats + new DateSerializer
def toJSONString(obj: Any): String = {
compact(JsonAST.render(decompose(obj)))
}
def fromJSONString[A: Manifest](jsonString: String): Option[A] = {
StringUtils.isBlank(jsonString) match {
case true => None
case false => Some(parse(jsonString).extract[A])
}
}
}
Model:
case class SomeDate(date : Date)
Serializing: (this works fine)
println(JsonUtil.toJSONString(SomeDate(new Date)))
output: {"date":"2014-06-11T13:13:57.884+0530"} --> serialization works as expected
Deserialization:
println(JsonUtil.fromJSONString[SomeDate]("""{"date":"2014-06-11T13:13:57.884+0530"}""")) --> same date string that was serialized in the previous call
I get following exception:
net.liftweb.json.MappingException: No usable value for date
Invalid date '2014-06-11T13:18:29.379+0530'
at net.liftweb.json.Meta$.fail(Meta.scala:190)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:356)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:316)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:252)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:252)
Deserialization with another date format: //date json string confirms to following format: "yyyy-MM-dd'T'HH:mm:ss'Z'"
println(JsonUtil.fromJSONString[SomeDate]("""{"date":"2014-06-10T22:56:22Z"}"""))
output: Some(SomeDate(Wed Jun 11 04:26:22 GMT+05:30 2014))
Deserialization works if date json confirms to following format: "yyyy-MM-dd'T'HH:mm:ss'Z'" but I've specified a different format in my deserializer: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
The deserializer I've written doesnt even seem that its getting called.
What wrong am i doing? How this can issue be resolved?
Thanks!