java 序列化 混淆,GsonBuilder序列化器混淆了时间戳和日期

I have used Gson to serialize and deserialize model class to json string , but the problem is gson takes date variabless to timestamp serializer.My class contain both Date and Timestamp variables but both are serialized by the same Timestamp serializer.I have wrote both Timestamp and date serializer in my GsonBuilder.Below is my class

StaffDetails.java :

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;

}

}

Below is my gson serializer

String jsonAccts = null;

SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

SimpleDateFormat dtfDate=new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

try{

GsonBuilder builder = new GsonBuilder();

builder.registerTypeAdapter(Date.class, new JsonSerializer() {

@Override

public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {

String jsDate = dtfDate.format(src);

return new JsonPrimitive(jsDate);

}

});

builder.registerTypeAdapter(Timestamp.class, new JsonSerializer() {

@Override

public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {

String jsDate = dtf.format(src);

return new JsonPrimitive(jsDate);

}

});

Gson gson = builder.create();

Type listType = new TypeToken>() {}.getType();

List staffDetailsList = new ArrayList();

staffDetailsList = loopDao.getStaffLeaveList(customUser.getId());

jsonAccts = gson.toJson(staffDetailsList, listType);

}catch(Exception e){

e.printStackTrace();

}

The problem is both dob and createdBy are going to TimeStamp serializer.

What i want is dob should go to Date serializer and createdBy to TimeStamp serializer.Please help me.

解决方案

Please use separate Serialize / de-Serialize classes as this.

public class LocalDateDeserializer extends StdDeserializer {

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

{

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 {

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 {

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);

}

}

Usage:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)

@JsonSerialize(using = LocalDateTimeSerializer.class)

private LocalDateTime time;

@JsonDeserialize(using = LocalDateDeserializer.class)

@JsonSerialize(using = LocalDateSerializer.class)

private LocalDate date;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值