You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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.
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
Expected behavior
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:
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
The text was updated successfully, but these errors were encountered: