A safe and efficient Java serialization library that captures serialized object graphs into a structured, inspectable intermediate representation, with full support for reading from and writing to standard Java serialization byte streams.
SmallRye Serial decouples the serialization process into two layers:
- Object Graph to Representation: Java objects are serialized into a tree of
Serializednodes that can be examined, transformed, or filtered. - Representation to Byte Stream: The
Serializedrepresentation can be written to or read from the standard Java serialization wire format.
This separation allows for secure class-filtering and graph-inspection before any live Java objects are actually instantiated.
It supports the full range of Java serialization mechanisms:
Serializableclasses (including customwriteObject/readObject)Externalizableclasses- Records
- Enums
- Proxies
writeReplace/readResolve- Circular and self-referencing object graphs
Add the following to the dependencies section of your pom.xml:
<dependency>
<groupId>io.smallrye.serial</groupId>
<artifactId>smallrye-serial</artifactId>
<version>VERSION</version>
</dependency>Replace VERSION with the latest release version.
First, configure a thread-safe SerialContext with standard serialization providers:
SerialContext ctx = SerialContext.builder()
.addDefaultProviders()
.build();Use the context to create a Serializer or Deserializer to convert between Java objects and Serialized representation nodes. While SerialContext is thread-safe, individual serializers and deserializers are not.
// Create a serializer and serialize an object to its intermediate representation
Serializer serializer = ctx.createSerializer();
Serialized serialized = serializer.serialize(myObject);
// Create a deserializer and deserialize back into a live object
Deserializer deserializer = ctx.createDeserializer();
Object restored = deserializer.deserialize(serialized);Use SerialStreamWriter and SerialStreamReader to write and read the intermediate representation to/from raw streams using the standard Java serialization format (compatible with ObjectOutputStream/ObjectInputStream wire format).
// Write the representation to a byte stream
try (SerialStreamWriter writer = SerialStreamWriter.builder(outputStream).build()) {
writer.writeSerialized(serialized);
}
// Read the representation from a byte stream
try (SerialStreamReader reader = SerialStreamReader.builder(inputStream).build()) {
Serialized readSerialized = reader.readSerialized();
}The module name is io.smallrye.serial.
module my.module {
requires io.smallrye.serial;
}mvn verifyPlease refer to the SmallRye Wiki for the Contribution Guidelines.
This project is licensed under the Apache License 2.0.