I know how to serialize null objects. but what if I have an objects and inside another object that is null?
e.g.
My 1st class:
public class Invoice {
private Adresse adresse;
private Double betrag;
private Double Ust;
private String zweck;
}
My 2nd class:
public class Adresse {
private String name;
private Ort ort;
}
And more sub-classes…
And if I do:
Gson gson = new GsonBuilder().serializeNulls().create();
I only get this:
{"adresse":null,"betrag":null,"Ust":null,"zweck":null}
instead of this:
{"adresse":{"name":null,"ort"{"plz":null,"name":null}},"betrag":null,"Ust":null,"zweck":null}
There should be an automated approach to it. There will always be other sub-classes. So a manual approach is a no-go.
I got it working! With Java Reflection!
Here the code:
userDefined() checks if a class is user defined or primitive, can be customized as you wish.
getSetterMethod() returns the setter Method of a field.
And finally, invokeSetters() invoke user defined object setter Methods and those of the subclasses! yea.