以处理LocalDateTime格式的全局反序列化器为例,全局序列化器类似
1、全局反序列化器代码
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.datatype.jsr310.deser.JSR310DateTimeDeserializerBase;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class CustomerLocalDateTimeDeserializer extends JSR310DateTimeDeserializerBase<LocalDateTime> {
public CustomerLocalDateTimeDeserializer() {
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
public CustomerLocalDateTimeDeserializer(DateTimeFormatter formatter) {
super(LocalDateTime.class, formatter);
}
protected CustomerLocalDateTimeDeserializer(CustomerLocalDateTimeDeserializer base, Boolean leniency) {
super(base, leniency);
}
protected CustomerLocalDateTimeDeserializer withDateFormat(DateTimeFormatter formatter) {
return new CustomerLocalDateTimeDeserializer(formatter);
}
protected CustomerLocalDateTimeDeserializer withLeniency(Boolean leniency) {
return new CustomerLocalDateTimeDeserializer(this, leniency);
}
protected CustomerLocalDateTimeDeserializer withShape(JsonFormat.Shape shape) {
return this;
}
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
if (parser.hasTokenId(6)) {
String string = parser.getText().trim();
if (string.length() == 0) {
return !this.isLenient() ? (LocalDateTime)this._failForNotLenient(parser, context, JsonToken.VALUE_STRING) : null;
} else {
try {
// 这里有点改造
if (string.length() > 10 && string.charAt(10) == 'T') {
return string.endsWith("Z") ?
LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC) :
LocalDateTime.parse(string);
} else {
LocalDateTime.parse(string);
return LocalDateTime.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
} catch (DateTimeException var12) {
return (LocalDateTime)this._handleDateTimeException(context, var12, string);
}
}
} else {
// 直接从com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize() 拷贝的
if (parser.isExpectedStartArrayToken()) {
JsonToken t = parser.nextToken();
if (t == JsonToken.END_ARRAY) {
return null;
}
LocalDateTime result;
if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT) && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
result = this.deserialize(parser, context);
if (parser.nextToken() != JsonToken.END_ARRAY) {
this.handleMissingEndArrayForSingle(parser, context);
}
return result;
}
if (t == JsonToken.VALUE_NUMBER_INT) {
int year = parser.getIntValue();
int month = parser.nextIntValue(-1);
int day = parser.nextIntValue(-1);
int hour = parser.nextIntValue(-1);
int minute = parser.nextIntValue(-1);
t = parser.nextToken();
if (t == JsonToken.END_ARRAY) {
result = LocalDateTime.of(year, month, day, hour, minute);
} else {
int second = parser.getIntValue();
t = parser.nextToken();
if (t == JsonToken.END_ARRAY) {
result = LocalDateTime.of(year, month, day, hour, minute, second);
} else {
int partialSecond = parser.getIntValue();
if (partialSecond < 1000 && !context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) {
partialSecond *= 1000000;
}
if (parser.nextToken() != JsonToken.END_ARRAY) {
throw context.wrongTokenException(parser, this.handledType(), JsonToken.END_ARRAY, "Expected array to end");
}
result = LocalDateTime.of(year, month, day, hour, minute, second, partialSecond);
}
}
return result;
}
context.reportInputMismatch(this.handledType(), "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT", new Object[]{t});
}
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
return (LocalDateTime)parser.getEmbeddedObject();
} else {
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
this._throwNoNumericTimestampNeedTimeZone(parser, context);
}
return (LocalDateTime)this._handleUnexpectedToken(context, parser, "Expected array or string.", new Object[0]);
}
}
}
}
2、将自定义的全局反序列化器加入调用链(配置)
import com.haier.boot.deserializer.CustomerLocalDateTimeDeserializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class LocalDateTimeSerializerConfig {
@Bean
public CustomerLocalDateTimeDeserializer customerLocalDateTimeDeserializer() {
return new CustomerLocalDateTimeDeserializer();
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
builder.deserializerByType(LocalDateTime.class, customerLocalDateTimeDeserializer());
};
}
}
标签:JsonToken,return,自定义,parser,CustomerLocalDateTimeDeserializer,LocalDateTime,impo
From: https://www.cnblogs.com/shareToAll/p/17714135.html