fastjson将Long型转成json精度丢失问题解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson将Long型转成json精度丢失问题解决相关的知识,希望对你有一定的参考价值。
这是jackson
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> c : converters) {
if (c instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) c;
ObjectMapper objectMapper = mappingJackson2HttpMessageConverter.getObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
if (this.dateFormat != null) {
DateFormat myDateFormat = new SimpleDateFormat(dateFormat);
objectMapper.setDateFormat(myDateFormat);
}
// 注册Long专用的json转换器
SimpleModule module = new SimpleModule();
//修复Long类型太长,丢失精度问题
module.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
module.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
module.addSerializer(BigDecimal.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
module.addSerializer(BigInteger.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
objectMapper.registerModule(module);
}
}
}
下面换成fastjson
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- json库统一使用fastjson -->
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
生成序列化的转换类ToStringSerializer
public class ToStringSerializer implements ObjectSerializer {
public static final ToStringSerializer instance = new ToStringSerializer();
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
return;
}
String strVal = object.toString();
out.writeString(strVal);
}
}
ToStringSerializer的Web配置
@Bean
public HttpMessageConverters getConverters() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
SerializerFeature.WriteMapNullValue,
SerializerFeature.QuoteFieldNames,
SerializerFeature.IgnoreNonFieldGetter
);
SerializeConfig serializeConfig = new CustomSerializeConfig();
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
config.setSerializeConfig(serializeConfig);
converter.setFastJsonConfig(config);
List<MediaType> mediaTypes = Arrays.asList(
MediaType.APPLICATION_JSON_UTF8,
MediaType.TEXT_PLAIN,
MediaType.TEXT_html,
MediaType.TEXT_XML,
MediaType.APPLICATION_OCTET_STREAM);
converter.setSupportedMediaTypes(mediaTypes);
return new HttpMessageConverters(converter);
}
按以上所示,注册序列化类型,经测试,功能正常!
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 初始化转换器
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
// 初始化一个转换器配置
FastJsonConfig fastJsonConfig = new FastJsonConfig();
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm";
fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.DisableCircularReferenceDetect);
//解决Long转json精度丢失的问题
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
// 将配置设置给转换器并添加到HttpMessageConverter转换器列表中
fastConvert.setFastJsonConfig(fastJsonConfig);
converters.add(fastConvert);
}
package com.chz.component.basic.configure;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import java.math.BigInteger;
public class FastJsonConfigExt extends FastJsonConfig {
public FastJsonConfigExt(){
super();
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class,ToStringSerializer.instance);
serializeConfig.put(Long.TYPE,ToStringSerializer.instance);
this.setSerializeConfig(serializeConfig);
}
}
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastConverter =
new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
以上是关于fastjson将Long型转成json精度丢失问题解决的主要内容,如果未能解决你的问题,请参考以下文章