-
Notifications
You must be signed in to change notification settings - Fork 641
@Serializable for Java classes/source code #1687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Auto-generating serializers for Java classes is not supported at the moment, indeed. One of the main reasons for that is serialization plugin heavily relies on the concept of primary constructor — it is the constructor that should definitely be called to recreate an instance of the class, so we collect properties from it to serialize the class. Java has no primary constructor and no constructor properties, so it's impossible to know which constructor with which arguments to call upon deserialization. Unless Java class has an empty constructor, and every property is represented with getter/setter, it is technically impossible to auto-generate KSerializer for Java class. We concluded that supporting Java classes with empty constructors is a relatively minor case (because there was a little demand for it), so we hadn't made an exception for it. To successfully use kotlinx.serialization with Java classes, one needs to implement |
Thank you for the anwer. I understand the problem with finding a primary constructor for Java classes. Implementing a serializer manually for each class is most likely a too large maintanance burden, and too error-prone (I did that as an experiment for a single class in https://github.com/AxonFramework/extension-kotlin/pull/124/files#diff-cc9e7866a7929cd55740daaa367352b89517dc56787fa6eb48f5c2fcb783b1bb). Using a surrogate serializer for a Kotlin class that wraps a Java class is an option that I will try. I will close this issue because you indicated that supporting Java classes by using a default constructor with getters/setters for each (public) field is a too small use case. I understand that. Feel free to re-open this issue if there is a greater need for that feature. I do think that supporting Java classes with Kotlin Serialization annotations will make the migration from one form of serialization to Kotlin serialization for Java-based probjects much easier. The performance benefits of using a serializer that does not use reflection is also useful for Java based projects. |
What is your use-case and why do you need this feature?
Reference to AxonFramework/extension-kotlin#124 (comment)
For Axon Framework I am investigating whether adding Kotlin Serialization is possible as a framework serializer.
The framework is developed with Java, and Kotlin is not a first-class language. However, the framework is very extensible and adding custom serialization implementations is straightforward.
During the investigation I was asked how the
@Serializable
annotation and the auto-generated Serializer static/companion methods could be added to the existing classes that need serialization support.I tried (see the linked comment above):
I saw:
serializer()
static/companion method is generated.This makes adding Kotlin serialization to existing Java projects without Kotlin dependencies hard/impossible.
Describe the solution you'd like
For a (non-Kotlin) Java project, it should be possible to add the
@Serializable
annotation to the Java source code with a compile-only dependency, and let the Gradle/Maven compiler plugin generate theserializer()
static/companion methods.Another (separate) project (with Kotlin as a depedency) can then implement a custom serialization implementation, which can serialize all the annotated
@Serializable
classes automatically.The text was updated successfully, but these errors were encountered: