java 设置 Jackson ObjectMapper 类不对 double 使用科学记数法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37722152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:43:51  来源:igfitidea点击:

set Hymanson ObjectMapper class not to use scientific notation for double

javaHymansonjsonschemaHymanson-modules

提问by Abhij

I am using a library com.fasterxml.Hymanson library for JsonSchema, I am creating creating an IntegerSchema object, when I set range for integer schema using below code:

我正在为 JsonSchema 使用库 com.fasterxml.Hymanson 库,当我使用以下代码为整数模式设置范围时,我正在创建一个 IntegerSchema 对象:

main(){
     IntegerSchema intSchema = new IntegerSchema();
     // setMaximum accepts Double object 
     intSchema.setMaximum(new Double(102000000));
     // setMaximum accepts Double object
     intSchema.setMinimum(new Double(100));

     printJsonSchema(intSchema);
}


public void printJsonSchema(JsonSchema schema){
        ObjectMapper mapper = new ObjectMapper();
        try {
            logger.info(mapper.writeValueAsString(schema));
        } catch (JsonProcessingException e) {
            throw new IllegalStateException(e);
        }
}

When I convert IntegerSchema to string using ObjectMapper getting below response:

当我使用 ObjectMapper 将 IntegerSchema 转换为字符串时,得到以下响应:

{"type":"integer","maximum":1.02E8,"minimum":100.0}

maximum and minimum values are getting converted to scientific notation.

最大值和最小值正在转换为科学记数法。

But I need output in non scientific notation as below:

但我需要以非科学记数法输出,如下所示:

{"type":"integer","maximum":102000000,"minimum":100}

I cannot change IntegerSchema class.

我无法更改 IntegerSchema 类。

Please suggest how to get required output without extending IntegerSchema class?

请建议如何在不扩展 IntegerSchema 类的情况下获得所需的输出?

Thanks in advance

提前致谢

回答by pandaadb

this is a java issue somewhat I believe. If you debug your program, your Double will always be displayed scientifically, so what we'll want is to force it into a String. This can be achieved in Java in multiple ways, and you can look it up here:

我相信这是一个java问题。如果您调试程序,您的 Double 将始终科学地显示,因此我们想要的是将其强制为 String。这可以通过多种方式在 Java 中实现,您可以在此处查找:

How to print double value without scientific notation using Java?

如何使用Java在没有科学记数法的情况下打印双值?

In terms of your specific question about Hymanson, I've written up some code for you:

就您关于Hyman逊的具体问题而言,我已经为您编写了一些代码:

public class ObjectMapperTest {

    public static void main(String[] args) throws JsonProcessingException {

        IntegerSchema schema = new IntegerSchema();
        schema.type = "Int";
        schema.max = 10200000000d;
        schema.min = 100d;

        ObjectMapper m = new ObjectMapper();

        System.out.println(m.writeValueAsString(schema));

    }

    public static class IntegerSchema {

        @JsonProperty
        String type;
        @JsonProperty
        double min;
        @JsonProperty
        @JsonSerialize(using=MyDoubleDesirializer.class)
        double max;
    }

    public static class MyDoubleDesirializer extends JsonSerializer<Double> {


        @Override
        public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers)
                throws IOException, JsonProcessingException {
            // TODO Auto-generated method stub

            BigDecimal d = new BigDecimal(value);
            gen.writeNumber(d.toPlainString());
        }

    }

}

The trick is to register a custom Serializer for your Double value. This way, you can control what you want.

诀窍是为您的 Double 值注册一个自定义序列化程序。这样,您就可以控制自己想要的东西。

I am using the BigDecimal value to create a String representation of your Double. The output then becomes (for the specific example):

我正在使用 BigDecimal 值来创建 Double 的字符串表示形式。然后输出变为(对于特定示例):

{"type":"Int","min":100.0,"max":10200000000}

I hope that solves your problem.

我希望这能解决你的问题。

Artur

阿图尔

回答by binh phung

Feature.WRITE_BIGDECIMAL_AS_PLAINset this for your Object Mapper

Feature.WRITE_BIGDECIMAL_AS_PLAIN为您的对象映射器设置此项