java float 不能序列化_java - 需要Jackson序列化器用于Double,需要在运行时指定精度 - 堆栈内存溢出...

这篇博客讨论了如何使用Jackson序列化器为Double类型指定输出精度。通过创建一个自定义的`JacksonJsonDoubleSerializer`类,可以实现对Double类型的格式化。这个类会根据`FormatterPrecision`注解的精度参数来决定显示的小数位数。博客还提到了如何使用MixIn来全局应用精度控制,以便在序列化Station类的数据字段时保持一致的精度。
摘要由CSDN通过智能技术生成

我使用了大部分建议的代码但执行了以下操作,它使用DecimalFormat进行格式化,这需要输出原始文本:

import java.io.IOException;

import java.text.DecimalFormat;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.BeanProperty;

import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.ser.ContextualSerializer;

/**

* Custom serializer to serialize Double to a specified precision in output string.

* The @FormatterPrecision(precision=2) annotation needs to have been specified, for example:

*

 
 

* @JsonSerialize(using=JacksonJsonDoubleSerializer.class) @FormatterPrecision(precision=6) abstract Double getLatitude();

*

* @author sam

*

*/

public class JacksonJsonDoubleSerializer extends JsonSerializer implements ContextualSerializer {

/**

* Precision = number of digits after the decimal point to display.

* Last digit will be rounded depending on the value of the next digit.

*/

private int precision = 4;

/**

* Default constructor.

*/

public JacksonJsonDoubleSerializer ( ) {

}

/**

* Constructor.

* @param precision number of digits after the decimal to format numbers.

*/

public JacksonJsonDoubleSerializer ( int precision ) {

this.precision = precision;

}

/**

* Format to use. Create an instance so it is shared between serialize calls.

*/

private DecimalFormat format = null;

/**

*

*/

@Override

public JsonSerializer> createContextual(SerializerProvider provider, BeanProperty property ) throws JsonMappingException {

FormatterPrecision precision = property.getAnnotation(FormatterPrecision.class);

if ( precision != null ) {

return new JacksonJsonDoubleSerializer(precision.precision());

}

return this;

}

/**

* Check that the format has been created.

*/

private DecimalFormat getFormat () {

if ( this.format == null ) {

// No format so create it

StringBuilder b = new StringBuilder("0.");

for ( int i = 0; i < this.precision; i++ ) {

b.append("0");

}

this.format = new DecimalFormat(b.toString());

}

return this.format;

}

/**

* Serialize a double

*/

@Override

public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider ) throws IOException {

if ( (value == null) || value.isNaN() ) {

jgen.writeNull();

}

else {

DecimalFormat format = getFormat();

jgen.writeRawValue(format.format(value));

}

}

}

我正在使用MixIn,所以该类具有:

public abstract class StationJacksonMixIn {

@JsonCreator

public StationJacksonMixIn () {

}

// Serializers to control formatting

@JsonSerialize(using=JacksonJsonDoubleSerializer.class)

@FormatterPrecision(precision=6) abstract Double getLatitude();

@JsonSerialize(using=JacksonJsonDoubleSerializer.class)

@FormatterPrecision(precision=6) abstract Double getLongitude();

}

最后,在ObjectMapper中启用MixIn:

ObjectMapper objectMapper = new ObjectMapper().

addMixIn(Station.class,StationJacksonMixIn.class);

它可以很好地提供全局应用于数据字段的精度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值