-
Notifications
You must be signed in to change notification settings - Fork 17
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
Ability to specify custom serializers in Services #89
Comments
Hi! Thank for the kind words and your question! Indeed, right now // todo update serializer to work with the value class
@Serializable(with = LocalDateSerializer::class)
@JvmInline
value class LocalDateValue(val value: LocalDate)
interface DatabaseAbsenceRPC : RPC {
suspend fun getAbsences(date: LocalDateValue): List<AbsenceRecord>
} We actually support interface DatabaseAbsenceRPC : RPC {
suspend fun getAbsences(@Contextual date: LocalDate): List<AbsenceRecord>
}
// and, for example, with kRPC protocol
rpcClientConfig {
serialization {
json { // for example, json
serializersModule = SerializersModule {
contextual(LocalDate::class) {
// do the thing
}
}
}
}
} But our current codegen does not copy the parameter annotation, with is a problem on our side. (So it actually works with custom classes as arguments with contextual properties inside them, but not with the method arguments themselves) |
Hi, we are currently implementing RPC and we'd appreciate having |
Hi! Probably with or right after K2 support in 0.2.1 |
Ok, so funny thing
data class Some(
val date: @Serializable(LocalDateSerializer::class) LocalDate,
)
fun main() {
Json.encodeToString(Some(LocalDate.now()))
} However, this does work data class Some(
val date: @Contextual LocalDate,
)
fun main() {
Json {
serializersModule = SerializersModule {
contextual(LocalDate::class) { LocalDateSerializer }
}
}.encodeToString(Some(LocalDate.now()))
} And this does work too! @Rpc
interface Service : RemoteService {
suspend fun nonSerializableClassContextual(localDate: @Contextual LocalDate): LocalDate
} And apparently it did all this time 😄 |
I was wrong about the first use case, actually, will check what is wrong |
Ok, just a heads up: @Rpc
interface Service : RemoteService {
// works
suspend fun func1(localDate: @Contextual LocalDate)
// works
suspend fun func1(localDate: @Contextual LocalDate): LocalDate
// works
suspend fun func1(localDate: @Serializable(with = ...) LocalDate)
// doesn't work
suspend fun func1(): @Serializable(with = ...) LocalDate
} Immediately, there is no intention to fix the last case, as the @contextual serializers cover it. Maybe later |
Hi! I'm currently trying to implement RPC for database access for an admin panel of my pet-project. Server-side database API utilizes Java's
LocalDate
andLocalDateTime
classes, which do not have default serializers in kotlinx.serialization, but I've written custom serializers for them by hand.I've tried to annotate all
LocalDate
fields with@Serializable()
annotation specifying serializer to use, but that didn't help and build still failed with messages like:DatabaseAbsenceRPCClient.kt:58:58 Serializer has not been found for type 'LocalDate'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
. Not including these annotations doesn't affect build's failure to build too. :)Declaration file:
So, my question is: would it be possible to somehow read these annotations in code-generation time and apply them to the generated code too? I think, I'm not the only one who uses custom serializers in kotlinx.serialization, so that would be a nice-to-have feature so we don't have to write boilerplate code to transform from-LocalDate-to-String and back :)
Awesome work on the library, btw! Looks promising!
The text was updated successfully, but these errors were encountered: