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

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

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,890 points

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,300 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
• 58,130 points
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
• 58,130 points
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
+15 votes
22 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
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
• 58,130 points
0 votes
1 answer

Serializable interface in Java

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

answered Jun 12, 2018 in Java by code.reaper12
• 3,470 points
0 votes
1 answer

What is the use of serialVersionUID ?

The docs for are probably about as good ...READ MORE

answered Jun 21, 2018 in Java by Rishabh
• 3,580 points
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,300 points
0 votes
1 answer

FirebaseListAdapter not pushing individual items for chat app

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

answered Feb 13, 2019 in Java by developer_1
• 3,300 points