1

I'm using jsonschema2pojo, and some of my fields need special serialization/de-serialization. How do I set that up in the json schema?

Here's my schema so far:

"collegeEducation": {
  "javaJsonView": "com.trp.erd.common.util.Views.InvestmentProfessionalListView",
  "type": "array",
  "position": 37,
  "items": {
    "$ref": "#/definitions/CollegeEducation"
  }
},

And need the resulting property to look like this:

@JsonProperty("collegeEducation")
@JsonView(Views.InvestProfessionalView.class)
@JsonSerialize(using = JsonListSerializer.class)
@JsonDeserialize(using = JsonListDeserializer.class)
@Valid
private List<CollegeEducation> collegeEducation = new ArrayList<CollegeEducation>();
CNDyson
  • 1,687
  • 7
  • 28
  • 63

1 Answers1

2

Here is an answer about custom annotations in jsonschema2pojo.

Use field::annotate in order to annotate fields rather than classes.

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);
        }
    }
}
Peretz
  • 21
  • 5