1

I am stuck with a problem which I have been trying to solve since last two days. I have an Integer object and a Float object which I do not want to display in the JSON response if it is 0. I am trying to achieve this with @JsonInclude(value=Include.NON_NULL) but it doesn’t seem to be working.

Does anyone have any suggestion and can explain me what I am doing wrong here?

Lets say the model class is something like this:

@JsonInclude(value = Include.NON_NULL)
public class myClassInfo {

    String                originalQuery;
    String                normalizedQuery;
    Long                  id;
    Integer               performanceStatus;
    Float                 atcPercentage;
    Integer               ruleOn;
    Integer               ruleOff;
}

I have the getter and setter methods accordingly. I want to display the atcPercentage, ruleOn and ruleOff only if it is not 0. How would I do that? I hope this explanation helps in understanding my problem. I have tried NON_NULL and it doesn't seems to be working. My understanding if I define the JsonInclude in the beginning of the class, that should be applicable to all the fields. Correct me if I am wrong.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • null and 0 are completely different things. – JB Nizet May 05 '19 at 17:00
  • https://fasterxml.github.io/jackson-annotations/javadoc/2.9/com/fasterxml/jackson/annotation/JsonInclude.Include.html#NON_DEFAULT – JB Nizet May 05 '19 at 17:02
  • Does `NON_DEFAULT` work for you? – Michał Ziober May 05 '19 at 19:25
  • Possible duplicate of [How to tell Jackson to ignore a field during serialization if its value is null?](https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null) – Michał Ziober May 05 '19 at 19:27
  • @MichałZiober: NON_DEFAULT doesn't work either. Thanks so much for commenting on my question. – Amith Rampur May 05 '19 at 20:28
  • @JBNizet Yes correct. but here I am using as Integer and Float objects. Correct me if i missed anything. Thanks so much for your valuable comments. Also please let me know if I need to define the variable in some other data types so that I check for not nulls instead of Integer and Floats. atcPercentage calculates the percentage which has a decimal point in it. Thanks so much in advance. – Amith Rampur May 05 '19 at 20:29
  • You need to use JsonInclude with NON_DEFAULT on the properties, not on the class. – JB Nizet May 05 '19 at 21:23

1 Answers1

0

You can write your own filter and use it as below:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        ClassInfo classInfo = new ClassInfo();
        classInfo.setId(0L);
        classInfo.setAtcPercentage(0F);
        classInfo.setPerformanceStatus(0);

        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(classInfo));
    }
}

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = ZeroNumberFilter.class)
class ClassInfo {

    private Long id;
    private Integer performanceStatus;
    private Float atcPercentage;

    // getters, setters
}

class ZeroNumberFilter {

    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof Number) {
            final Number number = (Number) obj;
            return Double.compare(number.doubleValue(), 0) == 0;
        }

        return false;
    }
}

prints {} - empty object. When we change all values to 1, it prints:

{"id":1,"performanceStatus":1,"atcPercentage":1.0}

Include.NON_NULL filters only properties with null value. You could use Include.NON_DEFAULT but in this case you should change your POJO and declare default values for all fields:

@JsonInclude(value = JsonInclude.Include.NON_DEFAULT)
class ClassInfo {

    private Long id = 0L;
    private Integer performanceStatus = 0;
    private Float atcPercentage = 0F;

    // getters, setters
}

But this solution could have some drawbacks if your business logic depends on null values somewhere.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Ziober Thanks so much for your help. Just one last question. If I do not want to see the Json Key altogether for Float and Integer variables rather that printing out the empty object should I be defining my Float atcPercentage = 0F in my class model? – Amith Rampur May 05 '19 at 22:15
  • @AmithRampur, empty object - `{}` is perfectly fine. It depends also what you need. I can not answer on this question without extra info and clarification. In case my answer was helpful, please, take a look at [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Michał Ziober May 05 '19 at 22:39
  • Ziober Thanks so much for all your valuable comments. – Amith Rampur May 06 '19 at 04:21