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

Implement additional extension serializers for frequently utilized data types in web requests. #2786

Closed
jamhour1g opened this issue Aug 17, 2024 · 1 comment
Labels
duplicate feature Static modules Request for serializer that can be added to Java module

Comments

@jamhour1g
Copy link
Contributor

jamhour1g commented Aug 17, 2024

What is your use case and why do you need this feature?

While developing a bot to extract job data from websites, I needed to handle date and time information in the requests, along with links to the job listings. I expected these common data types to have built-in serializers, akin to those for String or Int, but they were absent. Consequently, having to create serializers for these types in each project becomes a repetitive and burdensome task.

Describe the solution you'd like

  1. Add extension serializers to java.time interfaces and classes
  2. Add extension serializers to java.net.URI and java.net.URL

It's worth noting that certain java.time classes may require contextual serializers. Perhaps a ContextualSerializers factory class could be offered for scenarios where serialization remains consistent, but the input formatting varies, such as with java.time.ZonedDateTime.

 class ZonedDateTimeSerializer(
    val format: DateTimeFormatter
) : KSerializer<ZonedDateTime> {
    override val descriptor = PrimitiveSerialDescriptor(ZonedDateTime::javaClass.name, PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: ZonedDateTime) = encoder.encodeString(value.toString())
    override fun deserialize(decoder: Decoder) = ZonedDateTime.parse(decoder.decodeString(), format)
}

In the future, it may be beneficial to consider adding extensions to StringFormat that facilitate its conversion to HttpResponse.BodyHandler, as illustrated in this example.

inline fun <reified T> StringFormat.toBodyHandler() = HttpResponse.BodyHandler {
    HttpResponse.BodySubscribers.mapping(
        HttpResponse.BodySubscribers.ofString(Charsets.UTF_8)
    ) {
        runCatching { decodeFromString<T>(it) }.getOrNull()
    }
}
@sandwwraith
Copy link
Member

I think it was being discussed in #1931, see also #1806 and #507. In short, this is something that is reasonable to have, but we can't make any serializer for time.* default (because different applications need different purposes), and we currently do not have any mechanisms to globally declare serializers per application module. So the only option we do have for now is a separate library with just serializers that can be used with existing @file:UseSerializers annotations.

@sandwwraith sandwwraith added duplicate Static modules Request for serializer that can be added to Java module labels Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate feature Static modules Request for serializer that can be added to Java module
Projects
None yet
Development

No branches or pull requests

2 participants