Gson Builder — Force Serialization of null Values

Datetime:2016-08-23 03:09:38          Topic:          Share

In theprevious blog post we've shown you the basics of GsonBuilder . The GsonBuilder can be used to customize Gson's behavior during several aspects of the (de)serialization. In this blog post you'll learn how you can force Gson to serialize null values.

Of course, this is not the only post in our Gson series. If you're interested in the other topics, check out our series outline:

Gson Series Overview

  1. Getting Started with Java-JSON Serialization & Deserialization
  2. Mapping of Nested Objects
  3. Mapping of Arrays and Lists of Objects
  4. Mapping of Maps
  5. Mapping of Sets
  6. Mapping of Null Values
  7. Gson Model Annotations — How to Ignore Fields with @Expose
  8. Gson Model Annotations — How to Change the Naming of Fields with @SerializedName
  9. Gson Builder — Basics & Naming Policies
  10. Gson Builder — Force Serialization of null Values
  11. Gson Builder — Exclusion Strategies
  12. Gson Builder — Relax Gson with Lenient
  13. Gson Builder — Special Values of Floats & Doubles
  14. Gson Builder — Model Versioning
  15. Gson Builder — Formatting of Dates & Custom Date/Time Mapping
  16. Gson Builder — Pretty Printing
  17. Gson Builder — HTML Escaping
  18. Gson Advanced — Generics

Serialization of null Values

Do you remember our blog post on mapping of null values ? It demonstrated a bunch of examples how Gson behaves when values are not set (or null ). If you haven't read it, you might get some value out of it. If you're in a hurry: the gist is that properties do not serialize when they have no value set or null as the value.

This behavior of ignoring null properties is generally a good idea to reduce the size of the resulting JSON string. Nevertheless, it's not always for the best. Some APIs require that a field is existent for a request or having a null value for a property actually means something (in other words, the default value would not be null ; we need to explicitly set it to null ).

Gson offers an option to change this default behavior. We'll utilize the GsonBuilder to adapt the serialization of null values. If you're new to the GsonBuilder , read theblog post on the GsonBuilder basics.

We'll re-use the UserSimple model from the previous blog posts.

public class UserSimple {  
    String name;
    String email;
    boolean isDeveloper;
    int age;
}

Now, let's create an instance of that user without an email. We'll pass a null value to as the email:

Gson gson = new Gson();  
UserSimple user = new UserSimple("Norman", null, 26, true);  
String usersJson = gson.toJson(user);

With the default settings, the email property will not show up in the resulting JSON:

{
  "age": 26,
  "isDeveloper": true,
  "name": "Norman"
}

If you require that the email field is part of the JSON, you need to call .serializeNulls() on the GsonBuilder . If you do that, Gson will serialize all properties, even if they're null:

GsonBuilder gsonBuilder = new GsonBuilder();  
gsonBuilder.serializeNulls();  
Gson gson = gsonBuilder.create();

UserSimple user = new UserSimple("Norman", null, 26, true);  
String usersJson = gson.toJson(user);

The usersJson now contains a field for the email property:

{
  "age": 26,
  "email": null,
  "isDeveloper": true,
  "name": "Norman"
}

Outlook

In this blog post, you've learned how to force Gson to serialize properties with null values.

In the next blog post, we'll show you ways to customize which model fields are (de)serialized.

If you've feedback or a question, let us know in the comments or on twitter @futurestud_io .

Make it rock & enjoy coding!

Not enough Gson? Buy our book!

Enjoy JSON (de-)serialization in Java. Learn every detail about Gson's capabilities, configuration & customization.

Including a workbook & a cheat sheet!

Available on Leanpub