0

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
lipeiran
  • 526
  • 13
  • 33
  • 1
    What library are you using? I thought Gson because of `@SerializedName`, but Gson shouldn't have serialized a property through a method by default, so I assume Jackson. Which one is it? – Sotirios Delimanolis Aug 30 '16 at 22:53
  • 1
    Java serialization and JSON serialization are two totally different things. – Andreas Aug 30 '16 at 23:06
  • I believe I am using Gson. Let's just assume I have the option of using Gson or Jackson. What would be the solution for each option? – lipeiran Aug 30 '16 at 23:11
  • 2
    No, let's not do that. Be specific. In any case, both have duplicates: [gson](http://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations), [jackson](http://stackoverflow.com/questions/14708386/want-to-hide-some-fields-of-an-object-that-are-being-mapped-to-json-by-jackson). – Sotirios Delimanolis Aug 30 '16 at 23:13
  • The Gson example is only valid for fields. I am using spring framework, so I am not sure what the underlying serializer is. If you can point me, that would be great – lipeiran Aug 30 '16 at 23:21
  • Ok, thanks for pointing out that it was Jackson, and not Gson. I just switched over to Spring and didnt realize it uses Jackson by default. I added @JsonIgnore and now the problem have been solved. – lipeiran Aug 30 '16 at 23:41
  • `@Until(Double.MIN_VALUE)` :) – Mike Samuel Aug 30 '16 at 23:51

1 Answers1

0

You can add to your public method the @JsonIgnore annotation and the field will be removed from your response.