设置 Jackson ObjectMapper 类不对 double 使用科学记数法 [英] set Jackson ObjectMapper class not to use scientific notation for double
问题描述
我正在为 JsonSchema 使用库 com.fasterxml.jackson 库,我正在创建一个 IntegerSchema 对象,当我使用以下代码为整数模式设置范围时:
I am using a library com.fasterxml.jackson library for JsonSchema, I am creating an IntegerSchema object, when I set range for integer schema using below code:
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);
}
}
当我使用 ObjectMapper 将 IntegerSchema 转换为字符串时,得到以下响应:
When I convert IntegerSchema to string using ObjectMapper getting below response:
{"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}
我无法更改 IntegerSchema 类.
I cannot change IntegerSchema class.
请建议如何在不扩展 IntegerSchema 类的情况下获得所需的输出?
Please suggest how to get the required output without extending IntegerSchema class?
提前致谢
推荐答案
我认为这是一个 Java 问题.如果您调试程序,您的 Double 将始终科学地显示,因此我们想要的是将其强制为 String.这可以通过多种方式在 Java 中实现,您可以在这里查看:
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:
就您关于杰克逊的具体问题而言,我已经为您编写了一些代码:
In terms of your specific question about Jackson, I've written up some code for you:
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());
}
}
}
诀窍是为您的 Double 值注册一个自定义序列化程序.这样,您就可以控制自己想要的东西.
The trick is to register a custom Serializer for your Double value. This way, you can control what you want.
我正在使用 BigDecimal 值来创建 Double 的字符串表示形式.然后输出变为(对于特定示例):
I am using the BigDecimal value to create a String representation of your Double. The output then becomes (for the specific example):
{"type":"Int","min":100.0,"max":10200000000}
希望能解决您的问题.
阿图尔
这篇关于设置 Jackson ObjectMapper 类不对 double 使用科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!