package tests.models; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(Include.NON_EMPTY) public class Address { @JsonProperty("street") private String street; @JsonProperty("city") private String city; @JsonProperty("state") private String state; @JsonProperty("zipcode") private String zipcode; public Address() { } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public String toJson(boolean pretty) { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); String toret = ""; if (pretty) { mapper.enable(SerializationFeature.INDENT_OUTPUT); } try { toret = mapper.writeValueAsString(this); } catch (JsonProcessingException e) { e.printStackTrace(); } return toret; } }