I am trying to decode a string of the form "5m" or "5s" or "5ms" into objects of type FiniteDuration that are, respectively, 5.minutes, 5.seconds, 5.milliseconds.
I am trying to create a custom decoder and encoder for a project that involves the FiniteDuration class. The encoder is no problem, since it's just reading the fields of the FiniteDuration class and generating a string. However, I am having difficulty writing the decoder and am wondering if what I am doing is possible at all.
FiniteDuration is a class that has a constructor as follows: FiniteDuration(length: Long, unit: TimeUnit). Scala comes with some convenient syntactic sugar so that the class can be called using the notation 5.minutes, 5.seconds, or 5.milliseconds. In that case Scala takes care of the creation of the FiniteDuration class for you.
The idea is to convert this FiniteDuration class to a string like "5m" or "5s" or "5ms" which is easier on the eyes.
implicit val d2json: Encoder[FiniteDuration] = new Encoder[FiniteDuration] {
override def apply(a: FiniteDuration): Json = ???
}
implicit val json2d: Decoder[FiniteDuration] = new Decoder[FiniteDuration] {
override def apply(c: HCursor): Decoder.Result[FiniteDuration] = ???
}
The encoder I should have no problem writing. The decoder is more tricky. I am not sure what to do since the apply method expect an input of type HCursor.