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 为您的对象映射器设置此项
pan*_*adb 10
我相信这是一个java问题.如果你调试你的程序,你的Double将始终科学地显示,所以我们想要的是强制它成为一个字符串.这可以通过多种方式在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)
我希望能解决你的问题.
阿图尔
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)
更新 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)
| 归档时间: |
|
| 查看次数: |
8741 次 |
| 最近记录: |