Skip to content

Excluding attributes in Http requests using Gson.

Gson serves as a Java library for converting Java objects to JSON and vice versa. Gson can work with arbitrary Java objects, including pre-existing objects that you do not have source-code of. Because of it’s simple and easy to use interface (Like less annotations used and simple methods, etc.,) people love to use Gson library along with Retrofit and OkHttp for easier http calls in Android.

These libraries are also designed and made for easily processing with REST API’s. Since the REST architecture style is basically intended for transferring of representational states through the http requests( Like GET,POST,PUT) through a client-server interaction, The developers sometimes may try to omit or exclude the attributes in the state representation which are not required for the RESTful communication.

For example, in a purchasing app, the user wants to purchase fewer items. These items are stored in database and data representation is given to client from server through a GET call.Gson converts the data to a Java object and products are displayed in client side. Now the user selects few products. During the selection Java object gets updated according to the selection. These selected products are updated in the database by a POST method call of endpoint.Those data will contain the state of all products including the modified ones.But server wanted only the modified fields not the unmodified things. We need to omit the unwanted fields from the client side itself.

In Android, omitting a variable from a class object during its serialization, is not an easy task. As a hack, we can define another class which holds only the required fields only and copy those variable values from original class object to new class object. But this is not an efficient strategy for the issue.

In Gson library, there is a method which we can make use for solving this problem along with a custom annotation which is provided by Java. First, create an annotation as like in the below code snippet. A retention policy annotation determines at what point annotation should be discarded.

     /* Annotation class */

     public @interface ExcludeThisField {

Now comes the tricky part, we should create a custom strategy class which implements the ExclusionStrategy with overriding two methods. A method named shouldSkipField() will skip the fields that you want and shoudSkipClasses() will skip the classes too. Here, we are giving the custom annotation class that we have defined to the method shoudlSkipField(). So that, all the fields which annotated by our custom annotation will be skipped during conversion of the object to JSON.

 /* Strategy class */
 public class ExcludeUsingAnnotation implements ExclusionStrategy {

  public boolean shouldSkipField(FieldAttributes f) {
  return f.getAnnotation(ExcludeThisField.class) != null;

   public boolean shouldSkipClass(Class<?> clazz) {
   return false;
/* Gson builder class */
new GsonBuilder().setExclusionStrategies(new ExcludeUsingAnnotation()).create();

Finally, we need to add our strategy to Gson builder declaring class.You can set exclusion strategy as well other strategies like this in Gson builder declaration. Add the @ExcludeThisField annotation to the variable or class and run your code. That’s it! Now run you can see in the output console that Gson has excluded the fields that you have defined.

Published inAndroid

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *