Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson: How to exclude specific fields from Serialization without annotations

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 country but 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.

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

Edit: I'm trying to see if it's possible to emulate the behavior of Struts2 JSON plugin

using Gson

<interceptor-ref name="json">   <param name="enableSMD">true</param>   <param name="excludeProperties">     login.password,     studentList.*\.sin   </param> </interceptor-ref> 

Edit: I reopened the question with the following addition:

I added a second field with the same type to futher clarify this problem. Basically I want to exclude country.name but not countrOfBirth.name. I also don't want to exclude Country as a type. So the types are the same it's the actual place in the object graph that I want to pinpoint and exclude.

like image 455
Liviu T. Avatar asked Jan 26 '11 09:01

Liviu T.


People also ask

How do I exclude a field from 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.

How do I ignore fields in Gson?

2.1. By default, Gson would exclude a field from serialization and deserialization – both, if we simply mark the field as transient. Remember that it is not capable of blocking one way transformation. It blocks both. transient will have the same effect as @Expose (serialize = false, deserialize = false) .

Does Gson ignore extra fields?

3. Deserialize JSON With Extra Unknown Fields to Object. As you can see, Gson will ignore the unknown fields and simply match the fields that it's able to.

Does Gson ignore transient fields?

Gson serializer will ignore every field declared as transient: String jsonString = new Gson().


2 Answers

Any fields you don't want serialized in general you should use the "transient" modifier, and this also applies to json serializers (at least it does to a few that I have used, including gson).

If you don't want name to show up in the serialized json give it a transient keyword, eg:

private transient String name; 

More details in the Gson documentation

like image 101
Chris Seline Avatar answered Oct 15 '22 09:10

Chris Seline


Nishant provided a good solution, but there's an easier way. 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(); 
like image 29
JayPea Avatar answered Oct 15 '22 09:10

JayPea