1. Overview

While working with Gson to serialize fields at that time by default Gson will ignore null fields or we can say if null fields available in Java object then Gson will not serialized it or will not convert in Json. So this article is about to how to include null fields in serialization while working with Gson.

If you are new for spring or spring boot application development then i like to clear some points here, Gson and Jackson both are different library for Java Object to Json conversation. If you are looking for Jackson related configurations then more details are available here. By default spring boot will use Jackson configurations, If you looking for configuration of Gson then here is details to configure Gson in spring application.

2. Ways to configure serialize null fields in Gson

We have two ways include null fields in Gson: first, If we are working with spring boot application then we can use property called spring.gson.serialize-nulls and second, Creating bean of GsonBuilder and called serializeNulls() method.

2.1 Serialize null fields in Gson using application.properties

Add spring.gson.serialize-nulls property  in application.properties or application.yml file with any value.

NOTE: spring.gson.serialize-nulls with any value will work, It’s not like false or true. If you want to ignore this property then remove it from application.properties file. FALSE will not work.

If you are new for spring boot application and not aware of application.properties or yml file then here is well explain article related to application.properties or yml file.

application.properties

spring.gson.serialize-nulls=true

application.yml

spring:
  gson:
    serialize-nulls: true

2.2 Serialize null fields in Gson using GsonBuilder

We can also customize Gson properties using creating bean of GsonBuilder. When spring application will start and if find bean of GsonBuilder then it will use that bean to  Serialize and De-serialize java objects to Json. So here we have create bean of GsonBuilder and called serializeNulls() method so now any fields with null value also be serialized to Json.

@Bean
GsonBuilder gsonBuilder(){
    GsonBuilder gsonBuilder=new GsonBuilder();
    gsonBuilder.serializeNulls();
    return gsonBuilder;
}

3. Conclusion

In this article, We have discussed different ways to include null fields for serializing or converting Java Objects to Json String. Gson widely used library for Json conversation in Java also provide customization  as per user requirements, Here we have another article for fields naming conversation strategy.

Here are important properties related to Gson configuration in spring boot:

spring.gson.date-format= # Format to use when serializing Date objects.
spring.gson.disable-html-escaping= # Whether to disable the escaping of HTML characters such as '<', '>', etc.
spring.gson.disable-inner-class-serialization= # Whether to exclude inner classes during serialization.
spring.gson.enable-complex-map-key-serialization= # Whether to enable serialization of complex map keys (i.e. non-primitives).
spring.gson.exclude-fields-without-expose-annotation= # Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation.
spring.gson.field-naming-policy= # Naming policy that should be applied to an object's field during serialization and deserialization.
spring.gson.generate-non-executable-json= # Whether to generate non executable JSON by prefixing the output with some special text.
spring.gson.lenient= # Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.
spring.gson.long-serialization-policy= # Serialization policy for Long and long types.
spring.gson.pretty-printing= # Whether to output serialized JSON that fits in a page for pretty printing.
spring.gson.serialize-nulls= # Whether to serialize null fields.

4. References

5. Source Code

 

 

 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *