The default behaviour that is implemented in Gson is that null object fields are ignored. This allows for a more compact output format; however, the client must define a default value for these fields as the JSON format is converted back into its Java.
Here’s how you would configure a Gson instance to output null:
Gson gson = new GsonBuilder().serializeNulls().create();
NOTE: when serializing nulls with Gson, it will add a JsonNull element to the JsonElement structure. Therefore, this object can be used in custom serialization/deserialization.
Here’s an example:
public class Foo { private final String s; private final int i; public Foo() { this(null, 5); } public Foo(String s, int i) { this.s = s; this.i = i; } } Gson gson = new GsonBuilder().serializeNulls().create(); Foo foo = new Foo(); String json = gson.toJson(foo); System.out.println(json); json = gson.toJson(null); System.out.println(json); ======== OUTPUT ======== {"s":null,"i":5} null
Ref: https://sites.google.com/site/gson/gson-user-guide#TOC-Null-Object-Support