Skip to content
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

Gson is unable to serialise Kotlin subclass which overrides fields. #2453

Open
muditsen opened this issue Jul 27, 2023 · 2 comments
Open

Gson is unable to serialise Kotlin subclass which overrides fields. #2453

muditsen opened this issue Jul 27, 2023 · 2 comments
Labels
enhancement kotlin Issues concerning the use of Gson with Kotlin

Comments

@muditsen
Copy link

Problem solved by the feature

Gson will be able to easily serialise subclasses with overridden fields.

Feature description

Gson is unable to serialise Kotlin subclass which overrides fields gives error multiple JSON fields named. Gson should ignore the parents value when a value gets overridden. Use childs name and value when serializing.

Alternatives / workarounds

Use transient keyword either in parent or child class.

@eamonnmcmanus
Copy link
Member

Could you show some code where this is a problem? Doesn't have to be real code, of course: if you can boil it down to a simple example that would be even better.

I do think that if you have a choice, Moshi is probably a better choice than Gson when working with Kotlin.

@muditsen
Copy link
Author

[
open class Event(

@SerializedName("page_context")
open val pageContext: PageContext,

)](url)

[ open class CommonEvent(

@SerializedName("page_context") @Transient override val pageContext: PageContext,)

](url)

Please check the code. Here Event is a parent class and CommonEvent is a subclass. CommonEvent overrides a variable pageContext. When you serialize it gives an error without transient keyword.

@eamonnmcmanus eamonnmcmanus added the kotlin Issues concerning the use of Gson with Kotlin label Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement kotlin Issues concerning the use of Gson with Kotlin
Projects
None yet
Development

No branches or pull requests

2 participants