Serializing
public class Subclass extends Superclass {
private static final long serialVersionUID = 1L;
private int someProperty;
public Subclass() {
}
public Subclass(int someProperty, String myProperty) {
super(myProperty);
this.someProperty = someProperty;
}
public int getSomeProperty() {
return someProperty;
}
public void setSomeProperty(int someProperty) {
this.someProperty = someProperty;
}
}
with
public class Superclass implements Serializable {
private static final long serialVersionUID = 1L;
private String myProperty;
public Superclass() {
}
public Superclass(String myProperty) {
this.myProperty = myProperty;
}
public String getMyProperty() {
return myProperty;
}
public void setMyProperty(String myProperty) {
this.myProperty = myProperty;
}
}
shouldn't fail because of
Exception in thread "main" java.lang.IllegalArgumentException: class richtercloud.gson.exclusion.strategy.Subclass declares multiple JSON fields named serialVersionUID
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.Gson.toJson(Gson.java:661)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
at richtercloud.gson.exclusion.strategy.Main.main(Main.java:41)
if a serialization exclusion strategy is used as follows:
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.addSerializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
boolean retValue = f.getName().equals("serialVersionUID");
return retValue;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
Subclass a = new Subclass();
String response = gson.toJson(a);
System.out.println(response);
I asked at SO in order to make sure that I'm not missing something. Some issues concerned with similar issues, like #399, #920 and #464 suggest that it's necessary to write a type adapter, but it's not clear why from the documentation, i.e. needs to be fixed if this functioning is intended.
A SSCCE is provided at https://gitlab.com/krichter/gson-exclusion-strategy.
experienced with 2.8.2.
Serializing
with
shouldn't fail because of
if a serialization exclusion strategy is used as follows:
I asked at SO in order to make sure that I'm not missing something. Some issues concerned with similar issues, like #399, #920 and #464 suggest that it's necessary to write a type adapter, but it's not clear why from the documentation, i.e. needs to be fixed if this functioning is intended.
A SSCCE is provided at https://gitlab.com/krichter/gson-exclusion-strategy.
experienced with 2.8.2.