Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

When would you use @JsonCreator, @JsonValue and mix-ins instead of a custom serializer?

These annotations cover most cases where people reach for a custom serializer. @JsonValue writes an object as a single value, such as an id type as a plain string; @JsonCreator marks the constructor or factory method Jackson should use to read the object, which is how immutable types and single-value types are deserialized; and mix-ins attach Jackson annotations to classes we cannot change. They are simpler than full serializers and keep our types clean, and since Jackson 2.12 records work without any annotations at all.

Why this question is asked

Custom serializers are powerful, but each one is code to maintain, test and later migrate to a new Jackson version. Most of the time we do not need that power: we just want an id to appear as a string, an immutable class to be readable, or a third-party class to be shaped a little differently. Interviewers ask this to see whether we know the lighter tools and when each fits. In this answer, we will look at @JsonValue, then @JsonCreator in both its forms, then mix-ins for classes we do not own, see what records give us for free, and finish with when a real serializer is still the right choice.

1@JsonValue: One Object, One Value

Small wrapper types are common in well-designed code: OrderId instead of a bare long, Email instead of a String. By default Jackson writes them as objects with one field, which is noise for the client. @JsonValue on a method or field tells Jackson to serialize the whole object as the value that member returns.

Java
public record OrderId(String value) {
@JsonValue
public String value() { return value; } // the whole object is written as this string
@JsonCreator
public static OrderId of(String value) { // and read back from a plain string
if (!value.startsWith("ord-")) throw new IllegalArgumentException("not an order id: " + value);
return new OrderId(value);
}
}
record Order(OrderId id, int quantity) {}
mapper.writeValueAsString(new Order(new OrderId("ord-42"), 3));
// without @JsonValue: {"id":{"value":"ord-42"},"quantity":3}
// with @JsonValue: {"id":"ord-42","quantity":3}
mapper.readValue("{\"id\":\"ord-42\",\"quantity\":3}", Order.class).id(); // OrderId[value=ord-42]
mapper.readValue("{\"id\":\"42\",\"quantity\":3}", Order.class); // ValueInstantiationException -> 400

Notice that @JsonValue only handles writing. To read "ord-42" back, Jackson needs to know how to build an OrderId from a string, and that is what the @JsonCreator factory does. Putting validation in the factory gives us one place that guards every way an OrderId enters the system. The same @JsonValue trick works on enums, for example to write PaymentStatus.PAID as paid.

2@JsonCreator: Telling Jackson How to Build an Object

To read JSON into an object, Jackson must create it. For a class with a no-argument constructor and setters, it calls those. Immutable classes have neither, so we mark a constructor or static factory with @JsonCreator. It has two modes. In delegating mode, the whole JSON value is passed as one argument, as in the OrderId factory above, which reads a plain string. In properties mode, each parameter is bound to a named JSON property.

Java
public final class Shipment {
private final String trackingNumber;
private final int weightGrams;
@JsonCreator
public Shipment(@JsonProperty("trackingNumber") String trackingNumber,
@JsonProperty("weightGrams") int weightGrams) {
if (weightGrams <= 0) throw new IllegalArgumentException("weight must be positive");
this.trackingNumber = trackingNumber;
this.weightGrams = weightGrams;
}
public String getTrackingNumber() { return trackingNumber; }
public int getWeightGrams() { return weightGrams; }
}
// {"trackingNumber":"TR-9","weightGrams":1200} -> Shipment built through the constructor

Notice the @JsonProperty names on the parameters. They are needed unless the class is compiled with the -parameters flag and the parameter names module is registered, which Spring Boot does for its mapper; being explicit makes the class work with any mapper and survives a parameter rename. For a single-argument creator, Jackson sometimes cannot tell which mode we mean, so we say it with @JsonCreator(mode = JsonCreator.Mode.DELEGATING) or PROPERTIES when it matters.

3Mix-ins: Annotations for Classes We Cannot Change

Sometimes the class comes from a library or a generated model, and we cannot add annotations to it. A mix-in is a separate class or interface that declares the same members with the annotations we want; we tell the mapper to treat the mix-in's annotations as if they were on the target class.

Java
// Third-party class: no annotations, no default constructor
public class GeoPoint {
public GeoPoint(double lat, double lon) { ... }
public double getLat() { ... }
public double getLon() { ... }
public String getInternalCacheKey() { ... } // we do not want this in JSON
}
abstract class GeoPointMixin {
@JsonCreator
GeoPointMixin(@JsonProperty("lat") double lat, @JsonProperty("lon") double lon) {}
@JsonIgnore abstract String getInternalCacheKey();
}
mapper.addMixIn(GeoPoint.class, GeoPointMixin.class); // plain Jackson
// Spring Boot: annotate GeoPointMixin with @JsonMixin(GeoPoint.class) and it is registered for us
// {"lat":52.52,"lon":13.40} <-> GeoPoint, without internalCacheKey

Notice that the mix-in's constructor and methods are never called; they only carry annotations. The member signatures must match the target's exactly, or the annotations silently apply to nothing, so a quick round-trip test is worth writing whenever we add a mix-in. Mix-ins are also useful for keeping Jackson annotations out of a domain module entirely.

4Records Need No Annotations

Since Jackson 2.12, records are supported directly. Jackson uses the record components as properties for writing, and the canonical constructor as a properties-based creator for reading, with the component names that records always keep at runtime. So a plain DTO record needs no @JsonCreator and no @JsonProperty, and that is the simplest option of all for new code.

Annotations are still welcome on records when we want something different: @JsonProperty("order_id") on a component to rename it, @JsonValue for a single-value record as with OrderId, or a compact constructor for validation, which Jackson calls like any constructor.

Choosing the lightest tool
NeedToolExample
Object as a single value@JsonValueOrderId -> "ord-42"
Read a single value back@JsonCreator (delegating)"ord-42" -> OrderId
Immutable class with fields@JsonCreator + @JsonPropertyShipment constructor
Plain DTOa record, no annotationsrecord Order(OrderId id, int quantity)
Class we cannot changemix-inGeoPoint from a library
Logic in the formatcustom serializerMoney with currency-based scale
Move down the list only when the tool above cannot express what we need.

5When a Custom Serializer Is Still Right

Annotations describe structure: which constructor, which property, which name. When the format itself needs logic, such as a money amount scaled to the currency's decimals, a value that is written differently depending on a setting, or a type whose JSON shape does not match any of its members, a custom serializer is clearer than bending annotations to do it. The rule of thumb is to start with records and annotations, and write a serializer only when the JSON is not simply a view of the object's members.

6Follow-up questions

Can @JsonValue and @JsonCreator be used on enums?

Yes. @JsonValue on a method that returns a code writes the enum as that code, and Jackson also uses it when reading, matching incoming strings against the @JsonValue output. A @JsonCreator factory on the enum is useful when matching needs logic, such as accepting old codes for compatibility.

What happens if I put @JsonValue on two members?

Jackson fails when it builds the serializer, because it cannot know which value to use. There can be only one @JsonValue per class, including inherited members.

Do mix-ins work with records and Kotlin data classes?

Yes, mix-ins work with any class, but the member signatures must match exactly, including accessor names such as lat() for a record. For Kotlin, the Kotlin module handles most cases without mix-ins, so check whether it already does what we need first.

7Conclusion

In this answer, we saw the lighter alternatives to custom serializers. @JsonValue writes an object as one value, such as an id as a string, and a delegating @JsonCreator reads it back and is a good place for validation. A properties-based @JsonCreator with @JsonProperty lets Jackson build immutable classes, mix-ins attach annotations to classes we cannot change, and records work with no annotations at all since Jackson 2.12. A custom serializer remains the right tool only when the JSON format needs real logic.

What to say in the interviewAbout 90 seconds aloud

Open with

I start from the lightest tool and only write a custom serializer when the format needs logic. For a wrapper type like OrderId, I put @JsonValue on its accessor, so the whole object is written as a plain string, ord-42, instead of an object with a value field.

Then, if they want more

To read it back I add a @JsonCreator factory taking a String, which Jackson uses in delegating mode, and that's also a good place for validation.

For immutable classes with several fields, @JsonCreator on the constructor with @JsonProperty on each parameter tells Jackson how to build them without setters or a default constructor; with -parameters and the parameter names module, which Boot registers, the names can even be inferred, though I prefer being explicit.

For classes I can't change, like a library type, I use a mix-in: a separate abstract class with the same signatures carrying the annotations, registered with addMixIn or with @JsonMixin in Spring Boot. And for new DTOs I simply use records, which Jackson has supported since 2.12 with no annotations: components become properties and the canonical constructor is the creator.

These keep the types clean and there's no serializer code to maintain or migrate. I'd only write a full serializer when the output needs real logic, like formatting money with currency-dependent decimals.

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 documentation below. 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 "When would you use @JsonCreator, @JsonValue and mix-ins instead of a custom serializer?": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/6878d8f96b/