将Jackson ObjectMapper类设置为不使用双重科学记数法

Abh*_*hij 8 java jsonschema jackson jackson-modules

我正在为JsonSchema使用库com.fasterxml.jackson库,我正在创建一个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);
        }
}
Run Code Online (Sandbox Code Playgroud)

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

{"type":"integer","maximum":1.02E8,"minimum":100.0}
Run Code Online (Sandbox Code Playgroud)

最大值和最小值将转换为科学计数法.

但是我需要非科学记谱法的输出如下:

{"type":"integer","maximum":102000000,"minimum":100}
Run Code Online (Sandbox Code Playgroud)

我无法更改IntegerSchema类.

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

提前致谢

小智 11

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

  • 顾名思义,这仅适用于 BigDecimal。它不适用于双 (4认同)
  • 像这样: mapper.configure(Feature.WRITE_BIGDECIMAL_AS_PLAIN, true); (3认同)

pan*_*adb 10

我相信这是一个java问题.如果你调试你的程序,你的Double将始终科学地显示,所以我们想要的是强制它成为一个字符串.这可以通过多种方式在Java中实现,您可以在这里查找:

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

关于你关于杰克逊的具体问题,我已经为你写了一些代码:

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());
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

诀窍是为Double值注册自定义Serializer.这样,您就可以控制自己想要的内容.

我使用BigDecimal值来​​创建Double的String表示.然后输出变为(对于特定示例):

{"type":"Int","min":100.0,"max":10200000000}
Run Code Online (Sandbox Code Playgroud)

我希望能解决你的问题.

阿图尔

  • 您还可以注册一个模块,将序列化器应用于整个系统的所有双打.有关如何完成此操作的说明,请参见http://stackoverflow.com/a/25945058/14731. (2认同)

A P*_*aul 10

我知道我回复晚了,但我遇到的一些事情可能会帮助其他人

虽然我在下面遇到的转换 BigDecimal 正在工作

mapper = mapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));
Run Code Online (Sandbox Code Playgroud)

虽然这对我不起作用

mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
Run Code Online (Sandbox Code Playgroud)

  • 我不明白原因,但这也是适用于我的情况的解决方案。在我的例子中,“WRITE_BIGDECIMAL_AS_PLAIN”和“WRITE_NUMBERS_AS_STRINGS”都不起作用,但使用ExactBigDecimals设置节点工厂却起作用了。我正在通过 Jackson 2.11 的“myJsonMapper.valueToTree(myPojo)”执行序列化。 (2认同)

Vol*_*ozo 5

更新 Jakson 2.9.10

属性WRITE_BIGDECIMAL_AS_PLAIN替换为com.fasterxml.jackson.core.JsonGenerator. 你可以使用:

mapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
Run Code Online (Sandbox Code Playgroud)