Introduction
Java object serialisation is the built-in way for a Java
program to turn objects into bytes and back, using ObjectOutputStream and
ObjectInputStream. It has been part of Java since version 1.1. Files that
hold such a stream are often named .ser, and the same bytes travel over the
network in Java RMI and in many older enterprise systems.
Reading a stream creates objects of whatever classes it names, and some of those classes run code as they are rebuilt. Deserialising data from an untrusted source has led to many serious security holes, so it should be avoided or strictly filtered.
Structure
A stream starts with the magic number AC ED and the version
00 05. After that comes a run of items, each started by a type code byte,
such as 73 for a new object, 72 for a class description,
74 for a string, 75 for an array and 71 for a
reference back to an object already written. Numbers are big-endian.
A class description gives the class name, a 64-bit serial version number and the name and type of each field. The object’s field values follow, for each class from the top of its inheritance chain down, and objects already in the stream are referred to by number. The stream grammar and codes page gives every type code and flag.
Not described
The reference follows chapters 1, 2 and 6 of the Java Object Serialization Specification (Java SE 21).
Known, not yet covered
- Reference: the serialized form of every platform class. The
grammar page now lays out
the common ones (String, Class, the boxed numbers, BigInteger, BigDecimal, the main
collections, Date, UUID, BitSet, Throwable, the immutable collections and all of
java.time); the other roughly 600 classes on the Java SE 21 “Serialized Form” page (Collections wrappers, concurrent collections, atomics, text formats, chronologies, the other exceptions) are documented there and were not transcribed. TheputFields/readFieldsmechanism andreadObjectNoDataare described on the same page. - Viewer: not re-audited in this pass.
Links
Software
- SerializationDumper – prints a serialisation stream as readable text
Developers
- ObjectInputStream – with ObjectOutputStream, the reader and writer in the Java standard library (Java)
Reference
- Object Serialization Stream Protocol – the Java specification of the stream