In the previous 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
- 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
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 the blog 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!