Currently i working on serialising an java object to JSON using Gson. I am trying to skip a field in a class but not on the other class.
Sample code:
Class A {
var a;
var b;
}
Class B {
var a;
var c;
}
Class C {
class A;
class B;
}
I want to exclude a only on A, but not on B. I can't place annotations on the fields as the object is not owned by me or access object A and remove a manually. I am looking for some generic approach.
Similar question : Gson: How to exclude specific fields from Serialization without annotations which answer provided by Nishant matches the thought of mine. But I feel that the solution is not fully correct as the below condition provided there is wrong.
f.getDeclaringClass() == c
GetDeclaringClass is returning the type of the class not the parent class.
I wrote similar custom exclusion strategies using Gson. With exclusion strategy, I can do the following
shouldSkipClass(class)
shouldSkipField(fieldAttributes).
But I am expecting the combination of both skip if this fieldAttributes is present only in the provided class.
shouldSkipFieldUnderClass(class, fieldAttributes)
Are there any other ways to achieve the same? or am i doing anything wrong above? Please provide me suggestions to skip fields in specific class using Gson.