fastjson 序列化反序列化配置。
自定义转换器
1 类转 JSON, 继承 EnumSerializer等 fastjson 已经的或者 实现 ObjectSerializer 接口
config.getSerializeConfig().put(Double.class, new DoubleSerializerConfig(new DecimalFormat("0.00")));
添加进配置
2 JSON转类 实现 ObjectDeserializer 接口
config.getParserConfig().getDeserializers().put(OrderStatusEnum.class, new OrderStatusEnumSerializerConfig());
添加进配置
代码如下:
@Configuration
@Slf4j
public class FJsonConfig {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.getSerializeConfig().put(Double.class, new DoubleSerializerConfig(new DecimalFormat("0.00")));
config.getSerializeConfig().put(Enum.class,new OrderStatusEnumSerializerConfig());
config.getParserConfig().getDeserializers().put(OrderStatusEnum.class, new OrderStatusEnumSerializerConfig());
return new HttpMessageConverters(converter);
}
/**
* Double格式保留两位小数点(0.00)
*/
public static class DoubleSerializerConfig extends DoubleSerializer {
private DecimalFormat decimalFormat = null;
public DoubleSerializerConfig(DecimalFormat decimalFormat) {
this.decimalFormat = decimalFormat;
}
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull(SerializerFeature.WriteNullNumberAsZero);
return;
}
double doubleValue = ((Double) object).doubleValue();
if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
out.writeNull();
} else {
if (decimalFormat == null) {
out.writeDouble(doubleValue, true);
} else {
String doubleText = decimalFormat.format(doubleValue);
out.write("\"" + doubleText + "\"");
}
}
}
}
public static class OrderStatusEnumSerializerConfig extends EnumSerializer implements ObjectDeserializer {
public final static OrderStatusEnumSerializerConfig instance = new OrderStatusEnumSerializerConfig();
private final Member member;
public OrderStatusEnumSerializerConfig() {
this.member = null;
}
public OrderStatusEnumSerializerConfig(Member member) {
this.member = member;
}
public void write(JSONSerializer serializer, Object object, Object fieldName, java.lang.reflect.Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object instanceof OrderStatusEnum) {
//这里用 int 10 会变成 (10
//多一个左边的括号 不知道为什么
out.write(((OrderStatusEnum) object).toJSONString());
} else if (member == null) {
out.writeEnum((Enum<?>) object);
return;
}
if (object instanceof OrderStatusEnum) {
//这里用 int 10 会变成 (10
//多一个左边的括号 不知道为什么
serializer.write( ((OrderStatusEnum) object).toJSONString());
return;
}
Object fieldValue = null;
try {
if (member instanceof Field) {
fieldValue = ((Field) member).get(object);
} else {
fieldValue = ((Method) member).invoke(object);
}
} catch (Exception e) {
throw new JSONException("getEnumValue error", e);
}
serializer.write(fieldValue);
}
@Override
public <T> T deserialze(DefaultJSONParser parser, java.lang.reflect.Type type, Object fieldName) {
int value = (int) parser.parse();
for (OrderStatusEnum statusEnum : OrderStatusEnum.values()) {
if (statusEnum.getCode() == value) {
return (T) (statusEnum);
}
}
return (T) (OrderStatusEnum.CREATE);
}
@Override
public int getFastMatchToken() {
return 0;
}
}
}