我使用的是jsonschema2pojo,我的一些字段需要特殊的序列化/反序列化。我如何在json模式中设置它?
到目前为止,我的模式如下:
"collegeEducation": {
"javaJsonView": "com.trp.erd.common.util.Views.InvestmentProfessionalListView",
"type": "array",
"position": 37,
"items": {
"$ref": "#/definitions/CollegeEducation"
}
},并且需要如下所示的结果属性:
@JsonProperty("collegeEducation")
@JsonView(Views.InvestProfessionalView.class)
@JsonSerialize(using = JsonListSerializer.class)
@JsonDeserialize(using = JsonListDeserializer.class)
@Valid
private List<CollegeEducation> collegeEducation = new ArrayList<CollegeEducation>();发布于 2021-05-26 17:48:24
下面是关于jsonschema2pojo中的自定义注解的an answer。
使用field::annotate来注释字段而不是类。
public class SerializationAnnotator extends AbstractAnnotator {
@Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
super.propertyField(field, clazz, propertyName, propertyNode);
if (propertyName.equals("collegeEducation")) {
JAnnotationUse annotation;
annotation = field.annotate(JsonSerialize.class);
annotation.param("using", JsonListSerializer.class);
annotation = field.annotate(JsonDeserialize.class);
annotation.param("using", JsonListDeserializer.class);
}
}
}https://stackoverflow.com/questions/66319077
复制相似问题