直播平台开发,序列化时实现任意类型自定义转换
1、前言
在对象进行序列化时,希望对序列化的字段进行格式化处理,比如:Double与String转换、BigDecimal与String转换、Long与Date转换、Long与LocalDateTime转换等不同类型的字段之间实现转换操作;
只需要通过重新定义Jackson的ObjectMapper对象通过addSerializer()添加自定义的序列化转换器即可;
2、定义ObjectMapperConverter
ObjectMapperConverter:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.lhz.demo.converter.DataConverterConfig;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* @Description: 在json时进行转换操作, 比如:忽略null值字段进行序列化\BigDecimal格式化\Date格式化\基本类型转为String等操作
**/
public class ObjectMapperConverter {
private static ObjectMapper objectMapper;
private ObjectMapperConverter() {
}
public static synchronized ObjectMapper getInstance() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
// 禁止null值字段进行序列化
// 如果有需要则进行使用
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 添加默认序列化,将字段转化为转换String,支持各种可以直接使用toString()方法的类型
// 如果有需要则进行开启
// module.addSerializer(BigInteger.class, new ToStringSerializer());
// module.addSerializer(Long.class, new ToStringSerializer());
// module.addSerializer(Integer.class, new ToStringSerializer());
// module.addSerializer(BigInteger.class, new ToStringSerializer());
// 添加自定义序列化 Serializer
module.addSerializer(BigDecimal.class, new BigDecimalSerializer());
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
module.addSerializer(Date.class, new DateSerializer());
objectMapper.registerModule(module);
return objectMapper;
}
return objectMapper;
}
public static String toJsonString(Object value) {
try {
return getInstance().writeValueAsString(value);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 序列化实现BigDecimal转化为String
*/
private static class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
String res = null;
if (value != null) {
int scale = value.scale();
if (scale > 2) {
res = value.toString();
} else {
DecimalFormat df = new DecimalFormat("#0.00");
res = df.format(value);
}
gen.writeString(res);
}
}
}
/**
* 序列化实现 LocalDateTime转化为Long
*/
private static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
if (value != null) {
long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
gen.writeNumber(timestamp);
}
}
}
/**
* 序列化实现 Date转化为String
*/
private static class DateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = df.format(value);
gen.writeString(format);
}
}
}
}
3、定义序列化实体
@Data
public class TestEntity {
private Long id;
private Integer num;
private BigInteger count;
private BigDecimal price;
private LocalDateTime createTime;
private Date time;
}
以上就是直播平台开发,序列化时实现任意类型自定义转换的全部代码,更多内容请关注之后的文章
标签:自定义,value,直播,private,new,import,序列化,class From: https://www.cnblogs.com/yunbaomengnan/p/16806742.html