Serialization
23 questions in 7 subtopics.
Java serialization mechanics
How does Java serialization work withObjectOutputStream and ObjectInputStream? What does a class need to be serializable, and how are superclass fields handled?Show how to customise serialization of a class with private writeObject and readObject methods, for example to validate fields after reading. What must the methods call?What do readResolve and writeReplace do? Explain how they are used for singletons and for the serialization proxy pattern.Java serialization was added in 1997 for RMI and distributed objects. Explain why it became a long-term burden for the platform and its users, and what principles you take from it when evaluating new platform features.SerialVersionUID
What isserialVersionUID, what happens if you do not declare it, and when should you change it?After a deployment, a cluster with rolling updates sees InvalidClassException: local class incompatible: stream classdesc serialVersionUID = ... in session replication. What happened and how do you prevent it?Transient fields
What does transient do? After deserialization, what values do transient fields have, and how do you restore them?A class holds a transient finalReentrantLock lock = new ReentrantLock(). After deserialization calls fail with NullPointerException. Explain and give two fixes.Externalizable
Compare Externalizable with Serializable pluswriteObject. When was Externalizable a good choice, and what are its risks?Serialization security and filters
Why is deserializing untrusted data withObjectInputStream dangerous? Explain what a gadget chain is.Configure a deserialization filter so an ObjectInputStream only accepts classes in com.acme.dto, java.lang and java.util, with limits on depth and array size. Show both a pattern filter and the JVM-wide option.A security scan reports that an internal admin service accepts Java serialized objects over HTTP. What steps do you take, in order?A security audit finds Java serialization in a cache library, in JMX remote access and in an old RMI admin tool across about 60 services. Plan the remediation programme across teams.What organisation-wide policy would you set on Java serialization, given that some frameworks, caches and session replication still rely on it?Java's own architects have called serialization a major source of security vulnerabilities, and the platform has added filters rather than removing it. What lessons do you take for designing extension points and data formats with long lives?What would a serialization and deserialization security policy look like for a company with hundreds of Java services, many languages and third-party integrations? Describe its main rules and how you would keep it effective over years.Records and serialization
Givenrecord Range(int lo, int hi) implements Serializable {
Range {
if (lo > hi) throw new IllegalArgumentException();
}
}
JSON and binary alternatives
What are the main alternatives to Java serialization for sending data between services, and what are the trade-offs between JSON and a binary format such as Protobuf?After enabling Jackson default typing (activateDefaultTyping) so a cache can store polymorphic objects, a security review flags it. Why is this risky, and what is the safer setup?Your company is choosing a standard format for data stored long-term, such as event archives kept for seven years, that must stay readable by future services. Compare options and set rules.Your company publishes events on Kafka consumed by dozens of teams in several languages. Choose a serialization format and compatibility policy, and explain how you would enforce it.Over twenty years your company has used Java serialization, XML, SOAP, JSON and now Protobuf. What principles should guide the next data format decisions so migrations cost less?No question matches these choices.