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

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

问题描述

有许多关于为数字,货币等创建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.

任何想法都会受到赞赏。

Any ideas would be appreciated.

推荐答案

你可以使用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;

}

希望这会有所帮助!!

Hope this helps!!

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

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