【问题标题】:jsonschema2pojo: How to apply annotations to certain fieldsjsonschema2pojo:如何将注释应用于某些字段
【发布时间】:2021-05-24 21:28:21
【问题描述】:

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

【问题讨论】:

    标签: java json jsonschema2pojo


    【解决方案1】:

    这里是 an answer 关于 jsonschema2pojo 中的自定义注释。

    使用 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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2021-05-09
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-08-31
      • 2015-04-25
      相关资源
      最近更新 更多