Jackson: using @JsonIgnore and @JsonProperty annotations to exclude a property only from JSON deserialization (2024)

In the previous article we saw how and where to use the Jackson @JsonIgnore annotation to exclude a property of an Java object from the JSON serialization. Not being available, the value of that property, during deserialization will be set to the default value in accordance with its type. In this case, therefore, the process is symmetrical, in the sense that the property value is ignored by both serialization and deserialization operations.
If we want to make it asymmetrical, that is, to exclude a Java object property only during deserialization, but instead to include its value during JSON serialization we must use an appropriate combination of @JsonIgnore and @JsonProperty annotations. To be precise, we must:

  • annotate with @JsonIgnore the property itself
  • annotate with @JsonIgnore its set method
  • annotate with @JsonProperty its get method

We take the example already used in the previous post and see how to configure MyTestClass class so that the field “forgetThisField” is serialized in JSON but its value is then ignored during deserialization.

import java.io.IOException;import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;class MyTestClass { private long id; private String name; private String notInterstingMember; private int anotherMember; @JsonIgnore private int forgetThisField; public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @JsonIgnore public String getNotInterstingMember() { return this.notInterstingMember; } public void setNotInterstingMember(String notInterstingMember) { this.notInterstingMember = notInterstingMember; } public int getAnotherMember() { return this.anotherMember; } public void setAnotherMember(int anotherMember) { this.anotherMember = anotherMember; } @JsonProperty public int getForgetThisField() { return this.forgetThisField; } @JsonIgnore public void setForgetThisField(int forgetThisField) { this.forgetThisField = forgetThisField; } @Override public String toString() { return "MyTestClass [" + this.id + " , " + this.name + ", " + this.notInterstingMember + ", " + this.anotherMember + ", " + this.forgetThisField + "]"; }}public class JSONIgnorePropTest { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); MyTestClass mtc = new MyTestClass(); mtc.setId(1); mtc.setName("Test program"); mtc.setNotInterstingMember("Don't care about this"); mtc.setAnotherMember(100); mtc.setForgetThisField(999); String s = null; try { s = mapper.writeValueAsString(mtc); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(s); MyTestClass mtc2 = null; try { mtc2 = mapper.readValue(s, MyTestClass.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(mtc2.toString()); }}

As we saw earlier, it’s necessary to:

  • annotate with @JsonIgnore the “forgetThisField” property
 @JsonIgnore private int forgetThisField; 
  • annotate with @JsonIgnore its set method “setForgetThisField”
  •  @JsonIgnore public void setForgetThisField (int forgetThisField) { this.forgetThisField = forgetThisField; } 
  • annotate with @JsonProperty its get method, “getForgetThisField”
  •  @JsonProperty public int getForgetThisField () { this.forgetThisField return; } 

    Running the test program we get the following result:

     {"Id": 1, "name": "Test program", "anotherMember": 100, "forgetThisField": 999} MyTestClass [1, Test program, null, 100, 0] 

    As we can see, in the serialization the “forgetThisField” property is included with its value 999, while in the new object instantiated deserializing the string, this property is ignored and its value gets the default value of its type, that is, 0 being an int.

    The important thing to consider is that the annotations must be all exactly as explained. If we omits some of those listed, we do not obtain the desired result. If, for example, we only put the @JsonIgnore annotation on the set method and the @JsonProperty annotation on the get method, but we do not put @JsonIgnore on the property, this is not ignored during deserialization:

     class MyTestClass { private long id; private String name; private String notInterstingMember; private int anotherMember; //@JsonIgnore // COMMENTED private int forgetThisField; public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @JsonIgnore public String getNotInterstingMember() { return this.notInterstingMember; } public void setNotInterstingMember(String notInterstingMember) { this.notInterstingMember = notInterstingMember; } public int getAnotherMember() { return this.anotherMember; } public void setAnotherMember(int anotherMember) { this.anotherMember = anotherMember; } @JsonProperty public int getForgetThisField() { return this.forgetThisField; } @JsonIgnore public void setForgetThisField(int forgetThisField) { this.forgetThisField = forgetThisField; } @Override public String toString() { return "MyTestClass [" + this.id + " , " + this.name + ", " + this.notInterstingMember + ", " + this.anotherMember + ", " + this.forgetThisField + "]"; }}

    By running the program again we get:

     {"Id": 1, "name": "Test program", "anotherMember": 100, "forgetThisField": 999} MyTestClass [1, Test program, null, 100, 999] 

    As we can see this time the property has not been ignored in the process of de-serialization and the serialized value is then set to the new object.

    Similarly, if we put the @JsonIgnore annotation on the property and the @JsonProperty annotation on the get method, but we do not put @JsonIgnore on the set method, once again the indication to ignore the property is not applied to the deserialization:

    class MyTestClass { private long id; private String name; private String notInterstingMember; private int anotherMember; @JsonIgnore private int forgetThisField; public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @JsonIgnore public String getNotInterstingMember() { return this.notInterstingMember; } public void setNotInterstingMember(String notInterstingMember) { this.notInterstingMember = notInterstingMember; } public int getAnotherMember() { return this.anotherMember; } public void setAnotherMember(int anotherMember) { this.anotherMember = anotherMember; } @JsonProperty public int getForgetThisField() { return this.forgetThisField; } // @JsonIgnore// COMMENTED public void setForgetThisField(int forgetThisField) { this.forgetThisField = forgetThisField; } @Override public String toString() { return "MyTestClass [" + this.id + " , " + this.name + ", " + this.notInterstingMember + ", " + this.anotherMember + ", " + this.forgetThisField + "]"; }}
     {"Id": 1, "name": "Test program", "anotherMember": 100, "forgetThisField": 999} MyTestClass [1, Test program, null, 100, 999] 
    Jackson: using @JsonIgnore and @JsonProperty annotations to exclude a property only from JSON deserialization (2024)
    Top Articles
    The Top 10 Actors and Characters of Heartland (2007) (CA) (Ranked Cast in 2021) | Series with Sophie
    Julia Maren Baker – Age, Bio, Personal Life, Family & Stats - CelebsAges
    7 C's of Communication | The Effective Communication Checklist
    Calvert Er Wait Time
    Where are the Best Boxing Gyms in the UK? - JD Sports
    Gore Videos Uncensored
    Gw2 Legendary Amulet
    AB Solutions Portal | Login
    Lichtsignale | Spur H0 | Sortiment | Viessmann Modelltechnik GmbH
    Student Rating Of Teaching Umn
    Transfer Credits Uncc
    Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
    Willam Belli's Husband
    Sadie Proposal Ideas
    Dallas Craigslist Org Dallas
    Walgreens Alma School And Dynamite
    Samantha Aufderheide
    Evil Dead Rise Showtimes Near Pelican Cinemas
    Highmark Wholecare Otc Store
    Like Some Annoyed Drivers Wsj Crossword
    The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
    Reviews over Supersaver - Opiness - Spreekt uit ervaring
    When Does Subway Open And Close
    Papa Johns Mear Me
    Vera Bradley Factory Outlet Sunbury Products
    Section 408 Allegiant Stadium
    Cosas Aesthetic Para Decorar Tu Cuarto Para Imprimir
    Himekishi Ga Classmate Raw
    Past Weather by Zip Code - Data Table
    134 Paige St. Owego Ny
    Tire Pro Candler
    Was heißt AMK? » Bedeutung und Herkunft des Ausdrucks
    Mobile Maher Terminal
    47 Orchid Varieties: Different Types of Orchids (With Pictures)
    Panchang 2022 Usa
    Japanese Pokémon Cards vs English Pokémon Cards
    Golden Tickets
    Gerber Federal Credit
    The Legacy 3: The Tree of Might – Walkthrough
    Cross-Border Share Swaps Made Easier Through Amendments to India’s Foreign Exchange Regulations - Transatlantic Law International
    Pillowtalk Podcast Interview Turns Into 3Some
    Mistress Elizabeth Nyc
    Reese Witherspoon Wiki
    The power of the NFL, its data, and the shift to CTV
    Citroen | Skąd pobrać program do lexia diagbox?
    The Great Brian Last
    Hello – Cornerstone Chapel
    Premiumbukkake Tour
    Diablo Spawns Blox Fruits
    Tamilblasters.wu
    One Facing Life Maybe Crossword
    Latest Posts
    Article information

    Author: Msgr. Benton Quitzon

    Last Updated:

    Views: 5789

    Rating: 4.2 / 5 (43 voted)

    Reviews: 90% of readers found this page helpful

    Author information

    Name: Msgr. Benton Quitzon

    Birthday: 2001-08-13

    Address: 96487 Kris Cliff, Teresiafurt, WI 95201

    Phone: +9418513585781

    Job: Senior Designer

    Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

    Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.