Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide codec for standard class Duration #1212

Open
DavidPerezIngeniero opened this issue Oct 14, 2024 · 2 comments
Open

Provide codec for standard class Duration #1212

DavidPerezIngeniero opened this issue Oct 14, 2024 · 2 comments

Comments

@DavidPerezIngeniero
Copy link

DavidPerezIngeniero commented Oct 14, 2024

When trying:

case class ConfiguracionCola(
                              nombre: String,
                              maxWorkers: Int,
                              jobTimeout: Duration,
                              observaciones: String,
                              prioritaria: Boolean,
                              grupo: String,
                              httpTimeout: Duration,
                              httpTtl: Duration,
                              httpMaxReintentos: Int
                            )

object ConfiguracionCola:
   given JsonValueCodec[ConfiguracionCola:] = make

I receive this strange error message:

Local child symbols are not supported, please consider change 'scala.concurrent.duration.Duration.Infinite' or 
implement a custom implicitly accessible codec

For me, the error message is not explanatory enough.
Should I write a custom serializer for Durations?

@plokhotnyuk
Copy link
Owner

@DavidPerezIngeniero Yes, 'scala.concurrent.duration.Duration is not supported yet for automatic derivation of codecs.

So, if using of java.time.Duration is not an option, then need to write a custom codec.

@DavidPerezIngeniero
Copy link
Author

DavidPerezIngeniero commented Oct 14, 2024

Thanks!

Possible implementation:

  given JsonValueCodec[Duration] with
    override def decodeValue(in: JsonReader, default: Duration): Duration =
      val l = in.readLong()
      if (l == -1) Duration.Inf else Duration.fromNanos(l)

    override def encodeValue(x: Duration, out: JsonWriter): Unit =
      out.writeVal(if (x == Duration.Inf) -1 else x.toNanos)

    override def nullValue: Duration = Duration.Inf

@DavidPerezIngeniero DavidPerezIngeniero changed the title Duration codec Provide codec for standar class Duration Oct 14, 2024
@DavidPerezIngeniero DavidPerezIngeniero changed the title Provide codec for standar class Duration Provide codec for standard class Duration Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants