我使用FasterXML json-schema generator从我的POJO构建json-schema。 一切顺利,直到我的POJO使用自定义json序列化。在我的情况下,我有一个类型为org.joda.Money的字段,我用相应的joda模块序列化它。 bean像
一样序列化{
"amount": "...",
"currency": "..."
}
但它的架构如下所示:
{
"type" : "object",
"id" : "urn:jsonschema:org:joda:money:Money",
"properties" : {
"amount" : {
"type" : "number"
},
"amountMinorLong" : {
"type" : "integer"
},
"scale" : {
"type" : "integer"
},
"minorPart" : {
"type" : "integer"
},
"positive" : {
"type" : "boolean"
},
"amountMajor" : {
"type" : "number"
},
"amountMinor" : {
"type" : "number"
},
"amountMinorInt" : {
"type" : "integer"
},
"positiveOrZero" : {
"type" : "boolean"
},
"zero" : {
"type" : "boolean"
},
"negative" : {
"type" : "boolean"
},
"amountMajorLong" : {
"type" : "integer"
},
"amountMajorInt" : {
"type" : "integer"
},
"negativeOrZero" : {
"type" : "boolean"
},
"currencyUnit" : {
"type" : "object",
"id" : "urn:jsonschema:org:joda:money:CurrencyUnit",
"properties" : {
"symbol" : {
"type" : "string"
},
"numeric3Code" : {
"type" : "string"
},
"countryCodes" : {
"type" : "array",
"items" : {
"type" : "string"
}
},
"code" : {
"type" : "string"
},
"decimalPlaces" : {
"type" : "integer"
},
"defaultFractionDigits" : {
"type" : "integer"
},
"currencyCode" : {
"type" : "string"
},
"pseudoCurrency" : {
"type" : "boolean"
},
"numericCode" : {
"type" : "integer"
}
}
}
}
}
有没有办法自定义生成的架构?
答案 0 :(得分:0)
我认为jackson-datatype-joda
不支持Joda Money数据类型。
你可以尝试我自己的模块。我不确定它是否适合使用模式生成,但它适用于序列化/反序列化。
public class JodaMoneyModule extends SimpleModule {
@Override
public void setupModule(final SetupContext context) {
final SimpleSerializers serializers = new SimpleSerializers();
serializers.addSerializer(Money.class, new JodaMoneySerializer());
context.addSerializers(serializers);
final SimpleDeserializers deserializers = new SimpleDeserializers();
deserializers.addDeserializer(Money.class, new JodaMoneyDeserializer());
context.addDeserializers(deserializers);
}
private class JodaMoneySerializer extends JsonSerializer<Money> {
@Override
public void serialize(
final Money value,
final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeString(value == null ? null : value.toString());
}
}
private class JodaMoneyDeserializer extends JsonDeserializer<Money> {
@Override
public Money deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException {
final String value = p.getValueAsString();
return value == null ? null : Money.parse(value);
}
}
}