将Jackson ObjectMapper类设置为不使用双重科学记数法 [英] set Jackson ObjectMapper class not to use scientific notation for double
问题描述
我正在为JsonSchema使用库com.fasterxml.jackson库,
我正在创建一个IntegerSchema对象,当我使用下面的代码设置整数模式的范围时:
main(){
IntegerSchema intSchema = new IntegerSchema();
// setMaximum接受Double对象
intSchema.setMaximum(new Double(102000000));
// setMaximum接受Double对象
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){
抛出新的IllegalStateException(e);
}
}
当我使用ObjectMapper将IntegerSchema转换为字符串时得到以下响应:
{type:integer,maximum:1.02E8,minimum:100.0}
最大值和最小值将转换为科学记数法。
<但是我需要输出非科学记数法如下:
{type:integer,maximum: 102000000,最小:100}
我无法更改IntegerSchema类。
请建议如何在不延长IntegerSchema课程的情况下获得所需的输出?
提前致谢
我相信这是一个java问题。如果您调试程序,您的Double将始终以科学方式显示,因此我们需要将其强制为String。这可以通过多种方式在Java中实现,您可以在这里查找:
关于你关于杰克逊的具体问题,我已经为你写了一些代码:
public class ObjectMapperTest {
public static void main(String [] args)抛出JsonProcessingException {
IntegerSchema schema = new IntegerSchema();
schema.type =Int;
schema.max = 10200000000d;
schema.min = 100d;
ObjectMapper m = new ObjectMapper();
System.out.println(m.writeValueAsString(schema));
}
公共静态类IntegerSchema {
@JsonProperty
字符串类型;
@JsonProperty
double min;
@JsonProperty
@JsonSerialize(using = MyDoubleDesirializer.class)
double max;
}
公共静态类MyDoubleDesirializer扩展JsonSerializer< Double> {
@Override
public void serialize(Double value,JsonGenerator gen,SerializerProvider serializers)
抛出IOException,JsonProcessingException {
// TODO Auto-生成方法存根
BigDecimal d = new BigDecimal(value);
gen.writeNumber(d.toPlainString());
}
}
}
诀窍是为Double值注册一个自定义Serializer。这样,您就可以控制自己想要的内容。
我使用BigDecimal值创建Double的String表示。输出然后变为(对于特定示例):
{type:Int,min:100.0, max:10200000000}
我希望能解决您的问题。
Artur
I am using a library com.fasterxml.jackson library for JsonSchema, I am creating 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);
}
}
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}
I cannot change IntegerSchema class.
Please suggest how to get required output without extending IntegerSchema class?
Thanks in advance
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:
How to print double value without scientific notation using Java?
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());
}
}
}
The trick is to register a custom Serializer for your Double value. This way, you can control what you want.
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}
I hope that solves your problem.
Artur
这篇关于将Jackson ObjectMapper类设置为不使用双重科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!