Java Serialization with non serializable parts

Java Serialization with non serializable parts

In Java, when you need to serialize an object that contains non-serializable parts, you can implement custom serialization by using the writeObject() and readObject() methods. This allows you to control how the non-serializable parts are handled during the serialization and deserialization process.

Here's an example of how to serialize an object with non-serializable parts:

import java.io.*;

class NonSerializablePart {
    private String data;

    public NonSerializablePart(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }
}

class SerializableObject implements Serializable {
    private transient NonSerializablePart nonSerializablePart;
    private String serializableData;

    public SerializableObject(String nonSerializableData, String serializableData) {
        this.nonSerializablePart = new NonSerializablePart(nonSerializableData);
        this.serializableData = serializableData;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        // Serialize the serializable part
        out.defaultWriteObject();

        // Serialize the non-serializable part manually
        out.writeObject(nonSerializablePart.getData());
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        // Deserialize the serializable part
        in.defaultReadObject();

        // Deserialize the non-serializable part manually
        String nonSerializableData = (String) in.readObject();
        nonSerializablePart = new NonSerializablePart(nonSerializableData);
    }

    public NonSerializablePart getNonSerializablePart() {
        return nonSerializablePart;
    }

    public String getSerializableData() {
        return serializableData;
    }
}

public class CustomSerializationExample {
    public static void main(String[] args) {
        try {
            // Serialize the object
            SerializableObject objToSerialize = new SerializableObject("Non-Serializable Data", "Serializable Data");
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("serialized_object.ser"));
            out.writeObject(objToSerialize);
            out.close();

            // Deserialize the object
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("serialized_object.ser"));
            SerializableObject deserializedObj = (SerializableObject) in.readObject();
            in.close();

            // Access the deserialized parts
            System.out.println("Serializable Data: " + deserializedObj.getSerializableData());
            System.out.println("Non-Serializable Data: " + deserializedObj.getNonSerializablePart().getData());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

In this example:

  1. We have a SerializableObject class that contains a non-serializable part (NonSerializablePart) and a serializable part (serializableData).

  2. We implement the writeObject() and readObject() methods to manually serialize and deserialize the non-serializable part.

  3. During serialization, we first call out.defaultWriteObject() to serialize the serializable part. Then, we manually serialize the non-serializable part.

  4. During deserialization, we first call in.defaultReadObject() to deserialize the serializable part. Then, we manually deserialize the non-serializable part.

By implementing custom serialization, you can handle non-serializable parts of your object while still being able to serialize and deserialize the object as a whole.


More Tags

jenkins-workflow ion-select thymeleaf iqkeyboardmanager show-hide formatted-input laravel-5.1 force.com dynamics-crm-365 numpy

More Java Questions

More Tax and Salary Calculators

More Chemistry Calculators

More Animal pregnancy Calculators

More Pregnancy Calculators