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