SpringBoot使⽤FastJson反序列化⾃定义属性的时间格式
#问题的由来
由于前端传的⽇期格式⽐较奇葩 (eg:09-13/2018),如果是以此⽅式到后台通过@ResquestBody 注解的接收话会报序列化错误。
解决办法
在要进⾏反序列化的实体类上添加注解@JSONField(deserializeUsing=DateExtraProcessor.class),其中DateExtraProcessor就是⾃定义的反序列化处理类。
import java.io.Serializable;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
import fig.DateExtraProcessor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Article implements Serializable {
/**
*
*/
private static final long serialVersionUID = -633397330272103911L;
private int id;
private int news_type;
private String intro_img;
private String content;
@JSONField(deserializeUsing=DateExtraProcessor.class)
private Date time;
private String title;
private String describe;
}
import flect.Type;
import java.util.Date;
import java.util.Map;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer;
slf4j.Slf4j;
@Slf4j
public class DateExtraProcessor extends AbstractDateDeserializer {
private  DateTimeFormatter pattern = DateTimeFormat.forPattern("MM-dd/yyyy");
@Override
protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object value) {
Object object = parser.input;//此处得到是前端传来的所有数据的原始格式字符串
Map map = JSON.parseObject(String.valueOf(object),Map.class);//将json字符串转为map,⽅便后续操作
String timestr =(String) (String.valueOf(fieldName));//获取对应参数值(ps:不明⽩参数类型为什么是Object)
if(log.isDebugEnabled()){
log.debug("time is  {}",timestr);
}
Date date = parseStr2Date(timestr);//格式化成⽇期
return (T)date;
}
private  Date parseStr2Date(String source) {
return DateTime.parse(source, pattern).toDate();//使⽤joda的⽇期操作类
}
@Override
public int getFastMatchToken() {
return JSONToken.LITERAL_STRING;
}
}
关于AbstractDateDeserializer这个类,我⼀开始也不知道,是通过看JSON 这个类的源码看到ParserConfig这个核⼼序列化的处理类中定义了多个序列化处理类
private final IdentityHashMap<Type, ObjectDeserializer> deserializers        = new IdentityHashMap<Type, ObjectDeserializer>();
deserializers.put(SimpleDateFormat.class, MiscCodec.instance);
deserializers.put(java.sql.Timestamp.class, SqlDateDeserializer.instance_timestamp);
deserializers.put(java.sql.Date.class, SqlDateDeserializer.instance);
deserializers.put(java.sql.Time.class, TimeDeserializer.instance);
deserializers.put(java.util.Date.class, DateCodec.instance);
deserializers.put(Calendar.class, CalendarCodec.instance);
deserializers.put(XMLGregorianCalendar.class, CalendarCodec.instance);
deserializers.put(JSONObject.class, MapDeserializer.instance);
deserializers.put(JSONArray.class, CollectionCodec.instance);
deserializers.put(Map.class, MapDeserializer.instance);
deserializers.put(HashMap.class, MapDeserializer.instance);
deserializers.put(HashMap.class, MapDeserializer.instance);
deserializers.put(LinkedHashMap.class, MapDeserializer.instance);
deserializers.put(TreeMap.class, MapDeserializer.instance);
deserializers.put(ConcurrentMap.class, MapDeserializer.instance);
deserializers.put(ConcurrentHashMap.class, MapDeserializer.instance);
deserializers.put(Collection.class, CollectionCodec.instance);
deserializers.put(List.class, CollectionCodec.instance);
deserializers.put(ArrayList.class, CollectionCodec.instance);
deserializers.put(Object.class, JavaObjectDeserializer.instance);
deserializers.put(String.class, StringCodec.instance);
deserializers.put(StringBuffer.class, StringCodec.instance);
deserializers.put(StringBuilder.class, StringCodec.instance);
deserializers.put(char.class, CharacterCodec.instance);
deserializers.put(Character.class, CharacterCodec.instance);
deserializers.put(byte.class, NumberDeserializer.instance);
deserializers.put(Byte.class, NumberDeserializer.instance);
deserializers.put(short.class, NumberDeserializer.instance);
deserializers.put(Short.class, NumberDeserializer.instance);
deserializers.put(int.class, IntegerCodec.instance);
deserializers.put(Integer.class, IntegerCodec.instance);
deserializers.put(long.class, LongCodec.instance);
deserializers.put(Long.class, LongCodec.instance);
deserializers.put(BigInteger.class, BigIntegerCodec.instance);
deserializers.put(BigDecimal.class, BigDecimalCodec.instance);
deserializers.put(float.class, FloatCodec.instance);
deserializers.put(Float.class, FloatCodec.instance);
deserializers.put(double.class, NumberDeserializer.instance);
deserializers.put(Double.class, NumberDeserializer.instance);
deserializers.put(boolean.class, BooleanCodec.instance);
deserializers.put(Boolean.class, BooleanCodec.instance);
deserializers.put(Class.class, MiscCodec.instance);
deserializers.put(char[].class, new CharArrayCodec());
deserializers.put(AtomicBoolean.class, BooleanCodec.instance);
deserializers.put(AtomicInteger.class, IntegerCodec.instance);
deserializers.put(AtomicLong.class, LongCodec.instance);fastjson怎么用
deserializers.put(AtomicReference.class, ReferenceCodec.instance);
deserializers.put(WeakReference.class, ReferenceCodec.instance);
deserializers.put(SoftReference.class, ReferenceCodec.instance);
deserializers.put(UUID.class, MiscCodec.instance);
deserializers.put(TimeZone.class, MiscCodec.instance);
deserializers.put(Locale.class, MiscCodec.instance);
deserializers.put(Currency.class, MiscCodec.instance);
deserializers.put(InetAddress.class, MiscCodec.instance);
deserializers.put(Inet4Address.class, MiscCodec.instance);
deserializers.put(Inet6Address.class, MiscCodec.instance);
deserializers.put(InetSocketAddress.class, MiscCodec.instance);
deserializers.put(File.class, MiscCodec.instance);
deserializers.put(URI.class, MiscCodec.instance);
deserializers.put(URL.class, MiscCodec.instance);
deserializers.put(Pattern.class, MiscCodec.instance);
deserializers.put(Charset.class, MiscCodec.instance);
deserializers.put(JSONPath.class, MiscCodec.instance);
deserializers.put(Number.class, NumberDeserializer.instance);
deserializers.put(AtomicIntegerArray.class, AtomicCodec.instance);
deserializers.put(AtomicLongArray.class, AtomicCodec.instance);
deserializers.put(StackTraceElement.class, StackTraceElementDeserializer.instance);
deserializers.put(Serializable.class, JavaObjectDeserializer.instance);
deserializers.put(Cloneable.class, JavaObjectDeserializer.instance);
deserializers.put(Comparable.class, JavaObjectDeserializer.instance);
deserializers.put(Closeable.class, JavaObjectDeserializer.instance);
deserializers.put(JSONPObject.class, new JSONPDeserializer());
这些⾃定义的处理类都实现了ObjectDeserializer接⼝,由此我在com.alibaba.fastjson.parser.deserializer这个包下看到了⼀些和反序列化相关的核⼼处理类,AbstractDateDeserializer此类就在其中。(ps:不得不吐槽下fastjson的⽂档写的真烂, ⽽且很多类都没介绍)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。