Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does Java serialization handle enums, and how do JSON libraries such as Jackson serialize them by default? What breaks when a constant is renamed?

Java serialization writes only an enum constant's name and, on reading, looks it up with valueOf(), ignoring the enum's fields and any custom serialization methods. Jackson also writes name() by default and reads it back by name. So renaming a constant breaks reading old data: Java serialization throws InvalidObjectException and Jackson throws InvalidFormatException. We keep the external value stable through @JsonProperty or @JsonValue, or an explicit mapping, so code can be renamed without breaking stored data, and we avoid ordinal-based options because they are fragile.

Why this question is asked

Enums end up outside the JVM all the time: in session data, caches, Kafka messages, JSON APIs and database columns. Each of those stores the enum in some external form, and that form outlives the code that wrote it. Interviewers ask how serialization handles enums because a casual rename in an IDE can break production data that nobody thought of. In this answer, we will look at what Java serialization writes for an enum, what Jackson does by default, what exactly breaks on a rename, and how to decouple the external value from the Java name.

1A Quick Recap: Serialization Writes an External Form

Serialization turns an object into bytes or text so it can be stored or sent, and deserialization turns it back into an object. For most classes, Java serialization writes every non-transient field, and deserialization creates a new object and fills the fields in. Enums are treated specially, because creating a new instance would break the guarantee that each constant is a singleton.

2What Java Serialization Writes for an Enum

For an enum constant, ObjectOutputStream writes a marker that says this is an enum, a description of the enum class, and the constant's name as a string. Nothing else. The constant's fields are not written, and methods such as writeObject(), readObject(), readResolve() and writeReplace() are ignored if the enum declares them. The serialVersionUID of every enum is fixed at 0, so it plays no part either.

Java
enum Plan {
FREE(0), PRO(20);
private final int price; // never serialized
Plan(int p) { price = p; }
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (var out = new ObjectOutputStream(bytes)) { out.writeObject(Plan.PRO); } // writes "PRO"
try (var in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
Object p = in.readObject(); // Enum.valueOf(Plan.class, "PRO")
System.out.println(p == Plan.PRO); // true: the existing constant, not a copy
}
Step throughJava serialization of Plan.PRO
Writer JVMByte streamReader JVM
enum marker + class Plan + name "PRO"
read class and name
Enum.valueOf(Plan.class, "PRO")
return the existing Plan.PRO

4 messages in order. Play them, or step through them one at a time.

Only the name crosses the boundary; fields and custom methods play no part.

On reading, ObjectInputStream takes the name and calls Enum.valueOf() for the enum class, so the result is the existing constant. That is why singletons stay singletons and == keeps working after deserialization. It also means a field value in the stream could never be stale, because there is none: the constant's fields come from the current code.

3What Jackson Does by Default

Jackson, the JSON library Spring Boot uses, also uses the name by default. When it writes an enum, it calls name(), not toString(), and when it reads, it matches the JSON string against the constants' names.

Java
record Subscription(String user, Plan plan) {}
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(new Subscription("ana", Plan.PRO)); // {"user":"ana","plan":"PRO"}
mapper.readValue("{\"user\":\"ana\",\"plan\":\"PRO\"}", Subscription.class); // plan = PRO

Jackson has options that change this. One writes toString() instead of name(), another writes the ordinal as a number, and by default Jackson 2 also accepts a number when reading and treats it as an ordinal. The ordinal options are fragile for the same reason as anywhere else: inserting or reordering constants changes the numbers, so the same JSON would mean a different constant. We should leave them off, and consider enabling the setting that rejects numbers for enums.

4What Breaks When a Constant Is Renamed

Now someone renames PRO to PREMIUM, a harmless refactoring inside the code. Every external form that holds PRO is now a name that no longer exists.

Java
enum Plan { FREE(0), PREMIUM(20); ... }
// Java serialization of old bytes:
// java.io.InvalidObjectException: enum constant PRO does not exist in class Plan
// Jackson with old JSON {"plan":"PRO"}:
// InvalidFormatException: Cannot deserialize value of type `Plan` from String "PRO":
// not one of the values accepted for Enum class: [FREE, PREMIUM]
Where an old name can still be waiting
StoreTypical formatFailure after a rename
HTTP session storeJava serializationInvalidObjectException on load
Cache (Redis, Hazelcast)Java serialization or JSONread errors, cache misses
Kafka topicJSON, Avroconsumer fails on old messages
Database JSON columnJSONInvalidFormatException on read
Mobile or web clientsJSON APIrequests with the old value rejected

Notice where the old data lives: HTTP sessions persisted across a restart, a Redis cache with Java-serialized values, messages waiting in a Kafka topic, JSON documents in a database, and clients still on the previous app version. All of them fail after the rename. Some failures are loud, such as a consumer that cannot read its topic. Others are quieter, such as a cache that throws on every read and silently falls back to the database, adding load.

5Keeping the External Value Stable

The fix is to separate the external value from the Java name, so the code can be refactored while stored data keeps its meaning. With Jackson, @JsonProperty on a constant sets the value used in JSON, and @JsonValue on a method makes that method's result the external value for every constant. Reading uses the same value.

Java
enum Plan {
@JsonProperty("PRO") PREMIUM(20), // Java name changed, JSON value did not
@JsonProperty("FREE") FREE(0);
...
}
mapper.writeValueAsString(Plan.PREMIUM); // "PRO"
mapper.readValue("\"PRO\"", Plan.class); // PREMIUM
// Or an explicit code for every constant
enum Plan2 {
FREE("free"), PREMIUM("pro");
private final String code;
Plan2(String c) { code = c; }
@JsonValue public String code() { return code; } // used for writing and reading
}

For Java serialization there is no such annotation, because the name is the only thing written. If Java-serialized enums must survive a rename, we either keep the old constant name, or avoid Java serialization for durable data altogether, which is wise for other reasons too. For JPA, the same principle means @Enumerated(EnumType.STRING) only while names never change, or an AttributeConverter with an explicit code. And whichever mechanism we use, a test that pins the external value of each constant makes an accidental rename fail the build.

6Follow-up questions

Why are readResolve() and readObject() ignored for enums?

Because the specification makes enum deserialization a lookup by name, so there is no new object to customize. That rule is what guarantees the singleton property; letting enums run custom serialization code would allow it to be broken.

Can Jackson accept both the old and the new name during a migration?

Yes, there are ways: a @JsonCreator factory method that maps both strings, or @JsonAlias on the constant in recent Jackson versions (check your version). That lets us read old data while writing the new value, and later remove the old one.

Does renaming break Java serialization even if serialVersionUID is unchanged?

Yes. Enums ignore serialVersionUID, which is always 0 for them. The only thing that matters is whether the stored name still exists as a constant.

7Conclusion

In this answer, we saw that Java serialization writes only an enum constant's name and resolves it with valueOf() on read, ignoring fields and custom serialization methods, which keeps constants singletons. Jackson also writes name() and reads by name by default. Renaming a constant therefore breaks every stored copy of the old name, with InvalidObjectException or InvalidFormatException. We keep the external value stable with @JsonProperty, @JsonValue or explicit codes, avoid ordinal-based options, and pin the external values with a test.

What to say in the interviewAbout 80 seconds aloud

Open with

For enums, Java serialization writes only the constant's name, plus the class description. It does not write the fields, and it ignores writeObject, readObject, readResolve and serialVersionUID, which is always zero for enums.

Then, if they want more

On reading, ObjectInputStream calls Enum.valueOf with that name and returns the existing constant, which is how enums stay singletons through serialization. Jackson behaves similarly by default: it writes name, not toString, and reads by matching the name. There are options to use toString or the ordinal, but ordinals are fragile because reordering changes them, so I keep them off.

The consequence is that renaming a constant is a breaking change for stored data. If PRO becomes PREMIUM, old Java-serialized bytes fail with InvalidObjectException saying the enum constant does not exist, and old JSON fails with InvalidFormatException. That data can be anywhere: session stores, caches, Kafka topics, JSON columns, older clients.

So I separate the external value from the Java name: @JsonProperty on each constant, or an explicit code field with @JsonValue, and an AttributeConverter for the database. Then the code can be renamed freely. I would also avoid Java serialization for durable data, and add a test that pins each constant's external value.

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 "How does Java serialization handle enums, and how do JSON libraries such as Jackson...": https://scrivio-interview-library.netlify.app/java/enums/9d92f5b63b/