Currently I have an object create on my web server and then it will be returned to my client as a JSON object.
public class JsonClass{
@SerializedName("field1")
private myObject field1;
public myObject getField1() {
return field1;
}
public void setField1(myObject value) {
this.field1 = value;
}
@SerializedName("field2")
private myObject field2;
public myObject getField2() {
return field2;
}
public void setField2(myObject value) {
this.field2= value;
}
public final boolean isValid() throws InvalidObjectException {
if (field1 != null) field1 .isValid();
if (field2 != null) field2 .isValid();
return true;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + (field1 == null ? 0 : field1 .hashCode());
result = 31 * result + (field2 == null ? 0 : field2 .hashCode());
return result;
}
}
I expect the JSON object to be
{
"field1": {
"a": "123",
"b": "456",
"c": 789
},
"field2": {
"a": "123",
"b": "456",
"c": 789
}
}
However, because my .isValid() method is public, the JSON object in response all have an extra property "valid": true.
{
"field1": {
"a": "123",
"b": "456",
"c": 789,
"valid": true
},
"field2": {
"a": "123",
"b": "456",
"c": 789,
"valid": true
},
"valid": true
}
I can not change the method modifier to private or protected because classes in other packages need to call the validation method.
How can I eliminate the extra field "valid": true from my JSON object?
Edit: I am using Spring Framework for request handling and response handling. It uses Jackson behind the scenes.