Double 需要 Jackson 序列化程序,并且需要在运行时指定精度 [英] Need Jackson serializer for Double and need to specify precision at runtime

查看:80
本文介绍了Double 需要 Jackson 序列化程序,并且需要在运行时指定精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于为数字、货币等创建 Jackson 序列化程序的帖子.对于工程应用,通常需要根据单位或其他标准设置数字的精度.

There are many posts about creating Jackson serializers for numbers, currency, etc. For engineering applications, there is often a need to set the precision on numbers based on the units or other criteria.

例如,空间坐标可能被限制在小数点后 5 或 6 位,温度可能被限制在小数点后 2 位.具有太多数字或截断指数符号的默认序列化器行为不好.我需要的是这样的:

For example, spatial coordinates might be constrained to 5 or 6 digits after the decimal point, and temperature might be constrained to 2 digits after the decimal point. Default serializer behavior that has too many digits or truncated exponential notation is not good. What I need is something like:

@JsonSerialize(using=MyDoubleSerializer.class, precision=6) double myValue;

更好的是能够在运行时指定精度.我也在使用 MixIn.我可以为每个类编写一个序列化程序,但希望指定特定的值.

and better yet be able to specify the precision at run-time. I am also using a MixIn. I could write a serializer for each class but hoped to specify on specific values.

任何想法将不胜感激.

推荐答案

您可以使用 Jackson 的 ContextualSerializer 来实现所需的序列化,如下所示.

You may use Jackson's ContextualSerializer to achieve desired serialization as shown below.

首先,创建一个注解来捕获精度

Firstly, create an annotation to capture precision

@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Precision {
    int precision();
}

接下来,为 Double 类型创建一个上下文序列化器,它在要序列化的字段上查找 Precision 注释,然后为指定的精度创建一个新的序列化器.>

Next, create a contextual serializer for Double type which looks for Precision annotation on the field to be serialized and then create a new serializer for the specified precision.

public class DoubleContextualSerializer extends JsonSerializer<Double> implements ContextualSerializer {

    private int precision = 0;

    public DoubleContextualSerializer (int precision) {
        this.precision = precision;
    }

    public DoubleContextualSerializer () {

    }

    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        if (precision == 0) {
            gen.writeNumber(value.doubleValue());
        } else {
            BigDecimal bd = new BigDecimal(value);
            bd = bd.setScale(precision, RoundingMode.HALF_UP);
            gen.writeNumber(bd.doubleValue());
        }

    }
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        Precision precision = property.getAnnotation(Precision.class);
        if (precision != null) {
            return new DoubleContextualSerializer (precision.precision());
        }
        return this;
    }
}

最后,注释您的字段以使用自定义序列化程序并设置精度

Finally, annotate your field to use custom serializer and set precision

public class Bean{

   @JsonSerialize(using = DoubleContextualSerializer .class)
   @Precision(precision = 2)
   private double doubleNumber;

}

希望这有帮助!!

这篇关于Double 需要 Jackson 序列化程序,并且需要在运行时指定精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆