Only using @JsonIgnore during serialization, but not deserialization
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the world of JSON processing with Java, handling the way your objects are serialized and deserialized can vastly impact the API's flexibility and security. One of the many tools provided by libraries like Jackson is the @JsonIgnore annotation. Typically, @JsonIgnore is used to mark properties of Java objects to be ignored during serialization and deserialization processes. However, there are scenarios when you might want to ignore a field only during serialization but keep it active during deserialization.
Understanding Serialization vs. Deserialization
Before diving into specific usage, let's clarify what we mean by serialization and deserialization:
- Serialization: This is the process of converting a Java object into a JSON format. This is typically used when sending data from a server to a client.
- Deserialization: Conversely, this is the process of parsing JSON into a Java object. This commonly occurs when receiving JSON data from a client at the server.
Use Case for Ignoring Fields Only During Serialization
Imagine you have a Java object that includes sensitive information, such as a user's password. When retrieving user details, you might want to exclude this sensitive information when sending it to the client but include it when receiving it from the client if they need to update their password.
Here's a simple Java class to illustrate:
Using @JsonIgnore and @JsonProperty
To achieve the behavior of ignoring the password only during serialization, you can't merely use @JsonIgnore, as it will omit the field in both serialization and deserialization. Instead, you need to work with a combination of Jackson annotations like @JsonIgnore and @JsonProperty.
Here's how you can modify the User class:
In the modified version, getPassword() always returns null, which means the password field will not appear in the serialized JSON, thereby protecting sensitive data. However, setPassword() is annotated with @JsonProperty, which allows the password to be set during deserialization.
Technical Explanation
The Jackson library processes annotations to determine whether to include properties in serialization and deserialization. The absence of @JsonProperty on the getPassword() method coupled with its return value of null effectively excludes the field from serialization outputs. On the other hand, the presence of @JsonProperty on the setPassword() method allows JSON containing a password attribute to be properly deserialized into the Java object.
Summary Table
Here is a quick reference table summarizing the effects of different configurations:
| Annotation on Getter | Annotation on Setter | Serialized JSON | Deserialized Object Supports |
| None | None | Includes field | Yes |
@JsonIgnore | None | Excludes field | No |
| None | @JsonIgnore | Includes field | No |
@JsonIgnore | @JsonProperty | Excludes field | Yes |
Final Thoughts
Using @JsonIgnore only during serialization but enabling deserialization requires careful handling using the available Jackson annotations. It's important for developers to be aware of these nuances to control data exposure adequately and ensure the integrity and security of data within their applications. This approach can be particularly crucial in microservices architecture, where various services may interact with sensitive or personal user data.
Related reading
- OpenJDK availability for Windows OS
- Operator overloading in Java
- Optimal JVM settings for Cassandra
- Optimal settings for Cassandra Java driver to write to the local data centre only
- Optimisation of recursive algorithm in Java
- Optional environment variables in Spring app
- Optional.get without isPresent check
- Options or alternatives to Testcontainers for Spring Boot integration testing in Kubernetes?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.