Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After switching a client from Gson to Jackson, a downstream service fails because fields that were absent are now sent as null, and a field computed by a getter appears in the JSON. Explain the behaviour difference and fix it.

Gson and Jackson decide what goes into the JSON in different ways. Gson reads the object's fields directly and leaves out fields that are null, while Jackson discovers properties through public getters (and public fields) and writes null values unless told not to. We fix the change by configuring Jackson to skip nulls, ignoring computed getters with @JsonIgnore or switching Jackson to field-only visibility, and we protect the next migration with contract tests on the exact JSON shape.

Why this question is asked

Swapping one JSON library for another looks like a dependency change, but it is really a change to the wire format of every message the client sends. Downstream services often treat a missing field and a field set to null as two different things, so a small difference in defaults can break them. Interviewers ask this question to see whether we know how each library discovers properties, rather than only how to call toJson(). In this answer, we will recap how Gson and Jackson find properties, reproduce the two differences with a small class, explain why the downstream service breaks, and then go through the fixes and the tests that would have caught it.

1A Quick Recap of How Each Library Finds Properties

Serialization means turning a Java object into text, here JSON. Every JSON library has to answer one question first: which parts of the object become JSON properties? Gson and Jackson give different answers.

Gson looks at the fields of the class through reflection, the Java feature that lets code inspect a class at runtime. Every non-transient, non-static field becomes a property, whether it is private or public, and Gson never calls a getter. By default, Gson also skips any field whose value is null, so a null field simply does not appear in the output. We have to call serializeNulls() on the GsonBuilder to get null values written.

Jackson works from the other side. It builds a list of properties from the class's public getters, such as getName(), plus any public fields, and it uses setters or constructors when reading. Private fields without a getter are invisible to it by default. And Jackson writes every property it finds, including those whose value is null, unless we configure an inclusion rule.

2Reproducing Both Differences

A small class shows both effects at once. It has a nullable discount field and a getTotal() method that computes a value without any field behind it.

Java
public class OrderLine {
private String sku = "A-1";
private int quantity = 2;
private BigDecimal price = new BigDecimal("5.00");
private BigDecimal discount = null; // not set for this line
public String getSku() { return sku; }
public int getQuantity() { return quantity; }
public BigDecimal getPrice() { return price; }
public BigDecimal getDiscount() { return discount; }
public BigDecimal getTotal() { // computed, no field
return price.multiply(BigDecimal.valueOf(quantity));
}
}
new Gson().toJson(new OrderLine());
// {"sku":"A-1","quantity":2,"price":5.00}
new ObjectMapper().writeValueAsString(new OrderLine());
// {"sku":"A-1","quantity":2,"price":5.00,"discount":null,"total":10.00}
What each library does by default
BehaviourGsonJackson
Finds properties fromfields, any visibilitypublic getters and public fields
Null valuesomittedwritten as null
Getter without a fieldignoredbecomes a property
Unknown fields when readingignorederror, unless disabled (Spring Boot disables it)
The first two rows explain the bug in the question.

Notice the two extra properties in the Jackson output. discount appears as null because Jackson includes nulls by default, and total appears because Jackson saw a public getter named getTotal() and treated it as a property called total. Gson never saw total, because there is no field called total, and it dropped discount because the value was null.

3Why the Downstream Service Breaks

Many services give an explicit null a meaning of its own. With JSON Merge Patch, for example, "discount": null means remove the discount, while a missing discount means leave it unchanged. A validator may also reject a null where it accepts a missing field, or a strict schema with additionalProperties set to false may reject total because it was never part of the contract.

So the downstream service is not wrong. The client's contract changed without anyone deciding to change it, because the contract was defined by a library default rather than by a written schema.

4Fix 1: Tell Jackson to Leave Out Nulls

The direct fix is an inclusion rule. We can set it for the whole mapper, or in Spring Boot with one property.

Java
// Plain Jackson 2.x
ObjectMapper mapper = JsonMapper.builder()
.serializationInclusion(JsonInclude.Include.NON_NULL) // check your version: newer releases prefer
.build(); // changeDefaultPropertyInclusion(...)
// Spring Boot, application.properties
spring.jackson.default-property-inclusion=non_null
// Or for one class only
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderLine { ... }
mapper.writeValueAsString(new OrderLine());
// {"sku":"A-1","quantity":2,"price":5.00,"total":10.00}

The older call mapper.setSerializationInclusion(NON_NULL) does the same thing on a mutable ObjectMapper in Jackson 2. In Jackson 3, the mapper is immutable and is configured only through its builder, so check your version for the exact method name. The annotation on the class is the safest choice when only some types must imitate Gson, because it does not change every other response the application writes.

5Fix 2: Ignore the Computed Getter, or Use Fields Only

For the extra total property, we mark the getter with @JsonIgnore. If the class has many such getters, or we want Jackson to behave like Gson everywhere in this client, we change the visibility rules so that Jackson looks only at fields.

Java
public class OrderLine {
...
@JsonIgnore
public BigDecimal getTotal() { ... } // no longer serialized
}
// Field-only visibility, like Gson
ObjectMapper gsonLike = JsonMapper.builder()
.visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build();
gsonLike.writeValueAsString(new OrderLine());
// {"sku":"A-1","quantity":2,"price":5.00}

Now the output matches the Gson output exactly. Notice that field-only visibility also changes the names: Jackson now uses field names, which matters when a getter and its field were named differently, for example a boolean field active with a getter isActive(). That is usually what we want when imitating Gson, but it is another reason to compare the output rather than assume it.

6Preventing This Before the Next Switch

The real lesson is that the JSON shape must be tested, not trusted. Before switching libraries, we write contract tests that serialize representative objects, including ones with null fields, empty lists and computed values, and compare the result with approved JSON documents. We run them against the old library first so the approved files capture today's real behaviour, then switch the library and see exactly what changed.

Consumer-driven contract tools such as Spring Cloud Contract or Pact take the same idea further: the downstream service publishes what it expects, and the client's build fails if it sends something else.

Java
@Test
void orderLineMatchesTheApprovedShape() throws Exception {
String json = mapper.writeValueAsString(new OrderLine());
JSONAssert.assertEquals(
"{\"sku\":\"A-1\",\"quantity\":2,\"price\":5.00}",
json, JSONCompareMode.STRICT); // fails if discount or total appear
}

7Follow-up questions

Why does Jackson include nulls by default when Gson does not?

The two libraries made opposite choices about what is safer. Jackson's authors chose to show every property so the shape of a type is stable whatever its values, which helps clients that expect every key. Gson's authors chose compact output. Neither is wrong, which is exactly why the choice should be written in a schema rather than left to a default.

What about reading JSON, does the switch change anything there?

Yes. Jackson by default fails on unknown properties, while Gson ignores them, although Spring Boot's auto-configured ObjectMapper turns FAIL_ON_UNKNOWN_PROPERTIES off. Jackson also needs a way to create the object, such as a no-argument constructor, a record constructor or @JsonCreator, while Gson can create objects without calling a constructor at all.

Should we use NON_NULL or NON_ABSENT or NON_EMPTY?

NON_NULL leaves out null values only. NON_ABSENT also leaves out empty Optional values, and NON_EMPTY additionally leaves out empty strings, empty collections and empty arrays. Gson keeps empty lists, so NON_NULL is the closest match when imitating Gson; picking NON_EMPTY would change the contract again.

8Conclusion

In this answer, we saw that Gson serializes fields and omits nulls, while Jackson serializes getters and public fields and includes nulls. A computed getter therefore became a new property and unset fields became explicit nulls, which a downstream service read as a different message. We fixed it with a NON_NULL inclusion rule, @JsonIgnore on computed getters or field-only visibility, and we made the JSON shape a tested contract so the next library change cannot alter it silently.

What to say in the interviewAbout 90 seconds aloud

Open with

The root cause is that the two libraries discover properties differently. Gson reads fields directly with reflection, ignores getters, and leaves out null values unless you call serializeNulls.

Then, if they want more

Jackson builds properties from public getters and public fields, and it writes nulls by default. So after the switch, a field that was null and used to be absent is now sent as null, and a computed method like getTotal, which has no field behind it, suddenly becomes a total property.

The downstream service treats null differently from missing, for example as a delete in a merge patch, so it breaks. To fix it, I would set the inclusion rule to NON_NULL, either on the mapper builder, with spring.jackson.default-property-inclusion=non_null in Spring Boot, or with @JsonInclude on the affected classes if I do not want to change every response.

For computed getters I would add @JsonIgnore, or if I want the client to behave exactly like Gson, I would set Jackson's visibility to fields only. Then I would compare the output field by field, because naming can also change.

Most importantly, before switching libraries I would write contract tests that compare serialized objects with approved JSON in strict mode, run them against the old library first, and only then swap, so any shape change is caught in the build instead of by the downstream team.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After switching a client from Gson to Jackson, a downstream service fails because fields...": https://scrivio-interview-library.netlify.app/java/json-libraries/f70f87b562/