implicit object finiteDurationFormat extends RootJsonFormat[FiniteDuration] {
def write(fd: FiniteDuration) = JsObject(
"length" -> JsNumber(fd.length),
)
def read(value: JsValue) = {
value.asJsObject.getFields("length", "unit") match {
case Seq(JsNumber(length), JsString(unit)) =>
FiniteDuration(length.toLong, unit)
case _ => throw new DeserializationException("FiniteDuration expected")
}
}
}
implicit val statsFormat: RootJsonFormat[Stats] = jsonFormat8(Stats.apply)
Note the ordering of the implicits matters.