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 in Java by Sushmita
• 6,820 points
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 by developer_1
• 3,220 points

Related Questions In Java

0 votes
1 answer

How do I replace character from string at specific indexes?

String are immutable in Java. You can't ...READ MORE

answered Apr 26, 2018 in Java by Rishabh
• 3,520 points
25 views
0 votes
1 answer

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

I think it is most useful as ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,150 points
44 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
242 views
+14 votes
17 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 in Java by Suresh
• 700 points
52,482 views
0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,590 points
163 views
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,450 points
60 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,520 points
48 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 in Java by developer_1
• 3,220 points
56 views
0 votes
1 answer

FirebaseListAdapter not pushing individual items for chat app

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

answered Feb 13 in Java by developer_1
• 3,220 points
75 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.