GsonBuilder序列化程序搞乱了Timestamp和Date

时间:2018-06-06 06:12:44

标签: java gson

我已经使用Gson将模型类序列化和反序列化为json字符串,但问题是gson将日期变量引用到时间戳序列化器。我的类包含Date和Timestamp变量,但两者都由同一个Timestamp序列化器序列化。我写过我的GsonBuilder.Below中的Timestamp和日期序列化器都是我的类

StaffDetails.java:

update tb
  set cls = 'great2'
where idx = 1;

以下是我的gson序列化程序

        import javax.persistence.*;
    import java.io.Serializable;
    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.Locale;

    @Entity
    @Table(name = "staff_details")
    public class StaffDetails implements Serializable {

        @Id
        @GeneratedValue
        @Column(name = "id")
        private Long id;

        @Column(name="address")
        private String address;

        @Column(name = "name")
        private String name;

        @Column(name = "age")
        private Integer age;

        @Column(name = "dob")
        private Date dob;

        @Column(name="created_by")
        private Long createdBy;

        @Column(name="created_on")
        private Timestamp createdOn;

        public StaffDetails() {

        }

        public StaffDetails(Long id, String address, String name, Integer age, Date dob, Long createdBy, Timestamp createdOn) {
            this.id = id;
            this.address = address;
            this.name = name;
            this.age = age;
            this.dob = dob;
            this.createdBy = createdBy;
            this.createdOn = createdOn;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Date getDob() {
            return dob;
        }

        public void setDob(Date dob) {
            this.dob = dob;
        }

        public Long getCreatedBy() {
            return createdBy;
        }

        public void setCreatedBy(Long createdBy) {
            this.createdBy = createdBy;
        }

        public Timestamp getCreatedOn() {
            return createdOn;
        }

        public void setCreatedOn(Timestamp createdOn) {
            this.createdOn = createdOn;
        }
    }

问题是dob和createdBy都要使用TimeStamp序列化程序。 我想要的是dob应该去Date serializer和createdBy到TimeStamp序列化器。请帮助我。

1 个答案:

答案 0 :(得分:0)

请使用单独的Serialize / de-Serialize类。

public class LocalDateDeserializer extends StdDeserializer<LocalDate> {

    private static final long serialVersionUID = 1L;

    protected LocalDateDeserializer() {
        super( LocalDate.class );
    }

    @Override
    public LocalDate deserialize( JsonParser jp, DeserializationContext ctxt ) {
        try {
            return LocalDate.parse( jp.readValueAs( String.class ) );
        }
        catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }
}


public class LocalDateSerializer extends StdSerializer<LocalDate>
{

    private static final long serialVersionUID = 1L;

    public LocalDateSerializer()
    {
        super( LocalDate.class );
    }

    @Override
    public void serialize( LocalDate value, JsonGenerator gen, SerializerProvider sp ) throws IOException, JsonProcessingException
    {
        gen.writeString( value.format( DateTimeFormatter.ISO_LOCAL_DATE ) );
    }
}

public class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

    private static final long serialVersionUID = 1L;

    protected LocalDateTimeDeserializer() {
        super( LocalDateTime.class );
    }

    @Override
    public LocalDateTime deserialize( JsonParser jp, DeserializationContext ctxt ) {
        try {
            return LocalDateTime.parse( jp.readValueAs( String.class ), DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC")) );
        }
        catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }
}


public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> {

    private static final long serialVersionUID = 1L;

    public LocalDateTimeSerializer() {
        super(LocalDateTime.class);
    }

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider sp) throws IOException {
        String isoLocalDateTimeString = value.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + ".000Z";
        gen.writeString(isoLocalDateTimeString);
    }
}

用法:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime time;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate date;