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.

Consider all Serializable class implements a common interface.

See original GitHub issue

Consider all Serializable class implements a common interface to get the corresponding KSerializer for that class (and this should be done by the compiler plugin).

@Serializable
data class Test(
        val name: String
)

the generated code maybe like this

interface KSerializable<T> { // ignore the dummy name (because Serializable is already taken)
    val serializer: KSerializer<T>
}

@Serializable
data class Test(
        val name: String
) : KSerializable<Test> {
    override val serializer
        get() = Test.serializer()
}

For now, one can only serialize (and deserialize) an object like this:

        val kotlinMessage = buildKotlinMessage()
        val kotlinOut = ProtoBuf.plain.dump(Kotlin.Message.serializer(), kotlinMessage)

A serializer must be passed, so we must know the exactly class type.

By this, one can serialize an object easily:

        val kotlinMessage = buildKotlinMessage()
        val kotlinOut = ProtoBuf.plain.dump(kotlinMessage.serializer, kotlinMessage)
        // or if ProtoBuf takes advantage of this interface, we can also do this
        val kotlinOut = ProtoBuf.plain.dump(kotlinMessage)

Which looks much better!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:20
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
JoaaoVeronacommented, Jan 2, 2020

I came here just to ask for it, but for a different purpose: I want to create a type-safe API, that checks at compile-time, whether a class is serializable or not.

For example, say I have a generic function that serializes an object using the experimental KClass.serializer() function (to retrieve the class serializer using Reflection):

fun sendToClient(response: Any) {
    ...
    val json = Json.stringify(response::class.serializer(), response)
    ...
}

Consumers of this function will be able to call it with not-serializable instances, because it has the Any type.

However, if an interface (like, for example, KSerializable) gets added to every @Serializable-annotated class, then I could rewrite this function as:

fun sendToClient(response: KSerializable) {
    ...
    val json = Json.stringify(response::class.serializer(), response)
    ...
}

… And, suddenly, compile-time checking! 😃

Also, it could be used to further improve the signature of KClass.serializer() from:

fun <T : Any> KClass<T>.serializer(): KSerializer<T> = ...

To:

fun <T : KSerializable> KClass<T>.serializer(): KSerializer<T> = ...

And, suddenly, compile-time checking again.

2reactions
qwwdfsadcommented, Aug 24, 2020

All proposed use-cases are valid, but unfortunately, this feature cannot be implemented in a way that works all the time consistently. Classes with type parameters are one part of the problem. E.g. does List<T> implement KSerializable<T>? If so, what should its serializer function return? Another problem is Kotlin built-in types: primitives, collections, String etc. that obviously cannot implement an interface from the 3rd-party library. All interfaces are implicitly marked with @Polymorphic. Also, there are 3-rd party classes that are only externally serializable (@Serializer(forClass = ...)).

Taking this into account, it doesn’t seem possible to implement this with a reasonable effort. In the future, if Kotlin has typeclasses, it will be possible to solve this problem using them though.

Closing as won’t fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlinx serialisation, common interface or type class
I am working on a plugin type system where 3rd parties will register classes that will expose data. I don't know exactly what...
Read more >
Do not blindly implement the Serializable Interface
First, let's create a Person class and let's implement the Serializable interface. Next, let's quickly create a PersonSerializationUtil : ...
Read more >
NotSerializableException in Java with Examples
In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface.
Read more >
Commonly used Java -able Interfaces and Classes
The class implementing java.io.Serializable interface enables its objects to be serialized. This interface doesn't have any method. Such an ...
Read more >
Serialization in Java - DigitalOcean
If you want a class object to be serializable, all you need to do it implement the java.io.Serializable interface. Serializable in java is...
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