java日期加序列字符串_java – Jackson De /在一般地图中序列化日期到字符串的日期...

如果你有一个POJO,你可以使用序列化器和反序列化器在get和set方法上轻松使用注释.

下面以不同方式序列化和反序列化对象的示例:List< POJO> to String,String to Map和Map to List< POJO>再次.显然,在地图中,Date值为String.

我的POJO:

@JsonAutoDetect

public class QueueTask implements Serializable {

private static final long serialVersionUID = -4411796657106403937L;

public enum ActivitiQueueStatus {

IN_PROGRESS(AsyncProcessingWorkflowContentModel.InProgressTask.TYPE.getLocalName()), //

IN_QUEUE(AsyncProcessingWorkflowContentModel.InQueueTask.TYPE.getLocalName());

private String value;

private ActivitiQueueStatus(final String value) {

this.value = value;

}

public static ActivitiQueueStatus enumOf(final String value) {

for (ActivitiQueueStatus enum_i : values()) {

if (enum_i.value.equals(value))

return enum_i;

}

throw new IllegalArgumentException("value '" + value + "' is not a valid enum");

}

}

private String user;

private Date creationDate;

private int noRowsSelected;

private ActivitiQueueStatus status;

public String getUser() {

return user;

}

public void setUser(String user) {

this.user = user;

}

@JsonSerialize(using = JsonDateSerializer.class)

public Date getCreationDate() {

return creationDate;

}

@JsonDeserialize(using = JsonDateDeSerializer.class)

public void setCreationDate(Date creationDate) {

this.creationDate = creationDate;

}

public int getNoRowsSelected() {

return noRowsSelected;

}

public void setNoRowsSelected(int noRowsSelected) {

this.noRowsSelected = noRowsSelected;

}

public ActivitiQueueStatus getStatus() {

return status;

}

public void setStatus(ActivitiQueueStatus status) {

this.status = status;

}

}

我的序列化器:

@Component

public class JsonDateDeSerializer extends JsonDeserializer {

// use joda library for thread safe issue

private static final DateTimeFormatter dateFormat = DateTimeFormat.forPattern("dd/MM/yyyy hh:mm:ss");

@Override

public Date deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {

if (jp.getCurrentToken().equals(JsonToken.VALUE_STRING))

return dateFormat.parseDateTime(jp.getText().toString()).toDate();

return null;

}

}

和解串器:

@Component

public class JsonDateSerializer extends JsonSerializer {

// use joda library for thread safe issue

private static final DateTimeFormatter dateFormat = DateTimeFormat.forPattern("dd/MM/yyyy hh:mm:ss");

@Override

public void serialize(final Date date, final JsonGenerator gen, final SerializerProvider provider) throws IOException, JsonProcessingException {

final String formattedDate = dateFormat.print(date.getTime());

gen.writeString(formattedDate);

}

}

我的服务:

public class ServiceMock {

// mock this parameter for usage.

public List getActiveActivities(QName taskStatus) {

final List listToReturn = new LinkedList();

final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

Date d1 = null, d2 = null, d3 = null, d4 = null, d5 = null;

try {

d1 = dateFormat.parse("01/02/2013 12:44:44");

d2 = dateFormat.parse("21/12/2013 16:44:44");

d3 = dateFormat.parse("21/12/2013 16:45:44");

d4 = dateFormat.parse("21/12/2013 16:44:46");

d5 = dateFormat.parse("11/09/2013 16:44:44");

} catch (ParseException e) {

}

QueueTask dataSet = new QueueTask();

dataSet = new QueueTask();

dataSet.setUser("user_b");

dataSet.setStatus(ActivitiQueueStatus.enumOf("placeInQueue"));

dataSet.setNoRowsSelected(500);

dataSet.setCreationDate(d1);

listToReturn.add(dataSet);

dataSet = new QueueTask();

dataSet.setUser("user_d");

dataSet.setStatus(ActivitiQueueStatus.enumOf("placeInQueue"));

dataSet.setNoRowsSelected(300);

dataSet.setCreationDate(d2);

listToReturn.add(dataSet);

dataSet = new QueueTask();

dataSet.setUser("user_a");

dataSet.setStatus(ActivitiQueueStatus.enumOf("inProgress"));

dataSet.setNoRowsSelected(700);

dataSet.setCreationDate(d3);

listToReturn.add(dataSet);

dataSet = new QueueTask();

dataSet.setUser("user_k");

dataSet.setStatus(ActivitiQueueStatus.enumOf("inProgress"));

dataSet.setNoRowsSelected(700);

dataSet.setCreationDate(d4);

listToReturn.add(dataSet);

dataSet = new QueueTask();

dataSet.setUser("user_l");

dataSet.setStatus(ActivitiQueueStatus.enumOf("inProgress"));

dataSet.setNoRowsSelected(700);

dataSet.setCreationDate(d5);

listToReturn.add(dataSet);

return listToReturn;

}

}

主要用途:

public class SerializationServiceTest {

private static final Logger LOGGER = LoggerFactory.getLogger(OUPQueueStatusServiceIT.class);

public void testGetActiveActivitiesSerialization() throws Exception {

LOGGER.info("testGetActiveActivitiesSerialization - start");

ServiceMock mockedService = new ServiceMock();

// AsyncProcessingWorkflowContentModel.InProgressTask.TYPE is an QName, mock this calling

List tasks = mockedService.getActiveActivities(AsyncProcessingWorkflowContentModel.InProgressTask.TYPE);

assertNotNull(tasks);

assertTrue(tasks.size() == 5);

assertNotNull(tasks.get(0).getUser());

assertNotNull(tasks.get(0).getCreationDate());

assertNotNull(tasks.get(0).getStatus());

assertNotNull(tasks.get(0).getNoRowsSelected());

final ObjectMapper mapper = new ObjectMapper();

final String jsonString = mapper.writeValueAsString(tasks);

assertNotNull(jsonString);

assertTrue(jsonString.contains("creationDate"));

// test serialization from string to Map

final List> listOfMap = mapper.readValue(jsonString, new TypeReference>>() {

});

assertNotNull(listOfMap);

final DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

for (Map map_i : listOfMap) {

// check date value

assertTrue(map_i.containsKey("creationDate"));

final Date date = formatter.parse("" + map_i.get("creationDate"));

assertNotNull(date);

assertNotNull(map_i.get("user"));

assertNotNull(map_i.get("status"));

assertNotNull(ActivitiQueueStatus.valueOf("" + map_i.get("status")));

assertNotNull(map_i.get("noRowsSelected"));

}

// test de-serialization

List deserializedTaskList = mapper.convertValue(listOfMap, new TypeReference>() {

});

assertNotNull(deserializedTaskList);

assertTrue(deserializedTaskList.size() == 5);

for (QueueTask t : deserializedTaskList) {

assertNotNull(t.getUser());

assertNotNull(t.getCreationDate());

assertNotNull(t.getDownloadType());

assertNotNull(t.getStatus());

}

LOGGER.info("testGetActiveActivitiesSerialization - end");

}

public static void main(String[] args) throws Exception {

new SerializationServiceTest().SerializationServiceTest();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值