Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson date format issue : java bean to JSON string conversion

Tags:

java

json

jackson

I am converting a java bean to JSON string using Jackson 2.7.4 version. In doing so, I am facing date format issue. Java 1.7 version is being used.

Bean :

public class BaseBean {

    private java.util.Date fromDate;

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }
}

and I am getting below date format

{"fromDate":1465370289436}

Which is not required by me. then I configured below code

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS , false);
jsonInString = objectMapper.writeValueAsString(objJava);

Then I am getting below date format in JSON :

{"fromDate":"2016-06-08T07:47:06.636+0000"}

Expected date format :

{"fromDate":{"date":8,"day":3,"hours":12,"minutes":48,"month":5,"seconds":9,"time":1465370289436,"timezoneOffset":-330,"year":116}}

Is there any configuration to handle it and get expected date format in JSON string.

like image 723
Rahul Mittal Avatar asked Jun 08 '16 07:06

Rahul Mittal


1 Answers

JSON date Serializer :

public class CustomDateSerializer extends JsonSerializer<Date> {

@Override
public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException,
        JsonProcessingException {

    // below methods of Date object is deprecated - consider this as sample example 
    int idate = date.getDate();
    int day = date.getDay();
    int hours = date.getHours();
    int minutes = date.getMinutes();
    int month = date.getMonth();
    int seconds = date.getSeconds();
    long time = date.getTime();
    int timezoneOffset = date.getTimezoneOffset();
    int year = date.getYear();

    jgen.writeStartObject();

    jgen.writeNumberField("date", idate);
    jgen.writeNumberField("day", day);
    jgen.writeNumberField("hours", hours);
    jgen.writeNumberField("minutes", minutes);
    jgen.writeNumberField("month", month);
    jgen.writeNumberField("seconds", seconds);
    jgen.writeNumberField("time", time);
    jgen.writeNumberField("timezoneOffset", timezoneOffset);
    jgen.writeNumberField("year", year);

    jgen.writeEndObject();      
}

@Override
public Class<Date> handledType() {
    return Date.class;
}

}

Setting serializer in SimpleModule :

ObjectMapper objectMapper = new ObjectMapper();
    String jsonInString = "";
    try {
        SimpleModule myModule = new SimpleModule();

        myModule.addSerializer(Date.class, new CustomDateSerializer());
        objectMapper.registerModule(myModule);
        jsonInString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objJava);
    } catch (JsonProcessingException e1) {          
        e1.printStackTrace();
    }

Hope this will help !

like image 127
Santosh Avatar answered Oct 12 '22 23:10

Santosh