2

I have below json schema and generated java class using jsonschema2pojo library

AddressSchema.json

{
"$id": "https://example.com/address.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
"address": {
  "type": "string"
 }
}

AddressSchema.java

public class AddressSchema {

 private String address;

 @JsonProperty("address")
 public String getAddress() {
    return address;
 }

 @JsonProperty("address")
 public void setAddress(String address) {
    this.address = address;
 }
}

My requirement is to generate class with different values in @JsonProperty on setter and getter like below. Is there any way to achieve this behavior?

public class AddressSchema {

 private String address;

 @JsonProperty("address")
 public String getAddress() {
    return address;
 }

 @JsonProperty("addr") //different value in the setter
 public void setAddress(String address) {
    this.address = address;
 }
}
Nagendra
  • 191
  • 1
  • 3
  • 20

1 Answers1

0

I believe you are trying to use the same class to parse a certain json with different name addr and have it return with different name address. I don't see how it might be possible without using two classes and a mapper to map the values. As humans we'd think address and addr are pretty similar and there must be mechanisms to map them while address and name are totally different I wouldn't ask for them to be mapped. But for computers it would be a difficult feature to provide. Hope you get my point.

Bibek Khadka
  • 180
  • 1
  • 17
  • serialization and deserialization with different names from same model is possible using @JsonProperty with different values on getter and setter. My question was about to generate model class from the json schema! – Nagendra Feb 07 '20 at 03:20