4

Is it possible to serialize a JSON response while excluding some elements based on If conditions?

if(a == 1) {
   //show element
} else {
   //don't show element
}

I've tried using @JSONIgnore, but that simply ignores the element regardless of conditions. I'm new to this space. Any ideas?

EDIT: I'm working on enterprise software, so using 3rd party libraries and such won't be a possibility.

Brodiman
  • 149
  • 2
  • 9

3 Answers3

3

You could use custom Jackson serializer.

public class ConditionalValueSerializer extends StdSerializer<Integer> {
    public ConditionalValueSerializer() {
        this(null);
    }

    public ConditionalValueSerializer(Class<Integer> t) {
        super(t);
    }

    @Override
    public void serialize(Integer a, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if(a == 5 ){
            gen.writeString(a.toString());
        } else {
            gen.writeString("");
        }

    }
}

Then use the custom serializer in the object.

public class SomeThing {
    public String name;

    @JsonSerialize(using = ConditionalValueSerializer.class)
    public Integer value;
}
wpnpeiris
  • 766
  • 4
  • 14
  • I've added the class to my DTOs and used the annotation as you suggested. However, the element is still serialized even null. However, there are some differences. My compiler would not let me use StdSerializer. I had to use JsonSerializer. Would that be an issue? – Brodiman Feb 27 '20 at 14:25
  • 1
    `StdSerializer` is a `JsonSerializer`. That is, `StdSerializer` extends `JsonSerializer`. It should even work with `JsonSerializer`. May be you are using different `jackson` version. Try it with a different version, latest if possible. – wpnpeiris Feb 27 '20 at 16:00
3

I know your question is about @JsonIgnore, but you may want to try @JsonInclude:

@JsonInclude(value = JsonInclude.Include.CUSTOM, 
             valueFilter = CustomValueFilter.class)
private Integer value;
public class CustomValueFilter {

    @Override
    public boolean equals(Object other) {

        Integer a = (Integer) other;
        return a == 1;
    }
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • 2
    I was not able to get this work the way you described. However, it did lead me down the path to figure out how to get it to work for boolean statements. @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) – Brodiman Feb 27 '20 at 16:07
  • 1
    I'm going to mark this as the answer, as for my particular issue, this solved it. However, sithroo's answer is also a valid answer, but did not work with my current environment. – Brodiman Mar 01 '20 at 20:04
0

The most straightforward solution I've found for my needs involves using

    @JsonInclude(JsonInclude.Include.NON_NULL)

Basically if you can conditionally set a value to null, you can then exclude it entirely from serialization using the above annotation. Here's a quick example.

@JsonInclude(JsonInclude.Include.NON_NULL)
public String getValueToConditionallyExclude() {
    if (conditionWhereIWantToExcludethisField) {
        return null;
    }
    return theRealValueWhichWillNeverOtherwiseBeNull;
}
Adam Wise
  • 2,043
  • 20
  • 17