Announcement Announcement Module
Collapse
No announcement yet.
Date conversion when serializing from domain to JSON Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date conversion when serializing from domain to JSON

    Given the following domain object:

    Code:
    import java.util.Date;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.validation.constraints.NotNull;
    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.roo.addon.javabean.RooJavaBean;
    import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
    import org.springframework.roo.addon.json.RooJson;
    import org.springframework.roo.addon.tostring.RooToString;
    
    @RooJavaBean
    @RooToString
    @RooJpaActiveRecord
    @RooJson
    public class Person {
    
        @NotNull
        private String name;
    
        @NotNull
        private Integer age;
    
        @NotNull
        private Integer size;
    
        @Temporal(TemporalType.TIMESTAMP)
        @DateTimeFormat(style = "M-")
        private Date birthDate;
    }
    when calling the ROO generated toJsonArray() method I get the following JSON String

    Code:
    [{"age":39,"birthDate":75337200000,"id":1,"name":"tw-hra-3","size":188,"version":5},{"age":23,"birthDate":1168902000000,"id":5,"name":"tw-she","size":165,"version":0},{"age":38,"birthDate":1325458800000,"id":6,"name":"tw-mno","size":188,"version":0},{"age":31,"birthDate":311986800000,"id":7,"name":"tw-wli","size":178,"version":0},{"age":32,"birthDate":665794800000,"id":8,"name":"tw-ksc","size":178,"version":0}]
    Any idea how to get formatted localized dates here?

    Thank you in advance
    Fireball
    Last edited by Fireball; Jan 20th, 2012, 09:18 AM.

  • #2
    I use JSON serialization annotations around my getter/setter methods.

    @JsonSerialize(using = JsonCalendarSerializer.class)
    public Calendar getExpirationDate() {
    return expirationDate;
    }

    @JsonSerialize(using = JsonCalendarSerializer.class)
    public void setExpirationDate(Calendar expirationDate) {
    this.expirationDate = expirationDate;
    }

    Then define the serializer (found elsewhere on web):

    /**
    * Used to serialize Java.util.Date, which is not a common <a target="_blank" title="JSON" href="http://www.json.org/">JSON</a>
    * type, so we have to create a custom serialize method;.
    *
    * @author Loiane Groner
    * http://loianegroner.com (English)
    * http://loiane.com (Portuguese)
    */
    public class JsonCalendarSerializer extends JsonSerializer<Calendar> {

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    @Override
    public void serialize(Calendar date, JsonGenerator gen, SerializerProvider provider)
    throws IOException, JsonProcessingException {
    SimpleDateFormat formatter = new SimpleDateFormat(JsonCalendarSerializer.DATE_FORMA T);
    String formattedDate = formatter.format(date.getTime());
    gen.writeString(formattedDate);
    }
    }

    Comment

    Working...
    X