Using Google GSON : Extra Goodies : Part II

We continue with Using GSON from last article, also in case you missed the first article in series here is link. So, here we go with another installment in series.

Versioning Support

If you want to maintain multiple versions of an object for JSON conversion, Google GSON library has nice @Since annotation for it. This annotation can be applied to fields and classes.

For example, suppose you are maintaining the multiple versions of the your REST API and you are using JSON as end response payload. In next iteration of your API you are added some of fields for particular JSON entity and you don’t want to send the newly added fields to previous API version then @Since annotation comes into picture. Lets see in following listing, how we use this feature.

To make use of this annotation, you must configure the Gson instance to specific version using GsonBuilder, of course.

Output of above as

If you are not specifying any version it will include the all the fields regardless of its version.

Disable HTML Escaping

By default, during conversion any html characters contained, will be converted to their corresponding Unicode i.e. <  to \u003c, > to \u003e , & to \u0026and so on

For passing the html characters as is, you need to configure the gson instance to with use of GsonBuilder#disableHtmlEscaping() method.

Following listing shows the usage -

Output  as follows

Exclude the fields from Json Output

For this, Google GSON has 4 ways to deal,

  1. fields with transient and static modifier

  2. exclude with fields with specific modifiers

  3. Using Expose annotation

  4. User defined exclusion strategies

Lets see each of this in detail-

1. fields with transient and static modifier

This is default behavior, where you modify the field with transient  modifier it will not included as we saw in first article in series and of course, one with static modifier are also excluded as they are part of class and not the instance.

2. exclude fields with specific modifiers

You can configure gson instance, such that it will exclude the fields with modifiers you specified, e.g. you can exclude/ignore the fields which have protected or private modifier.

To leverage this you need to use GsonBuilder#excludeFieldsWithModifiers()  as shown in following series of listing.

For this example, we are not including the private fields. Although you can exclude fields with any modifiers (any modifiers that applies to fields)

In following output, you can see private fields are excluded as per our program, Although you can ignore fields which can have protected, synchronized etc.

3. Using Expose annotation

Gson also provides an annotation which you can mark for the fields, thus those fields will be excluded from serialized output..

For using this we need to following two

  1. configure gson instance as follows
    Gson gson= GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()

  2. and mark the fields with @Expose annotations, which are to be included in final json output. So, one which are not marked will be excluded

Following listing shows the detailed usage

Ouput as follows-

4. User defined exclusion strategy

Google Gson library provides very granular level of exclusions of fields based on type of field and field attributes.

For this you need to implement the ExclusionStrategy interface by implementing the two of its methods such as shouldSkipClass() and shouldSkipField(). Former can implemented such that it will skip fields based on type of it and later can implemented based on field attribute such its modifier, its annotation etc

and now after implementation ExclusionStrategy interface, you need to pass it to GsonBuilder#setExclusionStrategies() method for configuring the gson instance.

Following is minimal implementation of use of Exclusion strategy, in which we exclude the fields are having type as String, List and also field which are having annotation as Deprecated

Note the following implementation of ExclusionStrategy

Our custom Developer class for showing example

Output of above as follows

Custom field naming using field naming policy

As we saw in last article that Gson defaulted to include the output json field name same as that of class field name and also we can override this with use annotation @serilizedName.

In Gson there is more we can define the json field name to change to Uppercase, lowercase etc without use of any annotation.

For this you need to use GsonBuilder().setFieldNamingPolicy() method and passing the appropriate FieldNamePolicy

Following listing shows usage

About these ads

One thought on “Using Google GSON : Extra Goodies : Part II

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s