Gson — Mapping of Null Values

Datetime:2016-08-23 03:10:35          Topic:          Share

In our few Gson blog post, we've introduced to you what Gson is and how it can map various JSON data structures to Java objects and vice versa. Of course, a lot of questions stayed unanswered. In this blog post, we'll answer one of the most interesting ones. If you want to know how Gson handles null values keep reading!

Of course, this will not be 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

What Happens with null Values?

We're assuming you've readour first Gson post, which introduced the UserSimple class and its fields. In our previous demos, all values were always set. What happens if one of them is null ? For example, we're creating a user, which has an email, age and is a developer, but the name is null ?

UserSimple userObject = new UserSimple(null, "norman@futurestud.io", 26, true);  

This is valid Java code, which results in a regular UserSimple object. If we now let Gson create a matching JSON, how is it going to look like?

UserSimple userObject = new UserSimple(null, "norman@futurestud.io", 26, true);

Gson gson = new Gson();  
String userJson = gson.toJson(userObject); // userJson = ??  

Gson is going to create the following JSON:

{
  "age": 26,
  "email": "norman@futurestud.io",
  "isDeveloper": true
}

Gson simply ignores null values during the serialization! If a value is not set, it'll not be part of the resulting JSON at all. If you require that fields with null values also show up in the JSON (with null values), Gson has an option for it. But we'll look at that later.

For now it's important that you understand that during the serialization, Gson does not create JSON values for null s.

The deserialization works very similar. Let's assume we've the following JSON, which misses the email:

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

Even though the email is missing, Gson will still try to map as much as possible. It'll leave the Java object value null for all non-existing fields:

Of course, it's easy to set a field to null if it's a nullable type like String, but what if a non-nullable type is not set? For example, imagine the following JSON:

{
  "email": "norman@futurestud.io",
  "name": "Norman"
}

Both age (as an int type) and isDeveloper (as a boolean type) don't accept null values. In this case, Gson will still not fault and just use their default values ( 0 and false , respectively):

Outlook

In this blog post, you've learned how Gson deals with null values. It's important that you check your models and your business logic for possible errors when values are not set!

In the next blog post, we'll show you some model annotations, which can be used to exclude and include certain fields from (de)serialization.

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