Type hints can be used to alter the default conversion rules when converting
Scala instances into JSON and vice versa. Type hints must be used when converting
class which is not supported by default (for instance when class is not a case class).
Example:
class DateTime(val time: Long)
val hints = new ShortTypeHints(classOf[DateTime] :: Nil) {
override def serialize: PartialFunction[Any, JObject] = {
case t: DateTime => JObject(JField("t", JInt(t.time)) :: Nil)
}
override def deserialize: PartialFunction[(String, JObject), Any] = {
case ("DateTime", JObject(JField("t", JInt(t)) :: Nil)) => new DateTime(t.longValue)
}
}
implicit val formats = DefaultFormats.withHints(hints)
Type hints can be used to alter the default conversion rules when converting Scala instances into JSON and vice versa. Type hints must be used when converting class which is not supported by default (for instance when class is not a case class).
Example: