question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generic Serialization

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
cy6erGn0mcommented, Aug 3, 2020

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

0reactions
xht418commented, Dec 3, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generics serialization - Unity Forum
Scripting: The serializer can now serialize fields of generic types (e.g. MyClass someField) directly; it is no longer necessary to derive a ...
Read more >
How to make a Generic Type Serializable? - Stack Overflow
The class is used in a REST interface, so it should be Serializable. My Questions: 1. I don't think marking the type as...
Read more >
How to serialize and de-serialize generic types using the ...
If a Java class is a generic type and we are using it with the Gson library for JSON serialization and deserialization.
Read more >
Serialization Implementation Method Based on Generic ...
This article explores the concepts and benefits of generic programming and serialization through a simple example.
Read more >
Define schema and use generic serialization for transactions
This proposal defines how the generic serialization algorithm will be applied to transactions and specify the appropriate JSON schemas.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found