yangyoupeng

添加Date类型的Json序列化和反序列化的器

package com.zhaoonline.common.json;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
public class JsonDateDeserializer extends JsonDeserializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date deserialize(JsonParser jsonparser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String date = jsonparser.getText();
try {
return dateFormat.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
......@@ -2,17 +2,29 @@ package com.zhaoonline.common.json;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class JsonUtils {
private static ObjectMapper mapper= new ObjectMapper();
static{
SimpleModule module = new SimpleModule();
module.addSerializer(Date.class, new JsonDateSerializer());
mapper.registerModule(module);
JsonDateDeserializer deser=new JsonDateDeserializer();
module.addDeserializer(Date.class, deser);
mapper.registerModule(module);
}
/**
* Method name: toObject <BR>
* Description: 读取失败就返回null <BR>
......
......@@ -2,6 +2,7 @@ package com.zhaoonline.common.es;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -19,6 +20,7 @@ import com.zhaoonline.common.es.bean.QueryResponse;
import com.zhaoonline.common.es.bean.TermQuery;
import com.zhaoonline.common.es.bean.TermsQuery;
import com.zhaoonline.common.json.JsonUtils;
import com.zhaoonline.common.json.TestDateObject;
public class TestESClient {
......@@ -256,4 +258,25 @@ public class TestESClient {
Assert.assertTrue(result);
}
@Test
public void testBulkAddDate() throws UnsupportedOperationException, IOException{
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("192.168.0.162:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
List ids=new ArrayList<>();
List dataList=new ArrayList<>();
for(int i=0;i<100;i++){
TestDateObject o=new TestDateObject();
o.setId(i);
o.setDate(new Date(System.currentTimeMillis()));
dataList.add(o);
ids.add(i);
}
boolean result=client.bulkAddDoc(ids,dataList);
Assert.assertTrue(result);
}
}
......
......@@ -5,7 +5,6 @@ import java.util.Date;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class TestDateObject {
@JsonSerialize(using=JsonDateSerializer.class)
public Date date;
public Date getDate() {
......@@ -16,4 +15,26 @@ public class TestDateObject {
this.date = date;
}
public String name;
public Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
......
package com.zhaoonline.common.json;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class TestJsonDateSerializer {
@Test
public void testDateParse() throws JsonProcessingException{
ObjectMapper mapper= new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Date.class, new JsonDateSerializer());
mapper.registerModule(module);
TestDateObject o=new TestDateObject();
o.setDate(new Date(System.currentTimeMillis()));
System.out.println(mapper.writeValueAsString(o));;
}
@Test
public void testDateParseWithJsonUtils() throws JsonProcessingException {
TestDateObject object=new TestDateObject();
object.setDate(new Date(System.currentTimeMillis()));
System.out.println(JsonUtils.toJson(object));;
}
@Test
public void testDateDeserialWithJsonUtils() throws ParseException, IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String testDateString="{\"date\":\"2016-11-01 19:28:38\",\"name\":null,\"id\":null}";
TestDateObject testDate=JsonUtils.toObject(testDateString, TestDateObject.class);
Assert.assertEquals(dateFormat.parse("2016-11-01 19:28:38") , testDate.getDate());
Assert.assertNull(testDate.getId());
Assert.assertNull(testDate.getName());
}
}
......