Hi,

I tried using @JsonInclude(Include.NON_DEFAULT) on a Date but it doesn't 
work.

https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonInclude.Include.html#NON_DEFAULT

When NOT used for a POJO (that is, as a global default, or as property 
> override), definition is such that:
> All values considered "empty" (as per NON_EMPTY) are excluded
> Primitive/wrapper default values are excluded
> Date/time values that have timestamp (`long` value of milliseconds since 
> epoch, see Date) of `0L` are excluded


It still serializes the Date as 0 instead of excluding it. I have included 
my code below.

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;

public class NewClass {

    public static void main(String[] args) throws Exception {
        
        ObjectMapper objectMapper = new ObjectMapper();
        
        DTO dto = new DTO();
        System.out.println(objectMapper.writeValueAsString(dto));
        // prints out {"date":0} when it should print out {}
        
    }

}

class DTO {

    @JsonInclude(Include.NON_DEFAULT)
    private Date date = new Date(0);

    public DTO() {
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

}


-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to