Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON Marshall ignore getter

Tags:

json

jackson

Im using Jackson to convert a POJO into a JSON to store in the DB. However I have a getter that I want to ignore. I have see a lot of info relating to @JsonIgnoreProperties but I can't seem to make any progress with it. I basically want the equivalent of @Transient.

Basic usecase (I want to ignore the InternationalNumber):

public class PhoneNumber {  private String country; private String number;  public PhoneNumber() {}  public String getCountry() {     return country; }  public String getLocalNumber() {     return localNumber; }  public String getInternationalNumber() {     String result = "Not Available";     if (country != null && localNumber != null) {     result = new PhoneNumberHandler().internationalFormat(             localNumber, WorldCountries.countryToIso2Code(country));     }     return result; }  } 
like image 468
tarka Avatar asked Nov 23 '12 17:11

tarka


People also ask

How do I ignore a field in JSON response Jackson?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

How do I ignore properties in Jackson?

Ignore All Fields by Type Finally, we can ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly: @JsonIgnoreType public class SomeType { ... } More often than not, however, we don't have control of the class itself.

How do I ignore JSON property?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.


1 Answers

That would be @JsonIgnore on getter method.

like image 178
StaxMan Avatar answered Sep 21 '22 23:09

StaxMan