Monday 16 December 2019

java - How to serialize only fields with jackson

How can I tell jackson to only serialize fields (and not "methods") in a way that I don't need to add @JsonIgnore to all kinds of public methods in all my classes that are irrelevant to serialization? For example in the following class I would expect only the 2 fields: phone, name to be serialized, and not the isEmpty, isValid methods. (which is I think the default in GSon)



@JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact {
@JsonProperty("p")
private String phone;
@JsonProperty("n")

private String name;

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@JsonIgnore
public boolean isValid() {

return !StringHelper.isEmpty(phone);
}

@JsonIgnore
public boolean isEmpty() {
return StringHelper.isEmpty(phone) && StringHelper.isEmpty(name);
}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...