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.

Deserialize a list of generics inside a data class with kotlinx.serialization

See original GitHub issue

Say you have:

@Serializable
data class PagedData<T>(val data: T, val page: Int, val totalPages: Int, val pageSize: Int)

Where you know for sure that T is a class that has the annotations @Serializable or a list of a type that is annotated with @Serializable.

Why the serialization runtime is not able to infer what to use? It would be kind of pointless to explicitly build a serializer before every response as such.

Cross reference on kotlin.serialization issue board

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
e5lcommented, Apr 15, 2020

Hi, @lamba92. I reproduced the problem on the server, it will be fixed in the next minor release.

3reactions
monxalocommented, Oct 21, 2020

Don’t know if this is new or not but with a combination of Plugin generated generic serializer with Serializers module this is possible.

@Serializable
data class Item(val entry: String)

@Serializable
data class PagedData<T>(val data: T, val page: Int, val totalPages: Int, val pageSize: Int)

// in Application.module()

json(json = Json {
    serializersModule = SerializersModule {
        contextual(PagedData.serializer(Item.serializer()))
    }
})

With these changes on your response in ktor you no longer need to serialize the object in call.response() method to a String

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize a list of generics inside a data class #795 - GitHub
Where you know for sure that T is a class that has the annotations @Serializable or a list of a type that is...
Read more >
Deserialize a list of generics inside a data class with kotlinx ...
Deserialize a list of generics inside a data class with kotlinx.serialization ; Project, Ktor ; Type, Feature F ; Subsystem, Server ; Affected...
Read more >
Deserialize generic object using Kotlin Serialization
Save this question. Show activity on this post. I am trying to replace Gson library by kotlin serialization to handle JSON serialization/ ...
Read more >
Android Data Serialization Tutorial with the Kotlin Serialization ...
It's generic type parameter specifies the type of object serialized by the serializer. Open BoredActivity.kt in data. Below BoredActivity , add ...
Read more >
How do i serialize a generic sealed class with kotlinx ...
For the sake of simplicity in the example I only represent the exception message. @Serializable(with = ServiceResultSerializer::class) sealed class ...
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