How to handle complex transformations in circe decode

121 views Asked by At

Let's say that I have JSON

{"some": "123"}

And I want to create a custom decoder that would decode it to

case class Test(some: Long Refined NonNegative = 0)

So first I would need to decode JSON to String. Then turn String to Long which may throw an error which I can wrap in Try and use Decoder.decodeString.emapTry. But then I have to turn Long to Long Refined NonNegative which can be done via method refinedV[NonNegative](x: Long) which returns Either[String, Long Refined NonNegative]. And on top of all that, there is the default value I have to handle. Its gets very messy.

How can I manage such complex transformations during decoding, are there more complex structures than emap and emapTry?

0

There are 0 answers