Annyce Davis

Helping you write beautiful code

  • Home
  • Public Speaking
  • About Me
  • Courses
  • Life

Android: Custom Gson Deserialization

October 28, 2014 by Annyce Davis Leave a Comment

So here’s the scenario you have a JSON Object that contains a list of objects. Each one of the objects in the primary JSON Object also contain a list of objects. So for demonstration purposes we will use a car with a list of seats and each seat has a list of buckles (Car -> Seats -> Buckles). Completely random example, yes I know.

So if you have the use case where you don’t want to deserialize any seats that don’t have buckles this is how you can do it using a Custom GSON Deserializer.

First, this is a sample JSON response which contains the information for each car:

[{
    "description": "A nice car",
    "name": "Toyota",
    "seats": [{
        "buckles": [],
        "type": "front"
    }, {
        "buckles": ["latch", "snap"],
        "type": "front"
    }]
}]

What you will notice is that there is one Seat object that has an empty list of Buckle objects. So in that case you don’t want Gson to include that seat in the final list of seat objects.  We will use two advanced features of Gson: Exclusion Strategies and Custom Deserializer. 

Since we want to handle the “seats” array on our own we will create an Annotation that can be used by Gson to ignore the field during normal processing.

Car Class (portion):

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface SkipThis {

}

@SkipThis
private List<SeatVO> seats;

Now we can proceed to exclude any field that has our custom SkipThis annotation by means of an ExclusionStrategy.

private static class SkipThisAnnotationExclusionStrategy
implements ExclusionStrategy {
    public boolean shouldSkipClass(Class clazz) {
        return clazz.getAnnotation(CarVO.SkipThis.class) != null;
    }
 
    public boolean shouldSkipField(FieldAttributes f) {
        return f.getAnnotation(CarVO.SkipThis.class) != null;
    }
}

Finally, we can proceed to create the custom deserializer that will check for the existence of items in the “buckles” array.

@Override
public CarVO deserialize(JsonElement json, Type arg1,
                         JsonDeserializationContext arg2) throws JsonParseException {
  JsonObject carsJSON = json.getAsJsonObject();
  JsonArray seatsJSON = carsJSON.getAsJsonArray("seats");
 
  Gson g = new Gson();
  CarVO carVO = g.fromJson(json, CarVO.class);
  List<SeatVO> res = new ArrayList<SeatVO>();
 
  for (JsonElement elem : seatsJSON) {
    JsonArray bucklesJSON = elem.getAsJsonObject().getAsJsonArray("buckles");
    if (!bucklesJSON.isJsonNull() && bucklesJSON.size() > 0) {
      res.add(g.fromJson(elem, SeatVO.class));
    }
  }
 
  carVO.setSeats(res);
 
  return carVO;
}

The complete code:

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: Android Tagged With: Annotations, Gson, JSON

Newsletter

Start a new tech group on Meetup

Follow Me

Dev Machine Setup – Mobile Development

A new machine. Now what? Time to set it all up. Inevitably I end up going back and forth from my old machine or worse yet, my memory, to figure out what needs … [Read More...]

Dagger Multibinding in Dynamic Feature Modules

We're adding the use of dynamic feature modules to our app. This will help us to improve build speeds, conditionally include features and decouple our code. As … [Read More...]

Categories

  • Android (49)
  • Career (2)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • Java (8)
  • JavaScript (6)
  • Kotlin (11)
  • Life (4)
  • Public Speaking (22)
  • RxJava (1)
  • Software Development (3)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (4)

Copyright © 2021 · Beautiful Pro Theme on Genesis Framework · WordPress · Log in