We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
gson-extras has such class: https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java which is helpful for polymorphic types serialization without reflection.
gson-extras
For example, it can be used to handle Kotlin sealed class serialization:
sealed class Shape { class Rectangle() : Shape() class Circle() : Shape() }
val adapter: RuntimeTypeAdapterFactory<Shape> = RuntimeTypeAdapterFactory.of(Shape::class.java) .registerSubtype(Rectangle::class.java) .registerSubtype(Circle::class.java)
And then in your gson instance builder:
registerTypeAdapterFactory(adapter)
However, seems it is a boilerplate and not safe solution to write such factory for every sealed class in the project. Would be nice to handle it with annotations like kotlinx-serialization does with polymorphic serialization problems: https://medium.com/@andreclassen1337/goodbye-runtimetypeadapterfactory-polymorphic-serialization-using-kotlinx-serialization-46a8cec36fdc
kotlinx-serialization
@Polymorphic sealed class Shape { @SerialName("Rectangle") class Rectangle() : Shape() @SerialName("Circle") class Circle() : Shape() }
The text was updated successfully, but these errors were encountered:
@GediminasZukas that's a great idea! I'll have a look and keep you updated
Sorry, something went wrong.
I've made a tiny library which tries to solve this problem: https://github.com/GediminasZukas/PolyAssistant Feel free to use any idea from it if you find it helpful.
No branches or pull requests
gson-extras
has such class: https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java which is helpful for polymorphic types serialization without reflection.For example, it can be used to handle Kotlin sealed class serialization:
And then in your gson instance builder:
registerTypeAdapterFactory(adapter)
However, seems it is a boilerplate and not safe solution to write such factory for every sealed class in the project. Would be nice to handle it with annotations like
kotlinx-serialization
does with polymorphic serialization problems: https://medium.com/@andreclassen1337/goodbye-runtimetypeadapterfactory-polymorphic-serialization-using-kotlinx-serialization-46a8cec36fdcThe text was updated successfully, but these errors were encountered: