Consider all Serializable class implements a common interface.
See original GitHub issueConsider 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:
- Created 5 years ago
- Reactions:20
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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):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:… And, suddenly, compile-time checking! 😃
Also, it could be used to further improve the signature of
KClass.serializer()
from:To:
And, suddenly, compile-time checking again.
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>
implementKSerializable<T>
? If so, what should itsserializer
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.