微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jsonschema2pojo:如何将注释应用于某些字段

如何解决jsonschema2pojo:如何将注释应用于某些字段

我正在使用 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>();

解决方法

这里是关于 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);
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。