首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jsonschema2pojo:如何将注释应用于某些字段

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

Stack Overflow用户
提问于 2021-02-23 00:00:08
回答 1查看 148关注 0票数 0

我使用的是jsonschema2pojo,我的一些字段需要特殊的序列化/反序列化。我如何在json模式中设置它?

到目前为止,我的模式如下:

代码语言:javascript
复制
"collegeEducation": {
  "javaJsonView": "com.trp.erd.common.util.Views.InvestmentProfessionalListView",
  "type": "array",
  "position": 37,
  "items": {
    "$ref": "#/definitions/CollegeEducation"
  }
},

并且需要如下所示的结果属性:

代码语言:javascript
复制
@JsonProperty("collegeEducation")
@JsonView(Views.InvestProfessionalView.class)
@JsonSerialize(using = JsonListSerializer.class)
@JsonDeserialize(using = JsonListDeserializer.class)
@Valid
private List<CollegeEducation> collegeEducation = new ArrayList<CollegeEducation>();
EN

回答 1

Stack Overflow用户

发布于 2021-05-26 17:48:24

下面是关于jsonschema2pojo中的自定义注解的an answer

使用field::annotate来注释字段而不是类。

代码语言:javascript
复制
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);
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66319077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档