Skip to content

Generic Serialization  #2013

Closed
Closed
@Bizilizi

Description

@Bizilizi

ktor Server, kotlinx serialization

This issue is a try to draw attention to fixes made with respect to 1879 and partially related to 1858

With the current approach, kotlinx serialization feature is not able to perform serialization of generic classes during the call.respond.

Suppose you want to formalize responses in a way like this:

@Serializable
data class APIResponse<T>(
        val result: T,
        val success: Boolean = true,
        val errorCode: Int?,

        @Serializable(with = DateTimeSerializer::class)
        val timestamp: DateTime

)

In that situation serializer of APIResponse would have an argument, in order to implicitly declare nested serializer:

val serializer = APIResponse.serializer(ContentReview.serializer())

And by calling call.respond you would receive Can't locate argument-less serializer for class APIResponse, since with the current fixes module.getContextual(value::class) returns null.
I guess this problem also can be generalized to the case of call.receive<Boxed<Int>>()

Please consider specifying serializer to use in call.respond and call.receive in some way other than module.getContextual(...). Unless you want to deal with all of the feature requests related to kotlinx serialization of course.

My suggestion is to add an option to call.respond/call.receive in order to specify the serializer for complex cases and use default serializer installed through install(ContentNegotiation) as a default one.

Activity

self-assigned this
on Aug 3, 2020
cy6erGn0m

cy6erGn0m commented on Aug 3, 2020

@cy6erGn0m
Contributor

Yes, this is the known limitation that will be eliminated in the future.

lamba92

lamba92 commented on Aug 7, 2020

@lamba92

Duplicate of #1783

rwsbillyang

rwsbillyang commented on Aug 28, 2020

@rwsbillyang

I am also plagued by this problem. I used the similar design in http response. I have to add 'contextual(MyDataModel.serializer()) ' into SerializersModule for every data model class after upgrade to koltin 1.4.0. It's cumbersome if there are lots of data model classes in module.

e5l

e5l commented on Apr 5, 2021

@e5l
Member

Duplicate

xht418

xht418 commented on Dec 3, 2021

@xht418

I got the similar problem today: https://stackoverflow.com/q/70207677/3466808, any one can help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @cy6erGn0m@e5l@lamba92@xht418@Bizilizi

      Issue actions

        Generic Serialization · Issue #2013 · ktorio/ktor