It would be so great to have some way of global di...
# serialization
p
It would be so great to have some way of global discovery. I now have about 70 occurences in my project of `
Copy code
@Serializable(with = LocalDateSerializer::class)
👍 1
c
I tried to fix that with this, with ktor:
Copy code
install(ContentNegotiation) {
        json(module = serializersModuleOf(LocalDate::class, LocalDateSerializer))
    }
coulddn't get it to work though.
b
Would be great indeed, though I can imagine it's a complex subject to get exactly right (if there even is a definition of right that everybody can agree on). However, are you aware of https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#useserializers-annotation ? This might alleviate some of your pain. I suppose it would help if Kotlin could also support annotations at the package or directory level.
p
The file annotation doesn't work with library classes and still reports an error. At runtime it works but the ide is filled with errors. And registering such a module would still need me to suppress the warnings
b
The linked paragraph specifically uses the library class
java.util.Date
as example, is it not working as intended? What kind of error do you get?
p
That's no library class
b
Then what do you mean by library class?
p
I.e. threeten LocalDate
It doesn't work if the serializer is in another Gradle module
b
But what is the error you get?
p
"Serializer has not been found for type LocalDate..."
message has been deleted
b
This compiles fine for me:
Copy code
@file:UseSerializers(HostAndPortSerializer::class)
package x.y.z
data class Endpoint(val hp: HostAndPort)
HostAndPort
being from Google Guava, and
HostAndPortSerializer
defined in another module. Using Kotlin 1.3.31.
No IDEA warnings either, in 2020.1.1
Oh wait I forgot the Serializable Still works
p
It compiles fine for me too, ist just full of ide warnings
And your serializer is for sure in another module?
b
Yes, but it's not an Android project. I don't know too much about Android, but I believe the Gradle setup can get quite complex? Perhaps the IDE cannot handle it
In any case, that's something for the serialization developers. I'm just another user. As far as I can see, you're using it correctly
p
Got it reproducible
I'll report tomorrow with some additional ideas
And I opened a feature request on suppressing warnings for specific classes through a compiler flag: https://github.com/Kotlin/kotlinx.serialization/issues/831