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 cannot serialize subclass who override a field #2638

Open
SannaK opened this issue Feb 27, 2024 · 1 comment
Open

Gson cannot serialize subclass who override a field #2638

SannaK opened this issue Feb 27, 2024 · 1 comment
Labels
bug kotlin Issues concerning the use of Gson with Kotlin

Comments

@SannaK
Copy link

SannaK commented Feb 27, 2024

I saw some others threads about it but I did not find a solution in them.

Gson version

2.10.1

Java / Android version

17

Description

The real context

I have a sealed class (heritance from Throwable) who represent all errors in my app (~80 inner dataclass). Sometime I want to override "message" or "reason" in my "sub dataclass", but when i try to serialize it with, an error occurs.

A simple way to reproduce

    open class MyClass {
        open val a = 22
    }
    open class SubClass : MyClass() {
        override val a = 25
        val b = 12
    }

Expected behavior

  • When I serialize MyClass, i expect to have { "a" : 22 }
  • When I serialize SubClass, i expect to have { "a": 25, "b" : 12 }

Actual behavior

It cannot serialize SubClass, "JSON fields named 'a'; conflict is caused by fields ...".
Also i tried handmade reflection on "SubClass" and i see "a" only one time. I guess you are using more complex mecanics.

Notes:

  • I can't change the class Throwable (or use annotation in them)
  • A can't make an adapter for all the 80 inner data class
  • I tried with Jackson json serialization and it works as expected
val mapper = ObjectMapper()
val json = mapper.writeValueAsString(SubClass())
println(json)

Output : {"a":25,"b":12}

Reproduction steps

Just run an unit test with the code below and call the methode Builder().toJson(SubClass())

Exception stack trace

Class com.organisation.app.MyUnitTest$SubClass declares multiple JSON fields named 'a'; conflict is caused by fields com.organisation.app.MyUnitTest$SubClass#a and com.organisation.app.MyUnitTest$MyClass#a
java.lang.IllegalArgumentException: Class com.organisation.app.MyUnitTest$SubClass declares multiple JSON fields named 'a'; conflict is caused by fields com.organisation.app.MyUnitTest$SubClass#a and com.organisation.app.MyUnitTest$MyClass#a
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:302)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:130)
	at com.google.gson.Gson.getAdapter(Gson.java:556)
	at com.google.gson.Gson.toJson(Gson.java:834)
	at com.google.gson.Gson.toJson(Gson.java:812)
	at com.google.gson.Gson.toJson(Gson.java:759)
@SannaK SannaK added the bug label Feb 27, 2024
@SannaK SannaK changed the title Gson cannot serialize subclass who override property Gson cannot serialize subclass who override a field Feb 27, 2024
@Marcono1234
Copy link
Collaborator

Also i tried handmade reflection on "SubClass" and i see "a" only one time

Are you using Java reflection or Kotlin reflection? Possibly Kotlin reflection is hiding the fact that there are multiple fields with the same name.

This might be the same as #2453. Currently Gson is mainly targeting Java, it might also work fine for many cases with Kotlin but there is no proper support for it at the moment.

If possible it might be better if you used a library with explicit Kotlin support, such as Jackson (which you already tried), https://github.com/square/moshi or https://github.com/Kotlin/kotlinx.serialization.

@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
bug kotlin Issues concerning the use of Gson with Kotlin
Projects
None yet
Development

No branches or pull requests

3 participants