Use annotation for excluding specific fields from serialization

0 votes

I'm trying to learn Gson and I'm struggling with field exclusion. Here are my classes

public class Student {    
  private Long                id;
  private String              firstName        = "Philip";
  private String              middleName       = "J.";
  private String              initials         = "P.F";
  private String              lastName         = "Fry";
  private Country             country;
  private Country             countryOfBirth;
}

public class Country {    
  private Long                id;
  private String              name;
  private Object              other;
}

I can use the GsonBuilder and add an ExclusionStrategy for a field name like firstName or countrybut I can't seem to manage to exclude properties of certain fields like country.name.

Using the method public boolean shouldSkipField(FieldAttributes fa), FieldAttributes doesn't contain enough information to match the field with a filter like country.name.

I would appreciate any help with a solution for this problem.

P.S: I want to avoid annotations since I want to improve on this and use RegEx to filter fields out.

Thank you

Feb 1, 2019 in Java by Sushmita
• 6,920 points
1,767 views

1 answer to this question.

0 votes

Simply mark the desired fields with the @Expose annotation, such as:

@Expose private Long id;

Leave out any fields that you do not want to serialize. Then just create your Gson object this way:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
answered Feb 1, 2019 by developer_1
• 3,350 points

Related Questions In Java

0 votes
2 answers

How do I replace character from string at specific indexes?

You could turn the String into a ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
23,774 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,471 views
0 votes
1 answer

To use JavaMail API for sending mails in Android without using the default/built-in App

ADD 3 jars found in the following ...READ MORE

answered Jun 6, 2018 in Java by sridhar
• 160 points
1,456 views
0 votes
1 answer
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,619 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,260 views
0 votes
1 answer

Serializable interface in Java

Serializable is a marker interface that has ...READ MORE

answered Jun 13, 2018 in Java by code.reaper12
• 3,500 points
796 views
0 votes
1 answer

What is the use of serialVersionUID ?

The docs for java.io.Serializable are probably about as good ...READ MORE

answered Jun 21, 2018 in Java by Rishabh
• 3,620 points
1,961 views
0 votes
1 answer

Method for escaping HTML in Java

242 StringEscapeUtils from Apache Commons Lang: import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; // ... String source ...READ MORE

answered Jan 9, 2019 in Java by developer_1
• 3,350 points
4,412 views
0 votes
1 answer

FirebaseListAdapter not pushing individual items for chat app

o let the FirebaseRecyclerAdapter and FirebaseListAdapter show ...READ MORE

answered Feb 14, 2019 in Java by developer_1
• 3,350 points
2,177 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP