yangyoupeng

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

1 +package com.zhaoonline.common.json;
2 +
3 +import java.io.IOException;
4 +import java.text.ParseException;
5 +import java.text.SimpleDateFormat;
6 +import java.util.Date;
7 +
8 +import com.fasterxml.jackson.core.JsonParser;
9 +import com.fasterxml.jackson.core.JsonProcessingException;
10 +import com.fasterxml.jackson.databind.DeserializationContext;
11 +import com.fasterxml.jackson.databind.JsonDeserializer;
12 +
13 +public class JsonDateDeserializer extends JsonDeserializer<Date>{
14 + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
15 + @Override
16 + public Date deserialize(JsonParser jsonparser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
17 + String date = jsonparser.getText();
18 + try {
19 + return dateFormat.parse(date);
20 + } catch (ParseException e) {
21 + throw new RuntimeException(e);
22 + }
23 + }
24 +
25 +}
...@@ -2,17 +2,29 @@ package com.zhaoonline.common.json; ...@@ -2,17 +2,29 @@ package com.zhaoonline.common.json;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.InputStream; 4 import java.io.InputStream;
5 +import java.util.Date;
5 import java.util.Map; 6 import java.util.Map;
6 7
7 import com.fasterxml.jackson.core.JsonParseException; 8 import com.fasterxml.jackson.core.JsonParseException;
8 import com.fasterxml.jackson.core.JsonProcessingException; 9 import com.fasterxml.jackson.core.JsonProcessingException;
10 +import com.fasterxml.jackson.databind.JsonDeserializer;
9 import com.fasterxml.jackson.databind.JsonMappingException; 11 import com.fasterxml.jackson.databind.JsonMappingException;
10 import com.fasterxml.jackson.databind.ObjectMapper; 12 import com.fasterxml.jackson.databind.ObjectMapper;
13 +import com.fasterxml.jackson.databind.module.SimpleModule;
11 14
12 public class JsonUtils { 15 public class JsonUtils {
13 16
14 private static ObjectMapper mapper= new ObjectMapper(); 17 private static ObjectMapper mapper= new ObjectMapper();
15 18
19 + static{
20 + SimpleModule module = new SimpleModule();
21 + module.addSerializer(Date.class, new JsonDateSerializer());
22 + mapper.registerModule(module);
23 + JsonDateDeserializer deser=new JsonDateDeserializer();
24 + module.addDeserializer(Date.class, deser);
25 + mapper.registerModule(module);
26 + }
27 +
16 /** 28 /**
17 * Method name: toObject <BR> 29 * Method name: toObject <BR>
18 * Description: 读取失败就返回null <BR> 30 * Description: 读取失败就返回null <BR>
......
...@@ -2,6 +2,7 @@ package com.zhaoonline.common.es; ...@@ -2,6 +2,7 @@ package com.zhaoonline.common.es;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 +import java.util.Date;
5 import java.util.HashMap; 6 import java.util.HashMap;
6 import java.util.HashSet; 7 import java.util.HashSet;
7 import java.util.List; 8 import java.util.List;
...@@ -19,6 +20,7 @@ import com.zhaoonline.common.es.bean.QueryResponse; ...@@ -19,6 +20,7 @@ import com.zhaoonline.common.es.bean.QueryResponse;
19 import com.zhaoonline.common.es.bean.TermQuery; 20 import com.zhaoonline.common.es.bean.TermQuery;
20 import com.zhaoonline.common.es.bean.TermsQuery; 21 import com.zhaoonline.common.es.bean.TermsQuery;
21 import com.zhaoonline.common.json.JsonUtils; 22 import com.zhaoonline.common.json.JsonUtils;
23 +import com.zhaoonline.common.json.TestDateObject;
22 24
23 public class TestESClient { 25 public class TestESClient {
24 26
...@@ -256,4 +258,25 @@ public class TestESClient { ...@@ -256,4 +258,25 @@ public class TestESClient {
256 Assert.assertTrue(result); 258 Assert.assertTrue(result);
257 } 259 }
258 260
261 + @Test
262 + public void testBulkAddDate() throws UnsupportedOperationException, IOException{
263 + String index="zhaoon1";
264 + String type="test1";
265 + ESClientConfiguration config=new ESClientConfiguration();
266 + config.addHostPorts("192.168.0.162:9200");
267 + ESHttpClient client= new ESHttpClient(index,type,config);
268 + client.init();
269 + List ids=new ArrayList<>();
270 + List dataList=new ArrayList<>();
271 + for(int i=0;i<100;i++){
272 + TestDateObject o=new TestDateObject();
273 + o.setId(i);
274 + o.setDate(new Date(System.currentTimeMillis()));
275 + dataList.add(o);
276 + ids.add(i);
277 + }
278 + boolean result=client.bulkAddDoc(ids,dataList);
279 + Assert.assertTrue(result);
280 + }
281 +
259 } 282 }
......
...@@ -5,7 +5,6 @@ import java.util.Date; ...@@ -5,7 +5,6 @@ import java.util.Date;
5 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 5 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
6 6
7 public class TestDateObject { 7 public class TestDateObject {
8 - @JsonSerialize(using=JsonDateSerializer.class)
9 public Date date; 8 public Date date;
10 9
11 public Date getDate() { 10 public Date getDate() {
...@@ -16,4 +15,26 @@ public class TestDateObject { ...@@ -16,4 +15,26 @@ public class TestDateObject {
16 this.date = date; 15 this.date = date;
17 } 16 }
18 17
18 + public String name;
19 +
20 +
21 + public Integer id;
22 +
23 + public String getName() {
24 + return name;
25 + }
26 +
27 + public void setName(String name) {
28 + this.name = name;
29 + }
30 +
31 + public Integer getId() {
32 + return id;
33 + }
34 +
35 + public void setId(Integer id) {
36 + this.id = id;
37 + }
38 +
39 +
19 } 40 }
......
1 package com.zhaoonline.common.json; 1 package com.zhaoonline.common.json;
2 2
3 +import java.io.IOException;
4 +import java.text.ParseException;
5 +import java.text.SimpleDateFormat;
3 import java.util.Date; 6 import java.util.Date;
4 7
8 +import org.junit.Assert;
5 import org.junit.Test; 9 import org.junit.Test;
6 10
7 import com.fasterxml.jackson.core.JsonProcessingException; 11 import com.fasterxml.jackson.core.JsonProcessingException;
8 import com.fasterxml.jackson.databind.ObjectMapper; 12 import com.fasterxml.jackson.databind.ObjectMapper;
13 +import com.fasterxml.jackson.databind.module.SimpleModule;
9 14
10 public class TestJsonDateSerializer { 15 public class TestJsonDateSerializer {
11 @Test 16 @Test
12 public void testDateParse() throws JsonProcessingException{ 17 public void testDateParse() throws JsonProcessingException{
13 ObjectMapper mapper= new ObjectMapper(); 18 ObjectMapper mapper= new ObjectMapper();
19 + SimpleModule module = new SimpleModule();
20 + module.addSerializer(Date.class, new JsonDateSerializer());
21 + mapper.registerModule(module);
22 +
14 TestDateObject o=new TestDateObject(); 23 TestDateObject o=new TestDateObject();
15 o.setDate(new Date(System.currentTimeMillis())); 24 o.setDate(new Date(System.currentTimeMillis()));
16 System.out.println(mapper.writeValueAsString(o));; 25 System.out.println(mapper.writeValueAsString(o));;
17 } 26 }
27 +
28 +
29 + @Test
30 + public void testDateParseWithJsonUtils() throws JsonProcessingException {
31 +
32 + TestDateObject object=new TestDateObject();
33 + object.setDate(new Date(System.currentTimeMillis()));
34 + System.out.println(JsonUtils.toJson(object));;
35 + }
36 +
37 +
38 + @Test
39 + public void testDateDeserialWithJsonUtils() throws ParseException, IOException {
40 + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
41 + String testDateString="{\"date\":\"2016-11-01 19:28:38\",\"name\":null,\"id\":null}";
42 +
43 + TestDateObject testDate=JsonUtils.toObject(testDateString, TestDateObject.class);
44 + Assert.assertEquals(dateFormat.parse("2016-11-01 19:28:38") , testDate.getDate());
45 + Assert.assertNull(testDate.getId());
46 + Assert.assertNull(testDate.getName());
47 + }
48 +
49 +
50 +
18 } 51 }
......