JsonMapper日期类型转换代码优化
This commit is contained in:
@@ -11,6 +11,7 @@ import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonParser.Feature;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.introspect.Annotated;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
@@ -49,9 +50,9 @@ public class JsonMapper extends ObjectMapper {
|
||||
private static final class JsonMapperHolder {
|
||||
private static final JsonMapper INSTANCE = new JsonMapper();
|
||||
}
|
||||
|
||||
|
||||
public JsonMapper() {
|
||||
// 设置默认时区、默认日期格式
|
||||
// 日志类型格式化处理
|
||||
this.setLocaleTimeZoneDateFormat();
|
||||
// 为Null时不序列化
|
||||
this.setSerializationInclusion(Include.NON_NULL);
|
||||
@@ -68,7 +69,7 @@ public class JsonMapper extends ObjectMapper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认时区、默认日期格式
|
||||
* 日志类型格式化处理
|
||||
* @author ThinkGem
|
||||
*/
|
||||
public JsonMapper setLocaleTimeZoneDateFormat(){
|
||||
@@ -78,32 +79,52 @@ public class JsonMapper extends ObjectMapper {
|
||||
.getProperty("lang.defaultTimeZone", "GMT+08:00")));
|
||||
this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final String[] pattern = new String[] {"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS"};
|
||||
class JeeSiteJsonSerializer extends JsonSerializer<Date> {
|
||||
private final String pattern;
|
||||
private JeeSiteJsonSerializer(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
@Override
|
||||
public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
if (value != null){
|
||||
if (StringUtils.isNotBlank(pattern)) {
|
||||
gen.writeString(DateUtils.formatDate(value, pattern));
|
||||
} else {
|
||||
gen.writeString(DateUtils.formatDateTime(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
class JeeSiteJsonDeserializer extends JsonDeserializer<Date> {
|
||||
private final String pattern;
|
||||
private JeeSiteJsonDeserializer(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
@Override
|
||||
public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
||||
if (StringUtils.isNotBlank(pattern)) {
|
||||
return DateUtils.parseDate(parser.getText(), pattern);
|
||||
} else {
|
||||
return DateUtils.parseDate(parser.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Object findSerializer(Annotated a) {
|
||||
if (a instanceof AnnotatedMethod) {
|
||||
AnnotatedElement m = a.getAnnotated();
|
||||
JsonFormat jf = m.getAnnotation(JsonFormat.class);
|
||||
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(),
|
||||
"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS")) {
|
||||
return new JsonSerializer<Date>(){
|
||||
@Override
|
||||
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||
if (value != null){
|
||||
jgen.writeString(DateUtils.formatDate(value, jf.pattern()));
|
||||
}
|
||||
}
|
||||
};
|
||||
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(), pattern)) {
|
||||
return new JeeSiteJsonSerializer(jf.pattern());
|
||||
}
|
||||
AnnotatedMethod am = (AnnotatedMethod) a;
|
||||
if (am.getRawReturnType() == Date.class) {
|
||||
return new JsonSerializer<Date>(){
|
||||
@Override
|
||||
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||
if (value != null){
|
||||
jgen.writeString(DateUtils.formatDateTime(value));
|
||||
}
|
||||
}
|
||||
};
|
||||
return new JeeSiteJsonSerializer(null);
|
||||
}
|
||||
} else if (a instanceof AnnotatedClass) {
|
||||
if (a.getRawType() == Date.class) {
|
||||
return new JeeSiteJsonSerializer(null);
|
||||
}
|
||||
}
|
||||
return super.findSerializer(a);
|
||||
@@ -113,23 +134,16 @@ public class JsonMapper extends ObjectMapper {
|
||||
if (a instanceof AnnotatedMethod) {
|
||||
AnnotatedElement m = a.getAnnotated();
|
||||
JsonFormat jf = m.getAnnotation(JsonFormat.class);
|
||||
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(),
|
||||
"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS")) {
|
||||
return new JsonDeserializer<Date>(){
|
||||
@Override
|
||||
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
return DateUtils.parseDate(p.getText(), jf.pattern());
|
||||
}
|
||||
};
|
||||
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(), pattern)) {
|
||||
return new JeeSiteJsonDeserializer(jf.pattern());
|
||||
}
|
||||
AnnotatedMethod am = (AnnotatedMethod) a;
|
||||
if (am.getParameterCount() > 0 && am.getParameterType(0).getRawClass() == Date.class) {
|
||||
return new JsonDeserializer<Date>(){
|
||||
@Override
|
||||
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
return DateUtils.parseDate(p.getText());
|
||||
}
|
||||
};
|
||||
return new JeeSiteJsonDeserializer(null);
|
||||
}
|
||||
} else if (a instanceof AnnotatedClass) {
|
||||
if (a.getRawType() == Date.class) {
|
||||
return new JeeSiteJsonDeserializer(null);
|
||||
}
|
||||
}
|
||||
return super.findDeserializer(a);
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.jeesite.common.collect.ListUtils;
|
||||
import com.jeesite.common.collect.MapUtils;
|
||||
import com.jeesite.common.mapper.JsonMapper;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,6 +31,7 @@ public class JsonMapperTest {
|
||||
map.put("pId", 1);
|
||||
map.put("name", "你好");
|
||||
map.put("open", true);
|
||||
map.put("date", new Date());
|
||||
list.add(map);
|
||||
String json = JsonMapper.toJson(list);
|
||||
System.out.println(json);
|
||||
|
||||
Reference in New Issue
Block a user