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
- Mapping of Enums
- Mapping of Circular References
- Generics
- Custom Serialization for Simplification (Part 1)
- Changing the Default Serialization with Custom Serialization (Part 2)
- Custom Deserialization Basics
- Custom Instance Creator
- Customizing (De)Serialization via @JsonAdapter
- Custom Deserialization for Calculated Fields
- On-The-Fly-Parsing With Streams
- ProGuard Configuration
What Happens with null
Values?
We're assuming you've read our 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!